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

Niklas picture Niklas · Dec 27, 2011 · Viewed 74.6k times · Source

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 ?

Answer

Mike Nakis picture Mike Nakis · Dec 27, 2011

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.