Generate an N-digit random number

Saleh picture Saleh · Feb 14, 2011 · Viewed 96.5k times · Source

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?

Answer

Marco picture Marco · Feb 14, 2011

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