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
ibanez270dx
Forum Commoner
Posts: 74 Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California
Post
by ibanez270dx » Fri Aug 25, 2006 11:49 am
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Aug 25, 2006 12:18 pm
some use of
rand() ,
mt_rand() and such.
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Fri Aug 25, 2006 12:29 pm
mt_rand() is supposed to be better for some reason that escapes me now.
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Fri Aug 25, 2006 12:42 pm
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 » Fri Aug 25, 2006 1:09 pm
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 » Fri Aug 25, 2006 1:40 pm
yes, I like onion2k's idea best... I will probably use sequencial. Thanks everybody!
nickman013
Forum Regular
Posts: 764 Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York
Post
by nickman013 » Fri Aug 25, 2006 2:23 pm
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.
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Fri Aug 25, 2006 2:37 pm
Moral of the story is random is no subsitute for sequence when you need to generate a unique number.