Michan Posted October 25, 2007 Share Posted October 25, 2007 Okay, so I know how to create a random number from min and max values, but how do I create a random number from a pre-defined set of numbers? Say for instance, I have the numbers 1, 3, and 7 - and I want to select a random number of those three, how would I get about doing it? Thanks in advance. - Mi Quote Link to comment https://forums.phpfreaks.com/topic/74665-solved-random-selection-from-pre-defined-set/ Share on other sites More sharing options...
phpQuestioner Posted October 25, 2007 Share Posted October 25, 2007 try this: <?php $mypicks = array("1","3","7"); $pick = rand($mypicks); echo "$pick"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/74665-solved-random-selection-from-pre-defined-set/#findComment-377429 Share on other sites More sharing options...
kratsg Posted October 25, 2007 Share Posted October 25, 2007 Well, a simpler idea is the following. $something = array("add some array values in here"); shuffle($something); echo $something[0]; Quote Link to comment https://forums.phpfreaks.com/topic/74665-solved-random-selection-from-pre-defined-set/#findComment-377433 Share on other sites More sharing options...
Michan Posted October 25, 2007 Author Share Posted October 25, 2007 Well, a simpler idea is the following. $something = array("add some array values in here"); shuffle($something); echo $something[0]; Thank you very much, this works perfectly! And thanks phpQuestioner, but the rand() requires min and max values, so it didn't work Quote Link to comment https://forums.phpfreaks.com/topic/74665-solved-random-selection-from-pre-defined-set/#findComment-377434 Share on other sites More sharing options...
MadTechie Posted October 25, 2007 Share Posted October 25, 2007 FYI: you could also do this (if you want to use an array) <?php $mypic = array("1","3","7"); $rand = array_rand($mypic); echo $mypic[$rand]; ?> EDIT: also rand does NOT Require a min & max, they are optional but in phpQuestioner, code it should be rand(0, count($mypicks)); Quote Link to comment https://forums.phpfreaks.com/topic/74665-solved-random-selection-from-pre-defined-set/#findComment-377438 Share on other sites More sharing options...
phpQuestioner Posted October 25, 2007 Share Posted October 25, 2007 Thanks MadTechie; I thought that would work; But I did leave out some code - did not test it - lol. Here is also another way to use rand() - I use this for random text and images: <?php $mypicks[] = "1"; $mypicks[] = "3"; $mypicks[] = "7"; srand ((double) microtime() * 1000000); $picknums = rand(0,count($mypicks)-1); $picks = $mypicks[$picknums]; echo "$picks"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/74665-solved-random-selection-from-pre-defined-set/#findComment-377444 Share on other sites More sharing options...
MadTechie Posted October 25, 2007 Share Posted October 25, 2007 Side Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically. Quote Link to comment https://forums.phpfreaks.com/topic/74665-solved-random-selection-from-pre-defined-set/#findComment-377446 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.