3

I have an array of numbers and I need to make a random number then check if that number is not in the array, If it is make another random number and then check that number to see if its in the array.

example.

array = 1, 2, 4, 5, 7
rand(1, 7) = 3 or 6

if rand(1, 7) = 1, 2, 4, 5 or 7 it would run again until it returned 3 or 6.

anyone know how to do this in php?

scrowler
  • 24,273
  • 9
  • 60
  • 92
user3646863
  • 43
  • 1
  • 3

2 Answers2

5

You may simply generate a random number and check if it is already in the array

$in = [1, 4, 7, 9];
do {
    $rand = rand($min, $max);
} while(in_array($rand, $in));

echo $rand, ' is random, but not in the input array';

The above code generates a random integer that is insides the bounds defined in $min and $max. If the value already exists inside the array a new random value is fetched and compared to the input array.

Note: While the above is the minimal working code you may create an endless loop if your input array contains all possible values(Thanks @Action Dan). You didn't state in your question whether this is possible or not. If it is possible you need to work around this. Either by limiting the the maximum tries or validating the input array before and issuing an error message or increasing the 2nd parameter of rand.

Example(validating, only recommended for smaller arrays):

$in = [1,2,3,4,5];
$min = 1;
$max = 5;
if(range($min, $max) === $in) {
    echo 'No possible value in range';
    exit;
}
// code from above
Community
  • 1
  • 1
Rangad
  • 2,110
  • 23
  • 29
  • 1
    The only suggestion here is that there is a potential depending on the input for this to loop forever. I suggest a counter that iterates and if that counter gets to be (say) half the length of the input array, then switch to simply find the minimum available number which is not in the array or some other fall back. – Action Dan Jul 03 '14 at 00:25
  • Thanks @ActionDan added a node about that and a small catch in case of a small array. Feel free to change that if you have a better suggestion. – Rangad Jul 03 '14 at 00:40
  • @Rangad I always worry of the potential for such a loop to run forever or at least way too long. In your example code, it is possible that rand() consistently returns the numbers 1,4,7 and 9 - it is not likely but possible. So thinking more about it I have a new suggestion to make an array of all possibilities that are valid first, and then to make one random selection from it. Then you get certainty. I guess it depends on the application whether it is worth doing that. – Action Dan Jul 03 '14 at 04:24
1
<?php

    $numbers = array(1,2,3,4,5,6,7);

    $rand = rand(1,10);

    if (in_array($rand, $numbers))
    {
      echo "Match found";
    }
    else
    {
      echo "Match not found";
    }
    echo "<br />" . $rand;
?> 

In this sample I had $rand intentionally have more than 7, just to make sure the code is working well and "Match not found" is printed..

in_array() function checks if the value of $rand exists in $numbers if true.. prints "Match found" if not prints "Match not found".

Hope this helps..

Itachi Sama
  • 886
  • 1
  • 6
  • 18
  • 1
    **Welcome to StackOverflow** - answers should provide some background references and/or descriptions of why things work and how. Please provide a bit more information with your answer. – scrowler Jul 03 '14 at 00:31