C++ function for picking from a list where each element has a distinct probability
- by Stuart
I have an array of structs and one of the fields in the struct is a float.  I want to pick one of the structs where the probability of picking it is relative to the value of the float.  ie
struct s{
  float probability;
  ...
}
sArray s[50];
What is the fastest way to decide which s to pick?  Is there a function for this?  If I knew the sum of all the probability fields (Note it will not be 1), then could I iterate through each s and compare probability/total_probability with a random number, changing the random number for each s? ie
if( (float) (rand() / RAND_MAX) < probability)...