phpsecurityrandommcrypt

Generate an N-digit random number


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?


Solution

  • 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);
    }