PHP rand() exclude certain numbers

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

I have this:

<?php  $n = rand(1,1600); echo $n ?> 

I want to exclude from random numbers let's say 234, 1578 ,763 , 1274 and other numbers. How would I do that?

回答1:

<?php  while( in_array( ($n = rand(1,1600)), array(234, 1578 ,763 , 1274) ) ); 


回答2:

Try like this

do {        $n = rand(1,1600);  } while(in_array($n, array(234, 1578 ,763 , 1274 )); echo $n; 


回答3:

Check if the number is one that you don't want, if it is get a new random number.

function getRandomNumber() {     do {         $n = mt_rand(1,1600);     } while(in_array($n, array(234,1578, 763, 1274)));      return $n; } 


回答4:

If you don't have too many numbers to exclude, it is easier and faster to just retry if you find an unwanted number:

$n = 0; while (in_array($n, array(0, 234, 1578 ,763 , 1274))) {   $n = rand(1,1600);  }  echo $n; 


回答5:

Or avoid making loops with random (possibly infinite) running time:

/**  * Returns a random integer between $min and $max (inclusive) and  * excludes integers in $exarr, returns false if no such number  * exists.  *   * $exarr is assumed to be sorted in increasing order and each  * element should be unique.  */ function random_exclude($min, $max, $exarr = array()) {      if ($max - count($exarr) < $min) {         return false;     }      // $pos is the position that the random number will take     // of all allowed positions     $pos = rand(0, $max - $min - count($exarr));      // $num being the random number     $num = $min;      // while $pos > 0, step to the next position     // and decrease if the next position is available     for ($i = 0; $i < count($exarr); $i += 1) {          // if $num is on an excluded position, skip it         if ($num == $exarr[$i]) {             $num += 1;             continue;         }          $dif = $exarr[$i] - $num;          // if the position is after the next excluded number,         // go to the next excluded number         if ($pos >= $dif) {             $num += $dif;              // -1 because we're now at an excluded position             $pos -= $dif - 1;         } else {             // otherwise, return the free position             return $num + $pos;         }     }      // return the number plus the open positions we still had to go     return $num + $pos; } 

This function chooses a random position and walks the exclusion array to find the free position. It's running time depends on the amount of numbers to exclude. If you want to exclude certain ranges, you may want to adapt the algorithm to take this into account.



回答6:

Another solution for this could be as follows:

function random_number($min, $max, $exclude) {       $number = rand($min, $max);    if(in_array($number, $exlude))   {       random_number($min, $max, $exlude);   } else {       return $number;   } }  $number = random_number(1,10, [2,5,6]); 


回答7:

Always use cryptographically strong algorithms for generating random numbers:

/**  * @param int   $from     From number  * @param int   $to       To number  * @param array $excluded Additionally exclude numbers  * @return int  */ function randomNumber($from, $to, array $excluded = []) {     $func = function_exists('random_int') ? 'random_int' : 'mt_rand';      do {         $number = $func($from, $to);     } while (in_array($number, $excluded, true));      return $number; }  var_dump(randomNumber(1, 100)); var_dump(randomNumber(1, 10, [5, 6, 7, 8])); var_dump(randomNumber(1, 100, range(10, 90))); 


回答8:

You could create an array with valid numbers.

Then, your random number generation should return the index into that array.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!