Generate an N-digit random number

2019-01-17 10:09发布

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?

7条回答
Emotional °昔
2楼-- · 2019-01-17 11:12
    <?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;
        }
?>
查看更多
登录 后发表回答