Solved

Random number generation

Posted on 2009-05-06
5
384 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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