calculating a percent chance

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kryppienation
Forum Newbie
Posts: 9
Joined: Thu Dec 04, 2008 1:12 am

calculating a percent chance

Post 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++;
 
 
}
 
 
 
 
 
?>
Last edited by Benjamin on Tue Jun 09, 2009 11:34 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: calculating a percent chance

Post 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";
There are 10 types of people in this world, those who understand binary and those who don't
kryppienation
Forum Newbie
Posts: 9
Joined: Thu Dec 04, 2008 1:12 am

Re: calculating a percent chance

Post by kryppienation »

thanks :)
Post Reply