Generating unique numbers using php

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
madhurima
Forum Newbie
Posts: 15
Joined: Thu Mar 12, 2009 4:55 am

Generating unique numbers using php

Post by madhurima »

Hi,
I need help in generating unique number using php.I tired using rand() in php.But, by using rand() method,the generated values are repeating.I want unique numbers in my application.
Every time in loop,i need a unique number.
If anybody knows of generating unique numbers,pls reply AEAP.


Thank u inadvance,
madhu :)
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Generating unique numbers using php

Post by Mark Baker »

If the unique numbers are for a database table, then an autoincrement column

Alternatively, mktime() is unique to the second, or microtime to the 1000/th of a second
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Generating unique numbers using php

Post by markusn00b »

Try srand() instead.

Mark.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Generating unique numbers using php

Post by AbraCadaver »

madhurima wrote:Hi,
I need help in generating unique number using php.I tired using rand() in php.But, by using rand() method,the generated values are repeating.I want unique numbers in my application.
Every time in loop,i need a unique number.
If anybody knows of generating unique numbers,pls reply AEAP.


Thank u inadvance,
madhu :)
Many ways to combine functions to get unique numbers. Here are two functions:

mt_rand() and uniqid()
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Generating unique numbers using php

Post by pickle »

Neither srand(), rand(), mt_rand(), nor uniqid() are guaranteed to give you a unique number. The chance of collision for all of those is pretty low, but not 100%. The only way to guarantee you aren't duplicating a random number is to store the numbers you have already generated, then check against them.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Generating unique numbers using php

Post by Mark Baker »

An autoincrement column on a database, is going to be unique
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Generating unique numbers using php

Post by markusn00b »

pickle wrote:Neither srand(), rand(), mt_rand(), nor uniqid() are guaranteed to give you a unique number. The chance of collision for all of those is pretty low, but not 100%. The only way to guarantee you aren't duplicating a random number is to store the numbers you have already generated, then check against them.
And they aren't 'random' in the true sense of the word, but who cares really? ;)
madhurima
Forum Newbie
Posts: 15
Joined: Thu Mar 12, 2009 4:55 am

Re: Generating unique numbers using php

Post by madhurima »

thanks for all..... for giving replies.Unique id is very useful in generating unique numbers.


Thanks,
Madhu :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Generating unique numbers using php

Post by pickle »

markusn00b wrote:And they aren't 'random' in the true sense of the word, but who cares really? ;)
Your clients do when suddenly someone else has access to their session data.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply