Forum Moderators: coopster
I have the need to get results from a database in a random order ie:
select * from tbllisting order by random(id);
ofcourse that doesn't work so I figure I have to do it at a PHP level but I can't figure out how to do it easliy and efficiantly.
what I need to do is generate a sequence of random numbers between 0 and x x amount of times without repeating any number.
I first though of doing something like:
<?
$x=10;
$count=0;
for($i=0;$i<=$x;$i++)
$order[$i]=-1;
while( $count<=$x ) {
$tmp=rand(0,$x);
if( -1==$order[$tmp] ) {
$order[$tmp]=$count;
$count++;
}
}
?>
You would set $x to the highest index.
The issue is performance. The probability of hitting 0 or $x is much less than numbers in the center so there ends up being lots of wasted cycles and time.
Any ideas on a better solution?
daisho.