Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Random number generation

Posted on 2009-05-06
5
Medium Priority
?
414 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 400 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 800 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 800 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 200 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 600 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
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 count occurrences of each item in an array.
Suggested Courses

636 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