生成一个N位随机数

发布于 2024-10-17 04:53:14 字数 153 浏览 2 评论 0原文

我想使用 PHP mt_rand() 函数生成 6 位随机数。

我知道 PHP mt_rand() 函数只需要 2 个参数:一个最小值和一个最大值

我怎样才能做到这一点?

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

I know the PHP mt_rand() function only takes 2 parameters: a minimum and a maximum value.

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

傲世九天 2024-10-24 04:53:14

像这样的东西?

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

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

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

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

for ($i = 0; $i<6; $i++) 
{
    $a .= mt_rand(0,9);
}
倒带 2024-10-24 04:53:14
    <?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;
        }
?>
    <?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;
        }
?>
只为一人 2024-10-24 04:53:14

您可以使用以下代码。

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

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

`

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.

`

殤城〤 2024-10-24 04:53:14

示例:

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

更多详细信息

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

ペ泪落弦音 2024-10-24 04:53:14

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

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

或者,如果它可以为零,您可以这样做:

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

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
?>
橘和柠 2024-10-24 04:53:14

据理解,应该是这样;

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

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

    }
return $num;
}

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;
}
你是我的挚爱i 2024-10-24 04:53:14

您可以像这样内联执行此操作:

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

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

$randomNumbersArray = giveMeRandNumber(6);

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

这些将生成如下数组:

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

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
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文