How to create random, but unique IDs? |
[eluser]n0xie[/eluser]
You could of course generate a list of available id's as a cronjob, and place them in a temp table/memory/file/xml/whatever. It depends on how many new id's you think you'll need on a day to day basis. You could even just store them in memory as an array and just remove any used id from that array, then schedule a background task to repopulate the array with new id's. This would make new id's instantly available, plus it would save you database lookups every time you need a new id.
[eluser]lauripapchi[/eluser]
I like huzzel's solution, it's pretty easy to do.
[eluser]Steven Ross[/eluser]
Huzzel's solution doesn't meet my requirements (see my earlier reply). I need to show these numbers to users and 20-character hex strings are just way too awkward. I'll build something like what n0xie suggested... now figuring out how to keep an array in memory across sessions. I think that'd work best. Thanks all!
[eluser]Buso[/eluser]
[quote author="Steven Ross" date="1273798316"]Huzzel's solution doesn't meet my requirements (see my earlier reply). I need to show these numbers to users and 20-character hex strings are just way too awkward. I'll build something like what n0xie suggested... now figuring out how to keep an array in memory across sessions. I think that'd work best. Thanks all![/quote] serialize it an put it somewhere
[eluser]n0xie[/eluser]
[quote author="Steven Ross" date="1273798316"]now figuring out how to keep an array in memory across sessions. I think that'd work best. Thanks all![/quote] You could try memcache and the CI memcached library.
[eluser]mddd[/eluser]
If you have 100 000 id's out of a pool of 100 million, one in 1000 is used. That won't be a big problem in getting new id's just by trying. I think building a list upfront is more work than it pays off. You'll have to try a few times on maximum to get a new id. No problem there I would say. You're not making id's all the time - otherwise you would be over 100 000. So I'd just try a new random id and try again if it already exists. That said, I think you are in an in-between situation here. Do you need to have id's that can't easily be guessed? Then you should have longer id's. Does it not really matter? Then why not just use incrementing numbers. I mean, if some kind of security depends on people not guessing id's, I would not want to be in this database. Guessing a number is pretty easy if there are 100 000 (or even just 10 000) items in a pool of 100 M. |
Welcome Guest, Not a member yet? Register Sign In |