Jump to content

how to generat random 10 digit numbers


Go to solution Solved by Barand,

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

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;
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.