SirChick Posted October 20, 2007 Share Posted October 20, 2007 I want to create a random where by it knows the % chances of each then picks oen of the 5 by that % chance... so all can still happen of course. Can this be done? Quote Link to comment https://forums.phpfreaks.com/topic/74097-rand-of-5-options-using-chance/ Share on other sites More sharing options...
mschrank99 Posted October 20, 2007 Share Posted October 20, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/74097-rand-of-5-options-using-chance/#findComment-374122 Share on other sites More sharing options...
SirChick Posted October 20, 2007 Author Share Posted October 20, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/74097-rand-of-5-options-using-chance/#findComment-374146 Share on other sites More sharing options...
Barand Posted October 20, 2007 Share Posted October 20, 2007 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/74097-rand-of-5-options-using-chance/#findComment-374162 Share on other sites More sharing options...
SirChick Posted October 20, 2007 Author Share Posted October 20, 2007 Thanks could you just explain what shuffle($a); does? and why its only on $a ? Quote Link to comment https://forums.phpfreaks.com/topic/74097-rand-of-5-options-using-chance/#findComment-374163 Share on other sites More sharing options...
Barand Posted October 20, 2007 Share Posted October 20, 2007 shuffle() shuffles the array. Only $a because I just merged them all into array $a Quote Link to comment https://forums.phpfreaks.com/topic/74097-rand-of-5-options-using-chance/#findComment-374165 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.