Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i execute one event in php based on a probability for the event to happen

i want something like this:

$chance = 40; //40%
if ( run the probability check script ) {
    echo "event happened"; //do the event
}
else {
   echo "event didn't happened";
}

what is the best solution to achieve something like that?

thanks a lot!

like image 930
Artur Mendes Avatar asked Feb 12 '12 21:02

Artur Mendes


1 Answers

Use rand():

if (rand(1,100)<=$chance)

This will return a number between 1 and 100, so that the probability of it being lower or equal to 40 is 40%.

like image 173
Dor Shemer Avatar answered Nov 13 '22 23:11

Dor Shemer