Page 1 of 1

generate multiple unique random numbers

Posted: Thu Jun 18, 2009 3:24 pm
by gth759k
Ok, so I'm building a simple ad manager and what I want to do is generate say 3 random numbers between 1 and the number of rows in my mysql table. I know how to get the number of rows in my mysql table fine, I'm just not sure how to get 3 UNIQUE random numbers. Any help would be appreciated. Thanks.

Re: generate multiple unique random numbers

Posted: Thu Jun 18, 2009 3:32 pm
by gth759k
I think I came up with a simple solution.

$rand1 = rand(1, $adnum);
$rand2 = rand(1, $adnum);
$rand3 = rand(1, $adnum);

while ($rand1 == $rand2 || $rand1 == $rand3 || $rand2 == $rand1 || $rand2 == $rand3 || $rand3 == $rand1 || $rand3 == $rand2) {
$rand1 = rand(1, $adnum);
$rand2 = rand(1, $adnum);
$rand3 = rand(1, $adnum);
}

echo $rand1;
echo "<br>";
echo $rand2;
echo "<br>";
echo $rand3;
echo "<br>";