random x digit number generation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sh33p1985
Forum Commoner
Posts: 78
Joined: Thu Mar 11, 2004 9:22 am

random x digit number generation

Post by sh33p1985 »

looking to generate a random 8 digit number from 00000000-99999999 inclusive but cant work out how to do it easily.
e.g. 03840441

Cheers
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Try rand and str_pad
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Numbers that large can easily be outside the maximum range of the random engine, so you may need multiple calls to rand() or mt_rand()

On my local system:

Code: Select all

[feyd@home]>php -r "var_dump(getrandmax(), mt_getrandmax());"
int(32767)
int(2147483647)
sh33p1985
Forum Commoner
Posts: 78
Joined: Thu Mar 11, 2004 9:22 am

Post by sh33p1985 »

quite frankly i could shorten that to 6 digits no problem, the main principle is that is random number generated will always bee x digits long.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Code: Select all

for($i=0;$i<$yourStringLength;++$i)
{
   $random_digit .= rand(0,9);
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Random numbers arent always reliable... Even though the range of numbers is so great, beleive it or not I had the same number twice. If you are trying to generate unique numbers. Use the persons IP and time stamp, it works perfect.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

nickman013 wrote:Random numbers arent always reliable... Even though the range of numbers is so great, beleive it or not I had the same number twice. If you are trying to generate unique numbers. Use the persons IP and time stamp, it works perfect.
Good point. Unfortunately, 'perfect' means 100% and neither of our methods are 100%. You're right that mine could generate duplicate ids. Yours could too (technically) if two people from behind the same NAT gateway access the page in the same second. ~sh33p1985: I believe if you incorporate uniqid() into the mix, you should be ok.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

lol thats true
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think the only way to get true uniqueness is to create the number is some way that has been mentioned in this thread, then store it in the database. Check to see if it is in there, and if so, regenerate, else, use it.
Post Reply