Jump to content

[SOLVED] calculating a percent chance


kryppienation

Recommended Posts

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++;


}





?>

 

 

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.