Yesideez Posted November 5, 2008 Share Posted November 5, 2008 The "Weighted Rand" topic reminded me of something I've needed but never found. I'm looking for a routine that will pick random numbers out between X and Y. Here's the snag: no number may be called more than once and the routine needs to exit when all numbers between X and Y (inclusive) have been chosen. I need this for both a basic number range and for a 2D array. Any help appreciated Link to comment https://forums.phpfreaks.com/topic/131542-random-numbers/ Share on other sites More sharing options...
RichardRotterdam Posted November 5, 2008 Share Posted November 5, 2008 maybe you could just use shuffle to randomize an array list and then call them from the start till end Link to comment https://forums.phpfreaks.com/topic/131542-random-numbers/#findComment-683201 Share on other sites More sharing options...
Yesideez Posted November 5, 2008 Author Share Posted November 5, 2008 All that does is shuffle the order up assigning new keys so picking a random number could still pick the same one more than once. Link to comment https://forums.phpfreaks.com/topic/131542-random-numbers/#findComment-683207 Share on other sites More sharing options...
flyhoney Posted November 5, 2008 Share Posted November 5, 2008 You could keep up with the so-far-picked numbers with a static array. Link to comment https://forums.phpfreaks.com/topic/131542-random-numbers/#findComment-683210 Share on other sites More sharing options...
Yesideez Posted November 5, 2008 Author Share Posted November 5, 2008 That would be good for a small range of numbers but when we're looking at a very large range the routine could pause a little too long towards the end. I've seen what I need in action elsewhere (handy to fade out pictures for example using this random selection routine via 2D arrays) but I can't for the life of me figure out how to code it! Link to comment https://forums.phpfreaks.com/topic/131542-random-numbers/#findComment-683219 Share on other sites More sharing options...
Michdd Posted November 5, 2008 Share Posted November 5, 2008 It depends on how you're planning on using this. One possible way is to use rand(); and use a MySql database to save the created values, then check if they've been made before. Rand works like this: rand(5, 15); That chooses a random number between 5 and 15. Link to comment https://forums.phpfreaks.com/topic/131542-random-numbers/#findComment-683224 Share on other sites More sharing options...
corbin Posted November 5, 2008 Share Posted November 5, 2008 What about.... $min = 0; $max = 99; $numbers = array_fill($min, $max); shuffle($numbers); $c = $max - $min; for($i = 0; $i < $c; ++$i) { echo $numbers[$i]; } Or, if you wanted it to carry over pages, you could just store the offset. Link to comment https://forums.phpfreaks.com/topic/131542-random-numbers/#findComment-683228 Share on other sites More sharing options...
Yesideez Posted November 5, 2008 Author Share Posted November 5, 2008 Thanks, it's closer I need the routine in a loop. For example, if I have 100 numbers (0-99) I need the routine to run 100 times and at the end all numbers to be selected (forgot to mention that in my OP. Link to comment https://forums.phpfreaks.com/topic/131542-random-numbers/#findComment-683232 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.