Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make sure a generated guid is unique globally?

Tags:

c#

guid

Let's say I want to set a guid to be my application's assembly guid. As searched from internet, we can use (new Guid()).Next() to get a new unique value.

I cannot figure out how my guid is warranted to be unique against others? Please explain if you know how to.

like image 249
Nam G VU Avatar asked Oct 20 '10 06:10

Nam G VU


People also ask

How is a GUID Globally 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 do I make a global unique identifier?

Individuals and organizations can create GUIDs using a free GUID generator that is available online. An online generator constructs a unique GUID according to RFC 4122. When creating a GUID, users should note the timestamp, clock sequence and the node ID -- such as a Media Access Control (MAC) address.

Is it safe to assume GUID is unique?

In general, yes it is safe to assume. If your GUID generator is truly random, the possibilities of a clash within a 1000 GUIDs is extraordinarily small.

Can a GUID never be duplicated?

While each generated GUID is not guaranteed to be unique, the total number of unique keys (2128 or 3.4×1038) is so large that the probability of the same number being generated twice is very small.


2 Answers

The only guarantee you have is that probability is on your side. 2^128 possible GUIDs and some cleverness in the creation process makes it very unlikely you will ever see a duplicate.

It seems V4 is the standard GUID on Windows now. If that one is purely based on a pseudo-random number generator, as Wikipedia seems to indicate, it's affected by the Birthday problem.

I've seen several examples using 128-bits to show that a duplicate is almost impossible. Those often miss two things. The Birthday problem and that a V4 GUID actually is 124 bits.

You need 1/2+sqrt(1/4-2*2^124*ln(0,5)) ≈ 5.4*10^18 GUIDs to reach a 50% chance of a duplicate. That is still a lot, but 50% may not be the deal you are looking for. Say you want it to be one in a million to get a duplicate, then you can have sqrt(2*2^124*ln(1/(1-0,000001))) ≈ 6,5*10^15 GUIDs. If you create a thousand GUIDs per second you could keep on doing that for almost 206667 years before reaching a one to a million risk of getting a duplicate. 6,52191054316287e15/(3600*24*365,25*1000) ≈ 206666,874006986

The chance of me getting all of those calculations correct →0.

like image 143
Jonas Elfström Avatar answered Sep 22 '22 21:09

Jonas Elfström


It isnt, but the way it is generated and the way it is represented makes probability of generating two same GUIDs in this milenium almost zero.

See: Simple proof that GUID is not unique

like image 28
Euphoric Avatar answered Sep 20 '22 21:09

Euphoric