Smudly Posted April 19, 2011 Share Posted April 19, 2011 I am trying to create a script that will choose from 3 different Variables. This is much like a Chance game. Variable A has a 20% chance to be chosen Variable A has a 35% chance to be chosen Variable A has a 45% chance to be chosen How would I go about setting each of these variables chance to be chosen? Quote Link to comment https://forums.phpfreaks.com/topic/234182-giving-one-variable-more-weight-over-another-working-with-chance-percents/ Share on other sites More sharing options...
Skewled Posted April 20, 2011 Share Posted April 20, 2011 http://php.net/manual/en/function.mt-rand.php Something like this: in an IF/ELSE <?php $ournumber = mt_rand(1,100); // Random number between 1 and 100 if ($ournumber == 20) { // a 20% chance } elseif ($ournumber == 35) { // a 35% chance } elseif ($ournumber == 45) { // a 45% chance } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234182-giving-one-variable-more-weight-over-another-working-with-chance-percents/#findComment-1203772 Share on other sites More sharing options...
mens Posted April 20, 2011 Share Posted April 20, 2011 Logic, mostly. Using a little math, you make it appear as a fraction. if (rand(0,4) == 0) { ... 20% ... } // 1/5 ~ 20% / 100% Note: the above example is, still, only 1%. Quote Link to comment https://forums.phpfreaks.com/topic/234182-giving-one-variable-more-weight-over-another-working-with-chance-percents/#findComment-1203775 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.