Probability modifiers

Started by
3 comments, last by walsh06 10 years, 11 months ago

I am currently working on something and it involves a lot of probability.I have standard probabilities and I do this by randomizing a number and then if statements for each one. eg I have 40%, 30%, 20% and 10% of each event. I then have

n = (random number between 0 and 100)

if(n < 40)

else if(n < 70)

else if(n < 90)

else

But now Im trying to affect these probabilities by other factors and I want to update the ifs accordingly. But this involves a lot of calculations as I have to get the new probability and the difference from the last one in order to affect the if statements. So is there another way of programming probability that makes affecting these numbers easier.

Advertisement

Yes, add up all the proportions, divide by the total (basically normalising them), and use a roll from 0.0 to 1.0.

So if we have proportions - A 4, B 6, C 3, D 1 we have 14 total. Roll between 0 and 1

if(roll <= 4 / 14.0) doA

else if(roll <= (4 + 6) / 14.0) doB

else if(roll <= (4 + 6 + 3) / 14.0) doC

else doD

The pattern should be clear. Always end with an else (with no check) in case of rounding error.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

That was what I had in mind but I wanted to avoid as it meant doing a lot of calculations but it does seem like I have no choice.

You can do the calculations as you go along (by summing up the cumulative probability before you do the test in the if) if you want. I doubt it will be a bottleneck in your code though, so I'd go with the simplest and easiest to understand solution to the problem.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Cool thanks for the help. I have adjusted them to the way I want going by your method and it seems to be working.

This topic is closed to new replies.

Advertisement