Jump to content
  • Advertisement
Sign in to follow this  
walsh06

Probability modifiers

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!