Generate an N-digit random number

后端 未结 7 1555
清酒与你
清酒与你 2020-12-05 09:49

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 <

相关标签:
7条回答
  • 2020-12-05 10:37

    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
    ?>
    
    0 讨论(0)
提交回复
热议问题