Jump to content

Recommended Posts

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

<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.

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.

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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.