Re: Random Unique ID

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



ccspencer@xxxxxxxxxxxx wrote:

> Hello,
> 
> I want to add a random unique ID to a Mysql table.  Collisions
> are unlikely but possible so to handle those cases I'd like to
> regenerate the random ID until there is no collision and only
> then add my row.  Any suggestions for a newbie as to the right
> way to go about doing this?
> 
> Best,
> 
> Craig

First some computer science 101: A random number is not unique and a unique
number is not random. Learn it, know it, live it.

What you need is a component of the ID which is not random, but is probably
unique within the environment. Say, the IP address of the machine. Then,
perhaps the process id of Apache/PHP process. Then, the random component,
the time() (which is random with respect to the request, but unique for one
second), followed by some rand() requests seeded by some high resolution
micro timer. This will have a HUGH probability of uniqueness within the
single process, so it is virtually assured, and a guarantee of uniqueness
across all other processes and machines.

I suggest looking into a GUID sort of thing, it is all coded for you and it
works just like you want.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux