Jump to content

how to generat random 10 digit numbers


ShivaGupta

Recommended Posts

I don't know if this question deserves an answer, but just googling what you wrote here, got me this as a first response:

http://elementdesignllc.com/2011/06/generate-random-10-digit-number-in-php/

 

So please don't be that lazy and try using google from time to time.

It depends on what you mean by 10 digit. Do you consider 0000000001 the first valid number or is it 1000000000? Either way mt_rand() is what you want.

 

mt_rand()

 

 

 

$randNo = mt_ranf(1000000000, 9999999999);

 

or

 

 

$randNo = mt_ranf(1, 9999999999);

I cannot get mt_rand() to produce a number larger than 1,410,065,401

$max = 0;
for ($i=0; $i<1000000; $i++) {
    $max = max($max, mt_rand(1000000001,9999999999));
}
echo $max;    //--> 1410065401

so you may need something like

for ($i=0, $num=''; $i<10; $i++) {
    $num .= rand(1,9);
}
echo $num;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.