-1

How would I generate a random number from a range of numbers between 1 and 10 while excluding an array of numbers e.g. 4,5,6.

$exclude = array(4,5,6);

The following code allows to generate random numbers within a range however only for a single number and not an array of numbers

function randnumber() {
    do {
    $numb = rand(1,10);
} while ($varr == 4);
return $numb;
}
Billy
  • 2,823
  • 3
  • 16
  • 33
  • 4
    I can think of at least 20 ways off the top of my head. Do you want it to be fast? Do you care how evenly distributed it is? How much memory can it use? Are you going to call it once or thousands of times? – kainaw Jun 03 '16 at 15:43
  • I'm just a beginner so I'm not very fussed. Performance is not an issue and it does not need to be evenly distributed. – Billy Jun 03 '16 at 15:48
  • 1
    Can you share what you've tried? Please read [how to ask questions on StackOverflow](http://stackoverflow.com/help/how-to-ask) – Jay Blanchard Jun 03 '16 at 15:52
  • Actually, it looks like this one is better: http://stackoverflow.com/questions/2698265/how-to-get-a-random-value-from-1n-but-excluding-several-specific-values-in-php – Don't Panic Jun 03 '16 at 16:14
  • @Don'tPanic Thanks. – Billy Jun 03 '16 at 16:21

5 Answers5

2

Create a loop that iterates until a generated random number using rand function is not in array. If the generated number is found in array, again another random number is generated.

do {   
    $number = rand(1,10);
} while(in_array($number, array(4,5,6)));
echo $number;

or

while(in_array(($number = rand(1,10)), array(4,5,6)));
echo $number;

You can use it like a function too:

<?php

function randomNo($min,$max,$arr) {
    while(in_array(($number = rand($min,$max)), $arr));
    return $number;
}

echo randomNo(1,10,array(4,5,6));

The above function, does the same process, in addition, you can reuse the code. It gets minimum and maximum number and the array of values to exclude.


Finally,

without loop, but with recursive function. The function generates a random number and returns if it is not found in the exclude array:

function randomExclude($min, $max, $exclude = array()) {
    $number = rand($min, $max);
    return in_array($number, $exclude) ? randomExclude($min, $max, $exclude) : $number;
}

echo randomExclude(1,10,array(4,5,6));
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
2
<?php
$exclude = array(4,5,6); // The integers to excluded
do
{
   $x = rand(1, 10); // Generate a random integer between 1 and 10
}while(in_array($x, $exclude)); // If we hit something to exclude, try again
echo $x; // A random integer not excluded
?>

It would be wise to check if not all inputs are excluded to avoid infinite loops

Moussa Khalil
  • 635
  • 5
  • 12
1

You can simply do this, using array functions like this:

function my_rand($min, $max, array $exclude = array())
{
     $range = array_diff(range($min, $max), $exclude);
     array_shuffle($range);

     return array_shift($range);
}
maximkou
  • 5,252
  • 1
  • 20
  • 41
  • 1
    Why should the OP "simply do this"? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Jun 03 '16 at 15:51
  • @JayBlanchard, I think, in this case, function logic very simple and trransparent. I do not see reason, that comment this code. If you think different, please edit post and add explanation. – maximkou Jun 03 '16 at 15:54
  • Just as I said before, future visitors to SO may be lost on what you've said, perhaps they're newbies who know nothing about coding and your answer, as it stands, would not be helpful to them. Imagine the first few days you were coding - would your answer have been as transparent to you? I'll leave it to you to edit your own post. – Jay Blanchard Jun 03 '16 at 16:33
0

Some time ago I also wanted to become rid of these nasty little while loops. Reduced to fit your version of the problem, my approach would be to:

  • first generate a random number which in range (10 - 3) to indicate the position of the number to be generated in the hypothetical list of numbers in the desired range excluding {4,5,6}
  • second increment this value by the lower range (1)
  • third increment by 1 for every number in the set {4,5,6} it is equal to or greather (in order from the smallest to the highest number in that set)

So, all in all I'd basically stretch and blurr the hypothetical set of numbers the generated random value can be to fit into the possible outcomes.

Androbin
  • 991
  • 10
  • 27
-1
$total = range(0,10);
$exclude = range(4,6);
$include = array_diff($total, $exclude);
print_r (array_rand($include));
Oldskool
  • 34,211
  • 7
  • 53
  • 66
Wu Hao
  • 1
  • 1