Jump to content

Rand of 5 options using % chance


SirChick

Recommended Posts

Yeah.

 

Create a random number up to a maximum, say 100.

 

If < 25 then you have a 25% chance of doing a

If > 50 then you have a 50% chance of doing b

If > 25 then you have a 75% chance of doing c

 

You see what I'm getting at. Change the numbers and get your brain in a mathematical mood and find the correct numbers for what you want to do. The max random number and the individual numbers will vary depending on how many choices you want and the intended %'s but that is the basic solution.

If < 25 then you have a 25% chance of doing a

If > 50 then you have a 50% chance of doing b

If > 25 then you have a 75% chance of doing c

 

thing is though lets say it is <25... that doesnt represent the chances of b or c :S cos that would suggest 75% chance of b or c divided by 2..but C and B may have different chances completely.

One can only guess at what you want from your explanation, but here's my interpretation

 

A - 15%

B - 10%

C - 30$

D - 20%

E - 25%

 

So if you populate a 100 element array with 15 A's, 10 B's 30 C's etc then using rand(0,99) has those chances of picking that letter.

<?php
$a = $b = $c = $d = $e = array();

$a = array_pad($a, 15, 'A');
$b = array_pad($b, 10, 'B'); 
$c = array_pad($c, 30, 'C'); 
$d = array_pad($d, 20, 'D'); 
$e = array_pad($e, 25, 'E');
$a = array_merge ($a, $b, $c, $d, $e);  // put them all in one bag

shuffle($a);                                     // shake the bag

echo $a[rand(0,99)];                        // pull one out

?>

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.