PHP generates a certain number of non repeating random numbers

From , 2 Years ago, written in PHP, viewed 84 times.
URL https://pastebin.vip/view/aa6b7ad9
  1. <?php
  2. /*
  3. * array unique_rand( int $min, int $max, int $num )
  4. * 生成一定数量的不重复随机数
  5. * $min 和 $max: 指定随机数的范围
  6. * $num: 指定生成数量
  7. */
  8. function unique_rand($min, $max, $num) {
  9.     $count = 0;
  10.     $return = array();
  11.     while ($count < $num) {
  12.         $return[] = mt_rand($min, $max);
  13.         $return = array_flip(array_flip($return));
  14.         $count = count($return);
  15.     }
  16.     shuffle($return);
  17.     return $return;
  18. }
  19.  
  20. $arr = unique_rand(1, 25, 16);
  21. sort($arr);
  22.  
  23. $result = '';
  24. for($i=0; $i < count($arr);$i++)
  25. {
  26.         $result .= $arr[$i].',';
  27. }
  28. $result = substr($result, 0, -1);
  29. echo $result;
  30. ?>
  31.  

Reply to "PHP generates a certain number of non repeating random numbers"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website