kryppienation Posted June 9, 2009 Share Posted June 9, 2009 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. ??? <?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++; } ?> Quote Link to comment Share on other sites More sharing options...
Dathremar Posted June 9, 2009 Share Posted June 9, 2009 $percentage = rand( 0, 100); That will be a random number between 1 and 100. And if You want that percentage to be changing according to some rules or conditions then tell us what. Quote Link to comment Share on other sites More sharing options...
kryppienation Posted June 9, 2009 Author Share Posted June 9, 2009 no i mean i know how to make a random number, for now i just want that number to be 100 percent. What i'm trying to do is simply have this script display results based on a percent chance. In this new one i have it displaying 25, but the end result is not 25 battles and 75 nothings. <?php $action1 = "Battle"; $action2 = "Nothing"; $percentage = 25; //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; $act1c = 0; $act2c = 0; while ($loop <= 100){ //pick a random number $randomnumber = rand(0, 100); if($randomnumber <= $percentage){ $action = $action1; $act1c++; } else { $action = $action2; $act2c++; } echo "looping $loop - $action <br>" ; $loop++; } echo "total battles = $act1c | total nothing = $act2c"; ?> here are some of the results: Battles, Nothings 35, 65 28, 72 21, 79 29, 71 30, 70 26, 74 Quote Link to comment Share on other sites More sharing options...
Dathremar Posted June 9, 2009 Share Posted June 9, 2009 Hmm If i get You right $percentage = 25; // Put here the random number for dynamic percentage $act1c = 0; $act2c = 0; for ($i = 1; $i <= $percentage; $i++){ $action = $action1; $act1c++; } for ($j = $percentage; $j <= 100; $j++){ $action = $action2; $act2c++; } Is this better or I am still missing the point :S Quote Link to comment Share on other sites More sharing options...
kryppienation Posted June 9, 2009 Author Share Posted June 9, 2009 honestly, because i'm using random numbers, i don't think it's possible to get the results that i originally intended, even though it doesnt even out correctly out of the 100 echo's, it's still only doing 25%? i dunno, this is what i ended up going with. <?php $loop2 = 1; $act3c = 0; $act4c = 0; $randompercent = rand(12, 49); //chance of battle while($loop2 <= 100){ if(rand(1, 100) <= $randompercent){ echo "looping $loop2 - Battle <br>"; $act3c++; }else{ echo "looping $loop2 - Nothing <br>"; $act4c++; } $loop2++; } echo "total battles = $act3c | total nothing = $act4c | true battle percent $randompercent <p>"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.