A slow but easy way to do this is so that each member can choose a random number based on its probability and choose the one that matters the most.
Analogy:
Imagine that you need to choose 1 out of 3 people, but they have different probabilities. You let them die with a different number of faces. The first person of the bone has 4 faces, the 2nd person is 6 and the third person is 8. They roll their dice, and the one with the most victories wins.
Let's say we have the following list:
[{A,50},{B,100},{C,200}]
pseudo code:
A.value = random(0 to 50); B.value = random(0 to 100); C.value = random (0 to 200);
We choose the one that matters the most.
This method above does not accurately represent probabilities. For example, 100 will not have a double chance of 50. But we can do this by slightly changing the method.
Method 2
Instead of choosing a number from 0 to weight, we can limit them from the upper limit of the previous variable to adding the current variable.
[{A,50},{B,100},{C,200}]
pseudo code:
A.lowLimit= 0; A.topLimit=50; B.lowLimit= A.topLimit+1; B.topLimit= B.lowLimit+100 C.lowLimit= B.topLimit+1; C.topLimit= C.lowLimit+200
end limits
A.limits = 0,50 B.limits = 51,151 C.limits = 152,352
Then we select a random number from 0 to 352 and compare it with each variable limit to see if the random number is within it.
I believe this setting has better performance since there is only 1 random generation.
Other answers have a similar method, but this method does not require the sum to be 100 or 1.00.
WVrock Nov 15 '14 at 16:56 2014-11-15 16:56
source share