Hi there,
I've got a bit of a tricky question here.
I want to make a random selection from, say, 5 different choices. If I wanted to have an equal chance of choosing each one, then I could just create a random number between 0 and 1, multiply this by 5, and this would select my choice. No prob.
What I would like to do, however, is make a choice when there are different probabilities for each choice. Lets say, 5% for choice 1, 20% for choice 2, 30% for choice 3 etc.
If I had the percentages for each choice then I might say that given a random number between 1 and 100, then choice 1 would get chosen if the number were between 1 and 5, choice 2 would get chosen if the number were between 6 and 20, choice 3 would get chosen if the number were between 21 and 50 etc.
So far so good, but what I can't get my head around is how I would code the fact that choice 2 is '5 to 20' !!
I thought of having an array with the total number for the possiblities (this might be more than 100, for instance if I had a choice that had a probability of 30, but there were 4 other choices with a probability of 50, then the total would be out of 230). So in this case the array would be 230 units long. Then I would fill 0 to 29 with a
string representing the first choice and so on. When I get my random number between 0 and 229 then I check the array to find the id string with the name of my choice. I realise that this is a pretty rubbish solution, especially as my program will need to make lots of these probability decisions and I don't know how heavy dutymaking an array is that is that long.
Another solution would be to use if statements and say 'if the random number is between 0 and 29 then go to choice 1' etc. The problem with this is the solution needs to be able to handle different numbers of choices with varying probabilities.
Any ideas would be very much appreciated.
Cheers guys
Joe