Solved

Random number generation

Posted on 2009-05-06
5
399 Views
Last Modified: 2012-05-06
hi,

I want to generate a random number more than  10 digit (like in below code). But return value was always low than  10 digit.
 What is the  error?
 How can I generate random number more than 10 digit? (expect a code sample).
mt_rand(1000000000000, 9999999999999);

Open in new window

0
Comment
Question by:shaf81
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 5

Assisted Solution

by:QEMS
QEMS earned 100 total points
ID: 24313894
Taken from http://www.alt-php-faq.org/local/28/ 

To generate a random number between 0 and 100 do this:

srand((double)microtime()*1000000);  
echo rand(0,100);

So substitute relevant numbers above.

See also:
http://uk3.php.net/manual/en/function.rand.php
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 200 total points
ID: 24313958
The random number generated is a 32bit integer. That limits it to 9 digits maximum. Of course you could just stick two 5 digit randoms together. They are random after all...

echo mt_rand(10000,99999) . mt_rand(10000,99999) ;

0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 200 total points
ID: 24314009
[fx: slaps head]

Doh! 2^32 is 10 digits, not 9. That means you could generate the numbers directly in a smaller range

echo mt_rand( 1000000000, 2147483647 );

2147483647 is as big as you get in signed 32bit integers
0
 
LVL 3

Assisted Solution

by:Mathias
Mathias earned 50 total points
ID: 24314017
mt_rand() gives you an int :-) What's the largest value for an int? Right, 2147483647.
http://www.php.net/manual/de/function.mt-getrandmax.php

You have to write your own randomizer to get numbers greater than integer.

0
 
LVL 17

Assisted Solution

by:Chris Harte
Chris Harte earned 150 total points
ID: 24314162
This will give you a 15 digit number
$i = 1;
while ($i < 16)
{
    $x = $x.mt_rand(0, 9);
    $i++;    
}
echo $x;

Open in new window

0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question