Third_Degree Posted July 25, 2008 Share Posted July 25, 2008 I'm having a problem generating a list of unique random numbers. I am trying to populate an array with random numbers and no repeats, but the only way i can figure to do this is generate a single number and check if it's in the array already, then insert it if it is, and create a new random number, and redo the check, which could lead to infinite loops. Is there a more efficient way to do this. BOTTOM LINE:I need a list of unique random numbers... Quote Link to comment Share on other sites More sharing options...
jonsjava Posted July 25, 2008 Share Posted July 25, 2008 example solution: <?php $rand = array(); $count = 1; while ($count < 1001){ $rand[] = $count; $count++; } shuffle($rand); print_r($rand); ?> Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 25, 2008 Share Posted July 25, 2008 could also just do <?php $rand = (range(1, 200); shuffle($rand); ?> Quote Link to comment Share on other sites More sharing options...
Third_Degree Posted July 25, 2008 Author Share Posted July 25, 2008 Thank you very much jonsjava. and mbeals... Quote Link to comment Share on other sites More sharing options...
jonsjava Posted July 25, 2008 Share Posted July 25, 2008 could also just do <?php $rand = (range(1, 200); shuffle($rand); ?> why do I always forget range? I hardly have a need for it, I guess. Thanks for the reminder! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.