Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How generate unique Integers based on GUIDs

Tags:

c#

Is it possible to generate (highly probable) unique Integer from GUIDs?

int i = Guid.NewGuid().GetHashCode();  int j = BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0); 

Which one is better?

like image 697
anonim Avatar asked May 27 '10 11:05

anonim


People also ask

How can a GUID be unique?

How unique is unique? A GUID is a unique number that can be used as an identifier for anything in the universe, but unlike ISBN there is no central authority - the uniqueness of a GUID relies on the algorthm that was used to generate it.

How are GUIDs generated?

A GUID (globally unique identifier) is a 128-bit text string that represents an identification (ID). Organizations generate GUIDs when a unique reference number is needed to identify information on a computer or network. A GUID can be used to ID hardware, software, accounts, documents and other items.

How much of a GUID is unique?

The GUID generation algorithm relies on the fact that it has all 16 bytes to use to establish uniqueness, and if you throw away half of it, you lose the uniqueness. There are multiple GUID generation algorithms, but I'll pick one of them for concreteness, specifically the version described in this Internet draft.


1 Answers

Eric Lippert did a very interesting (as always) post about the probability of hash collisions.

You should read it all but he concluded with this very illustrative graphic:

Probability of hash collisions

Related to your specific question, I would also go with GetHashCode since collisions will be unavoidable either way.

like image 102
João Angelo Avatar answered Oct 27 '22 20:10

João Angelo