views:

472

answers:

3

I would like to get a few ideas on generating unique id's without using the GUID. Preferably i would like the unique value to be of type int32.

I'm looking for something that can be used for database primary key as well as being url friendly.

Can these considered Unique?

1) (int)DateTime.Now.Ticks
2) (int)DateTime.Now * RandomNumber

Any other ideas?

Thanks

EDIT: Well i am trying to practise Domain Driven Design and all my entities need to have a ID upon creation to be valid. I could in theory call into the DB to get an auto incremented number but would rather steer clear of this as DB related stuff is getting into the Domain.

+4  A: 

It depends on how unique you needed it to be and how many items you need to give IDs to. Your best bet may be assigning them sequentially; if you try to get fancy you'll likely run into the Birthday Paradox (collisions are more likely than you might expect) or (as in your case 1) above) be foreced to limit the rate at which you can issue them.

Your 1) above is a little better than the 2) for most cases; it's rate limited--you can't issue more than 1 ID per tick--but not susceptible to the Birthday Paradox. Your 2) is just throwing bits away. Might be slightly better to XOR with the random number, but in any case I don't think the rand is buying you anything, just hiding the problem & making it harder to fix.

MarkusQ
"it's rate limited--you can't issue more than 1 ID per tick" - not really. It depends on the precision of the system clock.
Joe
I'd go the sequential route.
Joe Philllips
@Joe -- If you mean the rate might be lower (the clock skips "ticks") sure. But it _can't_ be higher, so it is limited by the tick rate.
MarkusQ
No, unless your tick is any number on the continuous, real, number line, then such a method is rate limited because there is no guarantee that there will not be collisions.
Brad Barker
A: 

You can use the RNGCryptoServiceProvider class, if you are using .NET

RNGCryptoServiceProvider Class

Kirtan
+1  A: 

Are these considered Globally Unique?

1) (int)DateTime.Now.Ticks 2) (int)DateTime.Now * RandomNumber

Neither option is globally unique.

Option 1 - This is only unique if you can guarantee no more than one ID is generated per tick. From your description, it does not sound like this would work.

Option 2 - Random numbers are pseudo random, but not guaranteed to be unique. With that already in mind, we can reduce the DateTime portion of this option to a similar problem to option 1.

If you want a globally unique ID that is an int32, one good way would be a synchronous service of some sort that returns sequential IDs. I guess it depends on what your definition of global means. If you had larger than an int32 to work with, and you mean global on a given network, then maybe you could use IP address with a sequence number appended, where the sequence number is generated synchronously across processes.

If you have other unique identifiers besides IP address, then that would obviously be a better choice for displaying as part of a URL.

Brad Barker