Jump to content

Probability of an action happening in PHP.


PhpNewb314

Recommended Posts

I wonder how I can execute an action which has x% chance of happening, for instance a 31% chance of happening. How would I do that? I was thinking of using rand() to generate a number between 1 and 100 and if it is <= 50 (or whatever the percentage is, since that is going to vary based on the rank of the individual and hopefully fluctuate within ranks) then it is successful and if it is >50 then it isn't successful. 

 

 

This is just a preliminary idea and is definitely going to get more complex since I need to vary the percentage depending on rank and even slight fluctuation within rank (it is a game) but I was wondering about the best way to do this aspect of it. Is this the best way or is there a more efficient (or more elegant) way?

 

Thanks for any advice!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.