Page 1 of 1

Random Numbers

Posted: Thu Jan 24, 2008 5:48 pm
by ChessclubFriend
I'm trying to generate 5 random numbers from a set of numbers. Is there an easy way to do this? Thanks in advance!

EDIT:
For instance, if I had the numbers 1,3,5,7,9,11, .., I want to randomly pick from only those numbers, nothing in between :)

Re: Random Numbers

Posted: Thu Jan 24, 2008 7:13 pm
by thinsoldier
http://php.net/array_rand

Code: Select all

 
$options = '1,3,5,7,9,11,13,15,17,19';
$options = explode(',' , $options);
 
$rand_keys = array_rand($options , 5);
echo $options [$rand_keys[0]] . "\n";
echo $options [$rand_keys[1]] . "\n";
echo $options [$rand_keys[2]] . "\n";
echo $options [$rand_keys[3]] . "\n";
echo $options [$rand_keys[4]] . "\n";
 

Re: Random Numbers

Posted: Thu Jan 24, 2008 7:51 pm
by s.dot
Just put it into an array and shuffle it.

Code: Select all

$chars = array(1, 3, 5, 11, 79);
shuffle($chars);
 
echo $chars[0];  //or $chars[1] or 2 or 3 or 4.. you get the idea ;p

Re: Random Numbers

Posted: Thu Jan 24, 2008 8:10 pm
by thinsoldier
Touché