简体   繁体   English

在PHP中生成一个随机数的数组,该数组不接近X先前的元素

[英]Generate an array in PHP of random number not close to the X previous element

I want to generate in PHP an array of random numbers, but each number should not be the same as any of the X (for example 2 ) numbers bofore it and not even close to any of them by a define range (for example 5). 我想在PHP中生成一个随机数数组,但是每个数字都不应与其之前的任何X(例如2)数字相同,并且甚至不接近其定义范围内的任何数字(例如5) 。

So for example: 因此,例如:

  • I need numbers between 1 and 100 我需要1到100之间的数字
  • i've set my "range" to 5 我将“范围”设置为5
  • the first two generated number are 20 and 50. 前两个生成的数字是20和50。
  • the third number will be a random number between 1 and 100, excluding all the numbers between 15 and 25, and between 45 and 55. 第三个数字将是1到100之间的随机数,不包括15到25之间以及45到55之间的所有数字。

I can't figure out a function to achieve it. 我不知道要实现它的功能。 Ideally I want to call something like this: 理想情况下,我想这样称呼:

getRandomNumbers( $min, $max, $previous, $range); getRandomNumbers($ min,$ max,$ previous,$ range);

where $previous is the number of previous elements to take in consideration when generating the next one and $range is the "proximity" to those number where I don't want the next number to be. 其中$ previous是在生成下一个元素时要考虑的上一个元素的数量,而$ range是那些我不希望下一个数字出现的数字的“接近度”。

I hope I explained in a decent way my request. 我希望我能以体面的方式解释我的要求。 :) Please, add a comment if you have any question about it. :)如果您有任何疑问,请添加评论。

I just came up with this: 我只是想出了这个:

function getRandomNumbers($min, $max, $previous, $range) {
    static $generated = array();
    $chunk = array_slice($generated, -$previous);

    // Added this infinite loop check to save you some headache.
    if (((($max - $min + 1) / (($range * 2) + 1)) + 1) <= $previous) {
        die("Values set have the potential of running into an infinite loop. Min: $min, Max: $max, Previous: $previous, Range: $range");
    }

    while(true) {
        $number = rand($min, $max);
        $found = true;
        foreach ($chunk as $value) {
            if (in_array($number, range($value-$range, $value+$range))) {
                $found = false;
            }
        }
        if ($found) {
            $generated[] = $number;
            return $number;
        }
    }
}

Test it using this: 使用以下命令进行测试:

for ($i = 1; $i < 25; $i++) {
    echo getRandomNumbers(1, 100, 5, 5) . "<br />";
}

PHPFiddle Link: http://phpfiddle.org/main/code/51ke-4qzs PHPFiddle链接: http ://phpfiddle.org/main/code/51ke-4qzs

Edit: Added a check to prevent a possible infinite loop. 编辑:添加了检查以防止可能的无限循环。 For example: if you set the following values: 例如:如果您设置以下值:

$min = 1;
$max = 100;
$previous = 5;
$range = 12;
echo getRandomNumbers($min, $max, $previous, $range);

Then let's say, in a really unfortunate situation it would generate 13, 38, 63 and 88. So the 5th number cannot be anything between 1 and 25, 26 and 50, 51 and 75, 76 and 100. So it would result in an infinite loop. 然后说,在非常不幸的情况下,它将生成13、38、63和88。因此,第5个数字不能是1到25、26和50、51和7​​5、76和100之间的任何值。无限循环。 I've updated the PHPFiddle link as well. 我也更新了PHPFiddle链接。

getRandomNumbers( $previous, $range ) {
  //I'm assuming that previous will be an array of your previous X that you don't want to be close to
  $num = getRandomNumber() //However you are doing this now
  foreach( $previous as $key => $value ) {
    if ( ( $value - $range ) > $num && ( $value + $range ) < $num ) {
      return getRandomNumbers($previous, $range);
    }
  }
  //You need to also replace a value in previous
  return num;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM