randomly generated number

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
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

randomly generated number

Post by ibanez270dx »

Hi,
Is there a way to create a randomly generated 11 digit number? I'd like to use it to create SKU numbers...

Thanks,
- Jeff
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

some use of rand(), mt_rand() and such.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

mt_rand() is supposed to be better for some reason that escapes me now.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: randomly generated number

Post by onion2k »

ibanez270dx wrote:Is there a way to create a randomly generated 11 digit number? I'd like to use it to create SKU numbers...
SKU numbers shouldn't be random. As a rule, when I generate SKUs I always use a human recognisable format .. for example:

CN-0776-00023

CN - Site
0776 - Category
00023 - Product id

If you need a random bit to obscure the number to the user, tag a few random digits on the end or something. I wouldn't make it all random.
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post by jabbaonthedais »

Why random and not sequential? Basically what onion2k said, but if you don't already have product id's just start with any number and count up (storing each id as a reference in a database).
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Post by ibanez270dx »

yes, I like onion2k's idea best... I will probably use sequencial. Thanks everybody!
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

using rand isnt always reliable too. i used it, but now i had problems because "randomly" the number came out two times and replaced a page with another one.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Moral of the story is random is no subsitute for sequence when you need to generate a unique number.
Post Reply