Jump to content

Giving one Variable more weight over another (working with chance & percents)


Smudly

Recommended Posts

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?

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

}
?>

Archived

This topic is now archived and is closed to further replies.

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