# Probability modifiers

This topic is 2054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites

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

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633694
• Total Posts
3013373
×