php php随机x位数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8215979/
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-26 04:10:43  来源:igfitidea点击:

php random x digit number

phprandomnumbers

提问by user1022585

I need to create a random number with x amount of digits.

我需要创建一个具有 x 位数的随机数。

So lets say x is 5, I need a number to be eg. 35562 If x is 3, then it would throw back something like; 463

所以让我们说 x 是 5,我需要一个数字,例如。35562 如果 x 是 3,那么它会抛出类似的东西;463

Could someone show me how this is done?

有人可以告诉我这是如何完成的吗?

回答by Marcus

You can use rand()together with pow()to make this happen:

您可以使用rand()withpow()来实现这一点:

$digits = 3;
echo rand(pow(10, $digits-1), pow(10, $digits)-1);

This will output a number between 100 and 999. This because 10^2 = 100 and 10^3 = 1000 and then you need to subtract it with one to get it in the desired range.

这将输出一个介于 100 和 999 之间的数字。这是因为 10^2 = 100 和 10^3 = 1000,然后您需要将其减去 1 以使其处于所需范围内。

If 005 also is a valid example you'd use the following code to pad it with leading zeros:

如果 005 也是一个有效示例,您可以使用以下代码用前导零填充它:

$digits = 3;
echo str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);

回答by Code Magician

I usually just use RAND()http://php.net/manual/en/function.rand.php

我通常只使用RAND()http://php.net/manual/en/function.rand.php

e.g.

例如

rand ( 10000 , 99999 );

for your 5 digit random number

为您的 5 位随机数

回答by Akshay Khale

Here is a simple solution without any loops or any hassle which will allow you to create random string with characters, numbers or even with special symbols.

这是一个没有任何循环或任何麻烦的简单解决方案,它允许您创建带有字符、数字甚至特殊符号的随机字符串。

$randomNum = substr(str_shuffle("0123456789"), 0, $x);

$randomNum = substr(str_shuffle("0123456789"), 0, $x);

where $xcan be number of digits

哪里$x可以是位数

Eg. substr(str_shuffle("0123456789"), 0, 5);

例如。 substr(str_shuffle("0123456789"), 0, 5);

Results after a couple of executions

几次执行后的结果

98450
79324
23017
04317
26479

You can use the same code to generate random string also, like this

您也可以使用相同的代码来生成随机字符串,就像这样

$randomNum=substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyzABCDEFGHIJKLMNOPQRSTVWXYZ"), 0, $x);

Results with $x = 11

结果与 $x = 11

FgHmqpTR3Ox
O9BsNgcPJDb
1v8Aw5b6H7f
haH40dmAxZf
0EpvHL5lTKr

回答by halfdan

You can use rand($min, $max)for that exact purpose.

您可以将其rand($min, $max)用于该确切目的。

In order to limit the values to values with x digits you can use the following:

为了将值限制为 x 位的值,您可以使用以下命令:

$x = 3; // Amount of digits
$min = pow(10,$x);
$max = pow(10,$x+1)-1);
$value = rand($min, $max);

回答by Blender

Treat your number as a list of digits and just append a random digit each time:

将您的号码视为数字列表,每次只附加一个随机数字:

function n_digit_random($digits) {
  $temp = "";

  for ($i = 0; $i < $digits; $i++) {
    $temp .= rand(0, 9);
  }

  return (int)$temp;
}


Or a purely numerical solution:

或者纯粹的数值解:

function n_digit_random($digits)
  return rand(pow(10, $digits - 1) - 1, pow(10, $digits) - 1);
}

回答by animuson

function random_numbers($digits) {
    $min = pow(10, $digits - 1);
    $max = pow(10, $digits) - 1;
    return mt_rand($min, $max);
}

Tested here.

在这里测试。

回答by Alex

rand(1000, 9999);works more faster than x4 times rand(0,9);

rand(1000, 9999);工作速度比 x4 倍快 rand(0,9);

benchmark:

基准:

rand(1000, 9999)      : 0.147 sec.
rand(0,9)x4 times     : 0.547 sec.

both functions was running in 100000 iterations to make results more explicit

两个函数都在 100000 次迭代中运行,以使结果更加明确

回答by AMB

the simplest way i can think of is using randfunction with str_pad

我能想到的最简单的方法是使用rand函数str_pad

<?php
echo str_pad(rand(0,999), 5, "0", STR_PAD_LEFT);
?>

In above example , it will generate random number in range 0 to 999.

在上面的例子中,它将生成0 到 999范围内的随机数。

And having 5digits.

并且有5位数。

回答by philip mudenyo

function random_number($size = 5)
{
    $random_number='';
    $count=0;
    while ($count < $size ) 
        {
            $random_digit = mt_rand(0, 9);
            $random_number .= $random_digit;
            $count++;
        }
    return $random_number;  
}

回答by Rene Pot

do it with a loop:

用循环来做:

function randomWithLength($length){

    $number = '';
    for ($i = 0; $i < $length; $i++){
        $number .= rand(0,9);
    }

    return (int)$number;

}