【问题标题】:custom php random function自定义php随机函数
【发布时间】:2013-12-03 06:26:24
【问题描述】:

任何 php 随机函数,它给出指定的随机数数量并在定义的边界内。

例如来自1 to 5000的数字

我需要 5 个随机数。

我知道有一个函数可以用于数组索引array_rand

我需要类似的数字和边界定义。至少定义了上限。

【问题讨论】:

  • 夫妇range()array_rand()
  • @ShankarDamodaran 坏主意 - 因为如果我们只想要几个有边界的数字 1..2000000000 - 我们需要使用 4 x 2Gb 内存。看起来很糟糕

标签: php random


【解决方案1】:

这里我用 mt_rand 代替了 rand(快得多):

function randomNumbers($min, $max, $count = 1) {
    $randomFunc = function () use ($min, $max) { return mt_rand($min, $max); };
    return array_map($randomFunc, range(1, $count));    
}

【讨论】:

    【解决方案2】:

    为什么不做一个函数呢?

    function randomArray($count, $min, $max){
        $randomArray = array();
        for ($i = 0; $i < $count; $i++) {
            $randomArray[] = rand($min, $max);
        }
        return $randomArray;
    }
    

    你的情况是:

    $randoms = randomArray(5, 1, 5000);
    

    【讨论】:

      【解决方案3】:

      你可以试试这个

       <?php
          function random_generator($digits)
            {
                  srand((double) microtime() * 10000000);
                  //Array of alphabets or numeric ,you can define
                  $input = array ("0", "1", "2", "3","4");
      
                  $random_generator="";// Initialize the string to store random numbers
                  for($i=1;$i<$digits+1;$i++)
                  { // Loop the number of times of required digits
      
                      if(rand(1,2) == 1){// to decide the digit should be numeric or alphabet
                      // Add one random alphabet
                      $rand_index = array_rand($input);
                      $random_generator .=$input[$rand_index]; // One char is added
                  }
                  else
                  {
                      // Add one numeric digit between 1 and 10
                      $random_generator .=rand(1,10); // one number is added
                  } 
                  // end of if else
              } 
              // end of for loop
      
                  return $random_generator;
              }
      
          echo random_generator(5);
      ?>
      

      【讨论】:

        【解决方案4】:

        将返回唯一的随机数,您已将 3 个值传递给函数:Min、Max 和 Count(随机值的个数)

        function rand5($min,$max,$count){
            $num = array();
            for($i=0 ;i<$count;i++){
                if(!in_array(mt_rand($min,$max),$num)) {
                    $num[]=mt_rand($min,$max);
                }
                else {
                    $i--;
                }
            }
        return $num;
        }
        

        希望会有所帮助!

        【讨论】:

          【解决方案5】:
          use of define may be alternative in PHP
          
          <!DOCTYPE html>
          <html>
          <head>
          <style>
          table, th, td {
              border: 1px solid red;
          }
          </style>
          </head>
          <body bgcolor="gold">
          
          <table style="width:25%">
          <?php
          // code by Bhupinder Deol
          define('MIN', 1);
          define('MAX', 49);
          define('ROWS', 10);
          define('NUMS', 6);
          
          for($y=0;$y <ROWS;$y++){  // for loop started  for $y
                       for($x=0;$x<NUMS;$x++) {
                                                 $a[$x]=rand(MIN,MAX);
                                              }
                                                  asort($a);
                                                  $a=array_unique($a);
                                      if (count($a) == NUMS)
          
                                            {
                                               // print_r($a);
                                                                  echo "<tr>";
                                                              foreach ($a as $value) {
          
                                                                                         echo "<td>$value</td>";
          
          
                                                                                         //   echo  $value."  ";
                                                                                       }
          
                                                         echo "</tr>";
          
                             `enter code here`                                   $x=0;
                                               }
                                                        else
                                                             {
                                                                 --$y;
                                                                  $x=0;
                                                              }
                                    } // for loop ended for $y
          ?>
          </table>
          <h1> Total lottery lines  created =  <?php echo ROWS."  for ".  NUMS. "/" .MAX; ?>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-09-08
            • 1970-01-01
            • 2017-09-23
            • 1970-01-01
            • 2014-01-23
            • 2014-05-29
            • 2018-01-15
            • 1970-01-01
            相关资源
            最近更新 更多