Solved

Random number generation

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Healthcheck 2 81
converting numbers with php 3 26
PHP warning 4 28
datetime in sql 6 22
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to dynamically set the form action using jQuery.

930 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

12 Experts available now in Live!

Get 1:1 Help Now