Probability of selection

retkehing

Well-known member
Joined
Feb 5, 2006
Messages
153
Programming Experience
Beginner
Anyone can tell me how to implement an idea like selecting one array item from three items
according to their chances of selection, 0.2, 0.1 and 0.7 into VB coding?
 
Never really thought about implementing probability... so I'm sure there are better ways.
If elements are ABC and you put these into a 10 elements array like this ACCBBBBBBB you could pick a random number between 1-10 and the result would be the index of the element picked at requested probability :) This was just to give a visual presentation of the problem and solution.
Rnd function returns a Single between 0-1, so you can check if the random number is in range 0-0.1/0.1-0.3/0.3-1 and choose respectively first/second/third element from this test.
 
I would use JohnH's solution, but with the Random class. More specific: it's Next method with 10 as a parameter. This will yield results in the range 0-9 (both included), I think you can work out the math to pick a selection.

Make sure you always use the same instance of the Random object: make it a (Shared) member, or a static local variable.
 
I too would use a Random object but I would use its NextDouble method to return a number 0.0 >= number > 1.0. That way you just have to compare the number returned to the actual probabilities, regardless of how many decimal places they include. Note that Don Delegates suggestion will work fine for the values you've suggested, but add a few more decimal places and you'd have to change the 10 to something greater. With NextDouble you would test the number returned against the probability of the first item. If it is less then select the first item. If it's greater or equal then subtract the probability of the first item and move on to the second item. Continue until you find a matching item. You'll notr the lack of code as I suspect that this might be homework.
 
But actually what I want to do is to select one item from the whole array according to the Pow(rate, n) and n is their number of order in array, in other words,

rate=0.43
a(1)=6
a(2)=3
a(3)=7

the chances of being selected for each of them will be
a(1)=Pow(0.43, 1)
a(2)=Pow(0.43, 2)
a(3)=Pow(0.43, 3)
 
Last edited:
You would need to normalise the probabilities so that their sum was 1. Then you can do exactly what I said previously. As a bsic example, let's say that the probabilites were 8, 4, and 6. You would normalise them to make there sum 1 by dividing each by the actual sum, so the probabilities become (8 / (8 + 4 + 6), (4 / (8 + 4 + 6) and (6 / (8 + 4 + 6). The propertions are the same and the sum is now 1, so you can use the method I suggested above.
 
Back
Top