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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

?>

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.