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... Link to comment https://forums.phpfreaks.com/topic/116638-random-number-logic/ 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); ?> Link to comment https://forums.phpfreaks.com/topic/116638-random-number-logic/#findComment-599704 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); ?> Link to comment https://forums.phpfreaks.com/topic/116638-random-number-logic/#findComment-599705 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... Link to comment https://forums.phpfreaks.com/topic/116638-random-number-logic/#findComment-599706 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! Link to comment https://forums.phpfreaks.com/topic/116638-random-number-logic/#findComment-599710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.