Imagine you have an array of hashes representing competitor and their probability to win the prize (a float between 0 and 1). Like:
[ {:name => "Adam" , :prob => 0.5}
{:name => "Ben" , :prob => 1.0}
{:name => "Chris" , :prob => 0.1}
{:name => "Daniel" , :prob => 0.2}
{:name => "Ed" , :prob => 0.7}
{:name => "Frey" , :prob => 0.5}
{:name => "Gilbert" , :prob => 0.3}
]
I would like to have an algorithm on which I can select three winners using random numbers but respecting the probability of each person.
The total probability of the sample is 3.3
A logical approach would be to calculate the random value like:
val = rand(33)/10.0
And scan the array until I get the person which reaches the random number.
This approach works but it implies a scan in the array.
I wonder if there would be a more straightforward solution. Any ideas?
PS: Imagine the array might have a great amount of elements.