Is it possible that GUIDs are generated with all the same characters in .NET? (e. g.: {11111111-1111-1111-1111-111111111111})
Asked Answered
B

9

35

We use GUIDs extensively in our database design; Business Object properties provide Guid.Empty GUIDs for DB null values and null is always saved to the database if the value is Guid.Empty.

Apart from Guid.Empty (00000000-0000-0000-0000-000000000000) how likely is it that a GUID will be generated with all the same characters e. g.: 11111111-1111-1111-1111-111111111111

Just considering using GUIDs like these for specific values.

Bankable answered 28/1, 2010 at 15:25 Comment(16)
very unlikely :DFiberglass
Even though it's insanely unlikely, surely there's a better design than just using GUIDs for magic numbers?!Mentalist
Here at work, a guy got a duplicate GUID once; he'll never win at lottery, as already spent all his luckKiker
AS many have pointed out, it's highly unlikely. Still, if you're that concerned, write up some test cases (using fake, forced GUIDs) to see what would happen if 11111111-1111-1111-1111-111111111111 really was generated and used in your system. That way, you'll at least know how to handle it when it happens (because it's still possible, just unlikely)... And if it turns out that this test case breaks your system, you may want to replace GUIDs with some other scheme.Likker
@Rubens Farias: And the universe still hasn't collapsed? Seriously, there is no such thing as a duplicate GUID. Your co-worker was wrong. (or there's something very wrong with the random number generator of his machine/OS)Meredeth
Why would you need this design?Im
It was a just a thought on implementing some fixed items in a list page in asp.net eg: (show all), (none), (select) etc in addition to user-defined items, kinda like pseudo enums that are saved to the same db field, but having thought this through, its not a good idea. The idea was that a Guid might be less likely to contain the same characters based on the Guid algorithm rather than any other guid. I agree, no Magic Numbers and a better/more elegant solution.Bankable
If the GUID is generated randomly, wouldn't a GUID containing all the same characters be just as likely as one with different characters? Isn't that the definition of random, that all outcomes have the same potential to occur?Ferrotype
@DrJokepu, "Highly unlikely" does not mean "impossible" =)Kiker
Yes, it is also possible that a meteor strikes and kills you, just as you are reading this comment. Duck!Fortuitous
@Tester101: That's a big IF, actually. In fact, that's not how GUIDs are generated :-)Neurovascular
+1 The top answer actually makes the question useful =)Calculator
If you want to some constant values to have GUIDs, then simply create some GUIDs for them. No need to go and create your own that "might" be unique, or "might" never happen. Simply make some GUIDs and use them. That way you KNOW they're unique (they're, you know, GUIDs, from a GUID generator). Then simply document them and make constants from them in your code. Yes they're ugly, but GUIDs are ugly. They're always ugly.Stagestruck
@Will: Or just smash a network card: blogs.msdn.com/oldnewthing/archive/2004/02/11/71307.aspx ;-)Neurovascular
@Ferrotype Not really, take the numbers 0 - 99. The chance of A SPECIFIC number against any other number are equal, all 1/100. However the chances of ANY number with 2 digits the same are 9/100 (11,22,33,44,55,66,77,88,99) whilst the chances of getting a number where the digits are different are 91/100. (Been 5 years since you commented, sorry for the delayed reply)Negatron
@Negatron Yes, but that's true for every other combination of characters as well. So it's equally possible that it will be another combination, as it is that it will be this combination.Ferrotype
N
76

In short: For GUID generated according to the published standards and specifications it simply can't happen. A GUID has a structure and some of the fields actually have a meaning. What's more, .NET generates GUIDs of version 4, where it absolutely can't happen. They are defined in a way that there won't be such a GUID. For details, see below ;-)


There are five to seven bits which are the main pitfall here. Those are the version identifier (the first four bits of part three) and the variant field specifying what variant of GUID this is.

The version can be anything between 1 and 5 currently. So the only valid hex digits for which we could get such a GUID at this point are – obviously – 1 through 5.

Let's dissect the versions a little:

  1. MAC address and timestamp. Both are probably hard to coax into all-1 digits.
  2. MAC address and timestamp as well as user IDs. Same as for v1.
  3. MD5 hash. Could possibly even work.
  4. PRNG. Can't ever work since the first digit of the fourth part is always either 8, 9, A or B. This contradicts the 4 for the version number.
  5. SHA-1 hash. Could possibly even work.

So far we ruled out version 4 as impossible, others as highly unlikely. Lets take a look at the variant field.

The variant field specifies some bit patterns for backwards compatibility (x is a don't care), namely:

0 x x Reserved. NCS backward compatibility.
1 0 x The only pattern that currently can appear
1 1 0 Reserved, Microsoft Corporation backward compatibility
1 1 1 Reserved for future definition.

Since this pattern is at the very start of the fourth part, this means that the most significant bit is always set for the very first hex digit of the fourth part. This means that this very digit can never be 1, 2, 3 or 5. Not counting already generated GUIDs, of course. But those with the MSB set to 0 happen to be either v1 or v2. And the timestamp part of those means that they would have to be generated some millenia in the future for that to work out.

Neurovascular answered 28/1, 2010 at 15:32 Comment(8)
This is the most correct. E.g. if you are using System.Guid it is impossible because the 13th "nibble" will always be 4 and the 17th will always be 8, 9, A, or BUnderact
However, he's not just asking specifically for the "all 1" GUID, he just used that as an example. It sounds as though he could potentially uses an all-x guid as well to distinguish different specific values. Not that this really makes this argument any less valid.Zacheryzack
@Kevin: Rewrote from scratch. Assuming GUIDs generated according to the specifications this couldn't have happened so far.Neurovascular
Your "answer" is incorrect. A Guid has a finite number of possible values. If you generated an infinite number of Guids, how would you NOT end up with collisions?Token
@Arash, this is not about collisions. This is about the possibility that a GUID happens to have all the same digits. Besides, even for collisions the chance is close to zero (definitely for v1/v2, almost definitely for v4): “If each [of the 2^30] PCs can generate 2^20 GUIDs/s then after only about 2^72 seconds – one hundred and fifty trillion years – you'll have a very high chance of generating a collision with your specific GUID. The odds of collision get pretty good after only thirty trillion years.”Neurovascular
@Joey, your answer stated that it can't possibly happen meaning impossible. It is not impossibly, just very very unlikely.Token
@Arash, I am sure by now you have actually read my answer which clearly states that “for GUIDs generated according to the published standards it cannot happen”. And then please go and read RFC 4122. GUIDs are not just a (pseudo-)random collection of bits. It is a structure where some parts are fixed, some defined within a range and the rest made up of things that either have a specific meaning or are just random. Again, this is not about collisions, it's about probability that a GUID with all the same hex digits is generated. This probability is zero for conforming GUID generators.Neurovascular
I'm sorry Joey, we were arguing 2 different points :) Somehow I read your answer as saying that it was impossible for 2 Guids to ever be the same (a collision). Your answer is actually stating that it is impossible for a Guid to be 11111111-1111-1111-1111-111111111111. I removed the down-vote since your answer is 100% correct for the original question. Edit well I tried to remove the downvote and switch to an upvote by my answer was locked in already.Token
K
19

There are exactly 5,316,911,983,139,663,491,615,228,241,121,400,000 possible combinations, so even if it wasn't designed to always be unique, the chances would be pretty remote anyway.

Source: http://msdn.microsoft.com/en-us/library/aa446557.aspx

Korikorie answered 28/1, 2010 at 15:28 Comment(6)
"so you're saying there's a chance??" - movie quote, couldn't help myselfAlexaalexander
-1 - The source states that there is are that many GUID combinations, but GUIDs are not just random numbers, and so thats not the same as the probability of a given GUID existing.Drag
Also, this is wrong on the virtue that there are 15 such GUIDs that meet the criteria, not just 1! :-)Drag
-1 Wrong answer: this is the probability of getting any 1 specific GUID within this specific implementation.Underact
What was all that 1 in 5,316,911,983,139,663,491,615,228,241,121,400,000 talk?!Astroid
"Never tell me the odds kid!" -In case you were looking for another quote, but the first one was pretty good. ;)Hacksaw
Z
8

About as likely as any other randomly generated guids will collide. So, highly unlikely.

Though, you may want to rethink using guids to "store" data like that. They are really used to uniquely identify objects and components.

Zacheryzack answered 28/1, 2010 at 15:27 Comment(2)
-1 for saying that randomly generated guids are equally likely to collide - GUIDs are generated so that it is impossible for 2 GUIDs to collide except in silly circumstances such two machines with the same MAC address generating GUIDs at the exact same time.Drag
Kragen, I think you misunderstood my point. It is next to impossible for any random GUID to be duplicated, so I was pointing out that there is no difference in probability (apart from the specific cases mentioned by Johannes) that two random generated GUIDs will match and a random guid and 11111111-1111-1111-1111-111111111111 will match.Zacheryzack
S
5

GUID's are usually generated using an algorithm, rather than being a genuinely random string of hex characters. If you can be sure what algorithm is being used to generate them you can then be sure if the GUIDs you want to use as "magic numbers" are going to collide with generated ones.

The Wikipedia page on GUIDs has a decent amount of information regarding the algorithms that are used, so that may be able to give you a definitive answer. Or, running Reflector over the Guid.NewGuid() method in the .net framework, though based on looking at the reference source for the method this calls out to CoCreateGuid in OLE32.

Spaghetti answered 28/1, 2010 at 15:30 Comment(0)
J
5

Your question has already been answered, but I thought I'd be pragmatic here.

1) You will only give yourself 8 "hard-coded" options using this convention.

2) You could just create a real GUID for each these "special" cases instead of hand-cranking them. That way, it is guaranteed to be unique and you'll be able to have more than 8.

That's not a direct answer, I know, but it is probably a sensible suggestion given your intentions.

Jacquettajacquette answered 28/1, 2010 at 16:8 Comment(0)
W
4

Why use a specially designed GUID? Beside the recognizable aspect, why not just use a properly generated GUID? (You know it will be unique, that is the point)

Willettawillette answered 28/1, 2010 at 15:32 Comment(0)
E
4

For my last company, we used guids as primary keys for tables for all of our databases. In all we instantiated more than 1,000,000,000 objects and never had any issues.

Eiger answered 28/1, 2010 at 17:27 Comment(0)
H
3

Very, very low. The GUID format includes a few bits identifying the scheme. If you'd "generate" them yourselves, the GUID scheme would most likely be wrong.

Hulbert answered 28/1, 2010 at 15:28 Comment(0)
T
0

I've been sitting here at https://www.guidgenerator.com/online-guid-generator.aspx hitting the generate guid button every second since january 28 2010. It's now february 10 2021, and i haven't gotten a duplicate.

Teena answered 10/2, 2021 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.