timvdalen Posted July 19, 2010 Share Posted July 19, 2010 Hi all. I've got a bit of a though one here. I want to create 5 random numbers (1,45) every time a script is executed. The hard part is that it can only be numbers that don't occur in an array. This is what I currently have: $newqs = array(rand(1,$qcount),rand(1,$qcount),rand(1,$qcount),rand(1,$qcount),rand(1,$qcount)); $okay = false; while($okay == false){ $i = 0; while($i <5){ if(in_array($newqs[$i], $questions)){ $okay = false; $newqs[$i]++; }else{ $okay = true; } $i++; } } $questions is the array with the numbers that have already been chosen. I'll be adding the picked numbers to the array afterwards. I think that the problem with this method is that it's possible that two of the same numbers are picked. Right now, I'm having a bit of trouble with coming up with a better way to check if the number was already picked. Any ideas will be appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/208145-5-random-numbers-that-dont-occur-in-an-array/ Share on other sites More sharing options...
timvdalen Posted July 19, 2010 Author Share Posted July 19, 2010 I realize that this is not really a beginner problem, so I'm going to bump this hoping that some more experienced users see this Quote Link to comment https://forums.phpfreaks.com/topic/208145-5-random-numbers-that-dont-occur-in-an-array/#findComment-1088067 Share on other sites More sharing options...
Wolphie Posted July 19, 2010 Share Posted July 19, 2010 <pre> <?php $numbers = array(rand(1, 45),rand(1, 45),rand(1, 45),rand(1, 45),rand(1, 45)); $questions = array(rand(1, 45),rand(1, 45),rand(1, 45),rand(1, 45),rand(1, 45)); print_r($numbers); print_r($questions); $i = 0; while (true) { if ($i == count($numbers)) { break; } else { if (!in_array($numbers[$i], $questions)){ break; } else { echo 'Found!'; } } $i++; } ?> </pre> Result: Array ( [0] => 5 [1] => 6 [2] => 36 [3] => 41 [4] => 12 ) Array ( [0] => 7 [1] => 5 [2] => 27 [3] => 38 [4] => 37 ) Found! Note: The number 5 occurred in both arrays, therefore "Found!" was displayed on page. Quote Link to comment https://forums.phpfreaks.com/topic/208145-5-random-numbers-that-dont-occur-in-an-array/#findComment-1088072 Share on other sites More sharing options...
timvdalen Posted July 19, 2010 Author Share Posted July 19, 2010 Thanks for your input. I'm quite sure that your code and my code so far do the same (although I guess yours is faster, thanks). The problem I have is what to put at the place where you let the code echo found. I now let it increase, but this will result in an unwanted output in the case that $numbers[$i] equals 45 or two or more of the generated numbers are the same. I could go around this by making it 1 if it's 45 and that's already picked or just replacing $newqs[$i]++ by $newqs[$i]=rand(1,45) but I'm pretty sure these are both pretty slow. Quote Link to comment https://forums.phpfreaks.com/topic/208145-5-random-numbers-that-dont-occur-in-an-array/#findComment-1088094 Share on other sites More sharing options...
salathe Posted July 19, 2010 Share Posted July 19, 2010 Here's an alternative idea: $pool = range(1, 45); $questions = array(5, 9, 10, 12, 33, 39); // Get a list of questions from the pool that have not already been asked $available = array_diff($pool, $questions); // Get five random questions from the list of available ones $random = array_rand(array_flip($available), 5); print_r($random); Quote Link to comment https://forums.phpfreaks.com/topic/208145-5-random-numbers-that-dont-occur-in-an-array/#findComment-1088100 Share on other sites More sharing options...
timvdalen Posted July 19, 2010 Author Share Posted July 19, 2010 Thanks! That's way quicker and way easier. I really gotta read up on all the array_ functions Quote Link to comment https://forums.phpfreaks.com/topic/208145-5-random-numbers-that-dont-occur-in-an-array/#findComment-1088104 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.