playMaker

Author Topic: random weight  (Read 2710 times)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
random weight
« on: October 02, 2014, 08:52:41 AM »
hi,

i like to know more about how the random weight works

for example i have 2 random events, both set to 1.0, this is a 50/50% right?

but when it is set to 0.9 and 0.1 is it  90% / 10% then?

when i have 3 items. set like this 1.0 / 0.8 / 0.8 how does the chance % work then?

basically i would like to know how this calculates.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: random weight
« Reply #1 on: October 03, 2014, 05:59:19 AM »
does it work like this ?

1.0 + 0.8 + .08 = 2.6 and the random fall inside this given number .

item 1 : 1.0 chance out of 2.6
item 2 : 0.8 chance out of 2.6
item 3 : 0.8 chance out of 2.6

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: random weight
« Reply #2 on: October 13, 2014, 11:35:32 AM »
anyone knows how this works?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: random weight
« Reply #3 on: November 27, 2014, 02:05:29 AM »
Hi,

 I don't know to be honest. I think it that if you have two values weighted at 0.1 and the other at 0.9, then it's 90% of chance and 10% of chance to get them. that is if the sum of the weights are 100.

 Bye,

 Jean

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: random weight
« Reply #4 on: December 13, 2019, 05:08:19 PM »
Has anyone figured out the dark magic behind.weighted values? I still can't grasp the concept.
Available for Playmaker work

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: random weight
« Reply #5 on: December 14, 2019, 04:09:55 AM »
Hi.
This is the code of the weighted :

Code: [Select]
/// <summary>
        /// Given an array of weights, returns a randomly selected index.
        /// </summary>
        public static int GetRandomWeightedIndex(FsmFloat[] weights)
        {
            float totalWeight = 0;

            foreach (var t in weights)
            {
                totalWeight += t.Value;
            }

            var random = Random.Range(0, totalWeight);

            for (var i = 0; i < weights.Length; i++)
            {
                if (random < weights[i].Value)
                {
                    return i;
                }

                random -= weights[i].Value;
            }

            return -1;
        }