php 生成 N 位随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4990752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:28:24  来源:igfitidea点击:

Generate an N-digit random number

phpsecurityrandommcrypt

提问by Saleh

I want to generate a 6 digit random number using the PHP mt_rand()function.

我想使用 PHPmt_rand()函数生成一个 6 位随机数。

I know the PHP mt_rand()function only takes 2 parameters: a minimumand a maximumvalue.

我知道 PHPmt_rand()函数只需要 2 个参数:最小值最大值

How can I do that?

我怎样才能做到这一点?

回答by Marco

Something like this ?

像这样的东西?

<?php 
$a = mt_rand(100000,999999); 
?>

Or this, then the first digit can be 0 in first example can it only be 1 to 9

或者这个,那么第一个例子中的第一个数字可以是0它只能是1到9

for ($i = 0; $i<6; $i++) 
{
    $a .= mt_rand(0,9);
}

回答by rashedcs

You can use the following code.

您可以使用以下代码。

  <?php 
  $num = mt_rand(100000,999999); 
  printf("%d", $num);
  ?>

Here mt_rand(min,max);
min = Specifies the lowest number to be returned.
max = Specifies the highest number to be returned.

这里 mt_rand(min,max);
min = 指定要返回的最小数字。
max = 指定要返回的最高数字。

`

`

回答by Aditya P Bhatt

Examples:

例子:

print rand() . "<br>"; 
//generates and prints a random number
print rand(10, 30); 
//generates and prints a random number between 10 and 30 (10 and 30 ARE included)
print rand(1, 1000000); 
//generates and prints a random number between on and one million

More Details

更多细节

回答by Richelly Italo

    <?php
//If you wanna generate only numbers with min and max length:
        function intCodeRandom($length = 8)
        {
          $intMin = (10 ** $length) / 10; // 100...
          $intMax = (10 ** $length) - 1;  // 999...

          $codeRandom = mt_rand($intMin, $intMax);

          return $codeRandom;
        }
?>

回答by Andre Pastore

If the first member nunmber can be zero, then you need format it to fill it with zeroes, if necessary.

如果第一个成员编号可以为零,则需要对其进行格式化以在必要时用零填充它。

<?php 
$number = mt_rand(10000,999999);
printf("[%06s]\n",$number); // zero-padding works on strings too
?>

Or, if it can be form zero, you can do that, to:

或者,如果它可以是零形式,您可以这样做,以:

<?php 
$number = mt_rand(0,999999);
printf("[%06s]\n",$number); // zero-padding works on strings too
?>

回答by Kenan Deniz

as far as understood, it should be like that;

据了解,应该是这样;

function rand6($min,$max){
    $num = array();

    for($i=0 ;i<6;i++){
    $num[]=mt_rand($max,$min);

    }
return $num;
}

回答by xzyfer

You can do it inline like this:

你可以像这样内联:

$randomNumbersArray = array_map(function() {
    return mt_rand(); 
}, range(1,6));

Or the simpliar way, with a function:

或者简单的方法,用一个函数:

$randomNumbersArray = giveMeRandNumber(6);

function giveMeRandNumber($count)
{
    $array = array();
    for($i = 0; $i <= $count; $i++) {
        $array[] = mt_rand(); 
    }
}

These will produce an array like this:

这些将产生一个这样的数组:

Array
(
    [0] => 1410367617
    [1] => 1410334565
    [2] => 97974531
    [3] => 2076286
    [4] => 1789434517
    [5] => 897532070
)