自定义php随机函数


custom php random function

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

例如1 to 5000 中的数字

我需要5个随机数。

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

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

这里我用了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));    
}

为什么不创建一个函数?

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

你的情况是:

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

你可以试试这个

 <?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);
?>

将返回唯一的随机数,您必须将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;
}

希望会有所帮助!

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; ?>