Solved

Random number generation

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now