49

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 ?

Community
  • 1
  • 1
Niklas
  • 1,753
  • 4
  • 16
  • 35
  • 22
    About 1 in 2^128. – tenfour Dec 27 '11 at 09:13
  • 1
    No, the computer doesn't keep track. But no, the chances of a duplicate GUID are so low as to not be relevant. You've already answered your own question in the question; all the answers are just parroting that back to you. – Cody Gray - on strike Dec 27 '11 at 09:37
  • @CodyGray specific to the OP's context, the system *does* keep track. The filesystem will keep track of every GUID that's been used for this purpose, and won't let you create a file with an identical name. So basically if the OP writes good code, the answer is that it doesn't matter if there are collisions or not. – tenfour Dec 27 '11 at 11:20
  • 2
    @tenfour: That's not how I interpreted the question. Yes, the file system keeps track of all file names. But `Guid.NewGuid()` does *not* keep track of the GUIDs generated. – Cody Gray - on strike Dec 27 '11 at 13:04
  • 9
    Statistically speaking, you would be more likely to win the lottery 30,382 times, consecutively, than to run into a single pair of GUIDs within a sample of 1,000,000,000,000 GUIDs. – Gurgadurgen Jun 22 '14 at 20:54
  • It's like (3.40282366920938463463374607431768211456 × 10^38), which is 340.282.366.920.938.463.463.374.607.431.768.211.456 if you can read the number lol. I'd say, *practically*, impossible. –  Dec 16 '15 at 21:27
  • 6
    everyone here is correct, but I like to think of Murphy's Law in situations like this – jtate Mar 23 '17 at 15:57
  • On average, you would need to generated about 2^64.5549267 GUIDs to have a 50% chance of a duplicate. ANY answer that mentions 2^128 is wrong even without mentioning the Birthday Paradox because GUIDs are `not` 128 random bits. – Charles Burns Dec 14 '17 at 21:09
  • 1
    To be honest, it has happed to me. I am surprised that people say the chance of duplicate GUID is almost zero. It happened to my web application in production a couple of times. The table had about 200,000 rows, which is not too big. So, it can happen. – Sparrow Jul 10 '18 at 20:48
  • @tenfour it's only 1 in 2^128 when you generate the second guid; for every guid after that the chance goes up, 2 in 2^128, 3 in 2^128, etc... – Derek Tomes Jan 25 '19 at 02:33
  • I was disturbed to see this in a client's database : "000564C2-0000-0000-0000-000000000000" Disturbed mostly because I was populating the field with NewGuid() so that seemed impossible... until the client admitted he stuffed data in the field without knowing what he was doing. – Argle Mar 24 '22 at 16:41

6 Answers6

65

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.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • 1
    By that time we will have quantum processors capable of quanary operations and true multitasking and I don't think we'll care about guids anymore. We will also likely have quantum internet that uses Quantum Entanglement to transmit data and we'll have a universe spanning internet with 10ms latency from any point to point. Also quite possible we'll have warp drives (bend space to get FTL..) – Ryan Mann Dec 19 '14 at 06:31
  • I am afraid that our ability to communicate faster will only make GUID collisions a more frequent phenomenon. – Mike Nakis Jun 30 '16 at 22:14
  • Thousands of years? With the current model, there are enough GUIDs to fit ~800 million GUIDS per nanosecond over the last 13.800 billion years. Even if we get quadrillions of computers each creating billions of GUIDs every second, the risk will be negligible for a long time. If there truly will be a day where it could be a risk, we would have millennia to prepare for a new standard with say 4096 bits. – Aske B. Sep 14 '17 at 06:11
  • 2
    @AskeB. It is quite unclear to me what you mean when you say "fit X GUIDs per nanosecond over the last Y years", but it seems that you might be thinking of enumeration, and if that is the case, you are completely missing the point. The question is not how long it will take to enumerate the entire 128-bit space, the question is how often there will be a collision when generating GUIDs using the standard random GUID generation algorithm. Issuing GUIDs is completely unrelated to enumerating 128-bit numbers. – Mike Nakis Mar 16 '18 at 21:04
  • For anyone interested, I expounded on this topic in a post on my personal blog: [michael.gr - What is wrong with UUIDs and GUIDs](https://blog.michael.gr/2017/06/on-uuids-and-guids.html) – Mike Nakis Dec 31 '22 at 13:14
23

You would never run out of guids. The likelihood of duplicating them is VERY low:

http://betterexplained.com/articles/the-quick-guide-to-guids/

Stefan H
  • 6,635
  • 4
  • 24
  • 35
  • 2
    I am not agreed with this, as it is not very low. I have a practical example of it, http://stackoverflow.com/questions/39771/is-a-guid-unique-100-of-the-time/21570510#21570510 – vikas Feb 05 '14 at 07:00
  • Well... If you dont stay on 2^128, then generating twice GUIDs in per generation. – nyconing Apr 21 '18 at 14:53
10

There is always some miniscule chance of duplicates, but Globally Unique Identifiers are meant to be just that: globally unique ...not system-wide unique, but as in the Planet Earth unique.

I speculate that, theoretically, you have a better chance of duplicating a UUID across multiple systems, than on a single system. While the OS isn't going to store each GUID that it generates, it probably uses some time based seed data to avoid collisions in itself. Of course this is dependent on the implementation.

Oh, and chances...well there are 3.4 x 10^38 available, Wikipedia says you're more likely to get hit by a meteorite.

I'll also offer an alternative method, the Path.GetTempFileName() method may be worth looking into, because it has protection against collision...though it can only create 65,535 unique file names before throwing an exception if previous files aren't deleted.

Other than that, it's not very difficult to do:

string path;

do
{
    path = Guid.NewGuid().ToString(); // Format as needed

} while (File.Exists(path));
rfmodulator
  • 3,638
  • 3
  • 18
  • 22
  • 3
    it makes me feel better that u went from it being just system wide to planet earth unique!! still i am paranoid about the fact that its not 100% certain. I would rather check the db to see it exists but i know that causes more overhead... – Andy Oct 13 '15 at 14:05
2

Please read the following question for info about collisions: Are GUID collisions possible?

Regarding the "one guid per second", generated guids usually take time into account, so on the same computer, 0 chance to generate the same guid unless you change the settings of the internal clock

edit: apparently the wikipedia page about guids says that time is not mandatory when building a guid so i guess it depends on the algorithm used. Since we're talking C# (on windows?) it's in fact a UUID which does include timing in some versions.

Community
  • 1
  • 1
samy
  • 14,832
  • 2
  • 54
  • 82
0

You should not worry about getting duplicate GUID when you are creating it every second. You can refer to http://en.wikipedia.org/wiki/Universally_unique_identifier and read the probability of duplicate GUID.

peterflynn
  • 4,667
  • 2
  • 27
  • 40
Kangkan
  • 15,267
  • 10
  • 70
  • 113
0

You will definitely run out of disk space or jam the filesystem before GUIDs collide. Just handle errors when creating files gracefully and you'll be fine.

tenfour
  • 36,141
  • 15
  • 83
  • 142