Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the chances to get a Guid.NewGuid () duplicate? [duplicate]

Tags:

c#

guid

Possible Duplicate:
Is a GUID unique 100% of the time?
Simple proof that GUID is not unique

In MSDN you can read:

The chance that the value of the new Guid will be all zeros or equal to any other Guid is very low.

Say that you have an method that will create a file every second, and you use the method Guid.NewGuid() for filename, is it possible to get the same Guid then? Or will the local computer keep track in some way? How low is the chance ?

like image 664
Niklas Avatar asked Dec 27 '11 09:12

Niklas


People also ask

What does GUID NewGuid () do?

Guid. NewGuid() creates an empty Guid object, initializes it by calling CoCreateGuid and returns the object.

Is GUID NewGuid unique?

Multiple threads allocating new guids will get unique values, but you should get that the function you are calling is thread safe.

Can a GUID repeat?

Technically they are 128-bit unique reference numbers used in computing which are highly unlikely to repeat when generated despite there being no central GUID authority to ensure uniqueness.

Is GUID NewGuid thread safe?

You are concerned with concurrency: fortunately, the NewGuid method is thread-safe, which means it either locks or utilizes a thread-static random number generator for its purposes.


1 Answers

The chances of getting two identical guids are astronomically slim even if you are generating guids as fast as you can. (Generating, say, thousands of guids per second for the sole purpose of finding a duplicate.)

Of course, if you want my opinion, I do believe that there will be a time, in a couple of thousand years from now, when we will be colonizing the galaxy, our population will be in the trillions, the number of individual computers embedded everywhere will number in the gazillions, and every single one of those computers will be generating GUIDs at a rate which is unthinkable today, when we will start running into trouble with duplicate guids popping up every once in a while in distant areas of the galaxy, and then it will be like 640k of memory all over again, DLL hell all over again, two-digit-year millenium bug all over again, all of them combined.

The thing with GUIDs is that we don't want them to be huge, because then they would be wasteful, so someone had to come up with a number of bits that is small enough to not be too wasteful and yet large enough to give a reasonable guarantee against collisions. So, it is a technological compromise. In our century 128 bits seem to be a good compromise, but with almost mathematical certainty there will be another century when this compromise will not be so good anymore.

like image 118
Mike Nakis Avatar answered Sep 19 '22 22:09

Mike Nakis