Forum Moderators: coopster
$nums = array(0,1,2,3,4,5,...,180);
function randNum()
{
global $nums;
$rnd = rand(0, 180);
$num = $nums[$rnd];
if(is_int($num)
{
unset $nums[$rnd];
return $num;
}
else
{
randNum();
}
}
$num = randNum();
Untested, so use at your own risk...
For example:
$i = 0;
$nums = array();
#
for($i; $i <= 180; $i++)
$nums[] = $i;
#
shuffle($nums);
#
for($i = 0; $i < 12; $i++)
echo $nums[$i] . '<br>';
I haven't tested this yet but I'm not sure if a solution like this would be any faster or slower than generating random numbers and checking them against the array of already selected random numbers. My guess is the more random numbers you need to more intensive an algorithm like that would be. The one presented above should be constant regardless of how many numbers you need. Experiment with it a bit and see what you come out with.