Page 1 of 1
Generating unique numbers using php
Posted: Thu Oct 22, 2009 7:35 am
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

Re: Generating unique numbers using php
Posted: Thu Oct 22, 2009 7:39 am
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
Re: Generating unique numbers using php
Posted: Thu Oct 22, 2009 7:56 am
by markusn00b
Try
srand() instead.
Mark.
Re: Generating unique numbers using php
Posted: Thu Oct 22, 2009 12:03 pm
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()
Re: Generating unique numbers using php
Posted: Thu Oct 22, 2009 2:05 pm
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.
Re: Generating unique numbers using php
Posted: Thu Oct 22, 2009 2:59 pm
by Mark Baker
An autoincrement column on a database, is going to be unique
Re: Generating unique numbers using php
Posted: Thu Oct 22, 2009 5:17 pm
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?

Re: Generating unique numbers using php
Posted: Mon Oct 26, 2009 12:35 am
by madhurima
thanks for all..... for giving replies.Unique id is very useful in generating unique numbers.
Thanks,
Madhu

Re: Generating unique numbers using php
Posted: Mon Oct 26, 2009 9:59 am
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.