Page 1 of 1

calculating a percent chance

Posted: Tue Jun 09, 2009 3:12 am
by kryppienation
Hi guys I'm having a little bit of trouble figuring out how to calculate a percent chance of something happening. I am wondering if someone would help me figure out how to get this little script to display something on a percent chance.

All i want this to do is display $action1 based on the percentage in $percentage, otherwise $action2. In this example i would like it to display "Battle" 100% of the time, but i need this number to be able to change to any percent between 0 and 100. Like if i change it to 35, i would like there to be a 35% chance "Battle" will pop up and a 65% chance that "Nothing" will pop up. Thanks to anyone who can help me with this. ???

Code: Select all

<?php
 
$action1 = "Battle";
$action2 = "Nothing";
$percentage = 100;  //this should be able to be changed between zero and one hundred percent chance of, for test purpose i want $action1 to show 100% of the time, otherwise show $action2
 
$loop = 1;
 
while ($loop <= 100){
 
 
 
 
 
echo "looping $loop <br>" ;
 
 
$loop++;
 
 
}
 
 
 
 
 
?>

Re: calculating a percent chance

Posted: Tue Jun 09, 2009 3:32 am
by VladSun
kryppienation wrote:i would like there to be a 35% chance "Battle" will pop up and a 65% chance that "Nothing" will pop up...

Code: Select all

if (mt_rand(0, 100) <= 35)
    echo "Battle";
else
    echo "Nothing";

Re: calculating a percent chance

Posted: Tue Jun 09, 2009 3:56 am
by kryppienation
thanks :)