Page 1 of 1

rand() function that changes for each use

Posted: Wed Feb 25, 2009 5:49 am
by Aki
I want to have the possibility to show a few randomely choosen pictures from a folder, and thought I'd use rand() to pick them out. However, I realised that if I wrote

Code: Select all

$pic = rand(0,15);
echo "<img src='{$pic}.png'>
<img src='{$pic}.png'>
<img src='{$pic}.png'>";
I'd have the same picture written three times, as $pic doesn't change between these, only once - when the script loads. Is there a function or another way to have three DIFFERENT, randomely choosen pictures?

Thanks on forehand.

Re: rand() function that changes for each use

Posted: Wed Feb 25, 2009 6:29 am
by onion2k
Just call rand() 3 times.

Re: rand() function that changes for each use

Posted: Wed Feb 25, 2009 7:23 am
by requinix
If they have to be different, you're dealing with such a small set of numbers that

Code: Select all

$numbers = range(0, 15); shuffle($numbers);
should be fine. $numbers[X] will be a random number between zero and fifteen; this means you can pick whatever X you want, it doesn't have to be random.

Code: Select all

echo $numbers[0]; // this will be a random number