Is a GUID unique 100% of the time?
Asked Answered
T

25

675

Is a GUID unique 100% of the time?

Will it stay unique over multiple threads?

Triumphant answered 2/9, 2008 at 15:17 Comment(20)
No, not 100%... Just 99,999999999999999999999999999999999999999999999999999999999999999999999999999% ;)Ware
First of all, a GUID is not infinite, which means that for the literal meaning of "100% of the time", would mean that no matter how long you keep generating GUID's, they would always be unique. This is not the case. Also, since the original implementation, where the network card unique serial/id/MAC was used to produce a portion of the key is no longer used, for various reasons, a GUID is not really globally unique any more. It is, however, locally unique. In other words, if you keep generating GUIDs on a single machine, you will not get duplicates.Shorttempered
@ojrac I just choose to round down... :PWare
Further to Lasse's excellent comment: the cross-machine uniqueness of a GUID is related to the implementation/algorithm used. See: wikipedia on GUIDS - most GUID implementations on most platforms (including SQLCE 3.5) use type 4 GUIDs which are based on random number generators so they should go across machine boundaries fine (look for the '4' after the second hyphen).Cottonweed
What's a 'language-agnostic guid'?Alic
Every time I generate GUID I feel like I'm stealing one from the Universe. Sometimes I think about evil people who generate much more GUIDs than they need and those wasted GUIDs are so lonely not being used or generated again...Manas
A GUID (Globally Unique Identifier) is theoretically useless if it's only locally unique and not globally unique, because it loses guarantees provided by a truly globally unique identifier. Aside from its more unpredictable nature, it would have no advantage over an incremented integer served from a single-instance system-service. The main advantage of a truly global identifier is that you can generate them on different systems and still have them guaranteed to be unique when periodically pooled in a central database.Hallucinosis
128-bits is big enough and the generation algorithm is unique enough that if 1,000,000,000 GUIDs per second were generated for 1 year the probability of a duplicate would be only 50%. Or if every human on Earth generated 600,000,000 GUIDs there would only be a 50% probability of a duplicate. According to: guidgenerator.com/online-guid-generator.aspxAuxesis
@Manas I think you'll like wasteaguid.info ^_^Centri
I don't think that's very unique at all.Headphone
I still don't understand this willingness to gamble.Oe
I had it once that the same guid generated 9 times the same id, >.< Yikes!!Gibeon
The last GUID my application generated - I swear I have seen it before.Eadwina
Intersting thing happened - Recently i generated a GUID for my Firefox extension and it appeared to be the same as Firefox app identifier GUID. Crazy thing is - there was no way it was a bug. It was generated trough python's uuid module. Weird stuff. For some reason i feel down..Swiss
@Manas I had the same feeling, until I realized GUIDs typically include some form of timestamp, which means that at every point in time, any GUID not generated is a GUID wasted. In other words, you cannot "waste" GUIDs.Down
Just to add the to the logic of Global vs Local Unique IDs that can always relative to your universe. GUID can be used to globally identify entities within your local universe where using incremental IDs require two properties to achieve the same goal (ID + Type).Iorgo
for the record, I've run about a bug once, where one client crashed because the new GUID the software was trying to insert into the database was already there but for a different record. This occured only once in years of use of the software for hundreds of databases on our servers.Matelot
@Manas Dude I was having a good day... :(Unquote
@Centri all we need to do is load wasteaguid.info into some self-replicating iframes and make it the default page on some retail display laptops. MUAHAHAHAGrata
We cannot guarantee 100% uniqueness across global usage bcos it no longer considers the MAC Id and uses 0-9 and A-F (only). However if Guid were to use 0-9 and A-Z, we could have really really large combinations and we could guarantee a 100% !Caboodle
S
536

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. For example, consider the observable universe, which contains about 5×1022 stars; every star could then have 6.8×1015 universally unique GUIDs.

From Wikipedia.


These are some good articles on how a GUID is made (for .NET) and how you could get the same guid in the right situation.

https://ericlippert.com/2012/04/24/guid-guide-part-one/

https://ericlippert.com/2012/04/30/guid-guide-part-two/

https://ericlippert.com/2012/05/07/guid-guide-part-three/

​​

Stun answered 2/9, 2008 at 15:19 Comment(24)
Wouldn't they be called a UUID, then? ;)Daric
A GUID is microsoft's specifica implementation of the UUID standard. So, it's both. Globally unique ID vs Universally unique ID.Stun
Technically, it is not 2^128, because in a v4 GUID, you have one hex digit that will always be a 4 (effectively removing 4 bits), and two bits further on are also reserved. However, 2^122 valid V4 GUIDs still leaves about 5x10^36, which will do for me. and for you too. Each star will have to accept just about 1.1x10^14 GUIDs apiece.Obstipation
If you're like me, then you'll want to know that 2^128 written out is approximately: 34,028,236,692,093,846,346,337,460,743,177,000,000. Statistically, if you calculated 1000 GUIDs every second, it would still take trillions of years to get a duplicate.Embarrassment
It's not "very small", it's "very^very small". Get your english math right! :-POwl
I just thought its funny to read it out so here have fun guys :) Thirty four undecillion twenty eight decillion two hundred thirty six nonillion six hundred ninety two octillion ninety three septillion eight hundred forty six sextillion three hundred forty six quintillion three hundred thirty seven quadrillion four hundred sixty trillion seven hundred forty three billion one hundred seventy seven millionEmbargo
Whew! That's easy for you to say!Unlovely
@TheAdamGaskins and if you're like me, you'll notice that that's got almost full precision, and want to know that it is exactly: 340,282,366,920,938,463,463,374,607,431,768,211,456 (and you'd notice that you were off by a power of 10 in your number - it's ~340 undecillion, not ~34 undecillion)Toomay
I think that just saying that there are a lot of possible GUID's is only part of the answer. You would also need to know how the GUID is calculated. For instance if you need only 100 unique integers (instead of a GUID) and your method would be something like 'if (IsItASunnyDay) then return 1 else return 2', you would pratically only have two unique integers...Bowlds
As @GeertImmerzeel comments, the question of how the guid is generated is the important point. Just like in xkcd.com/221Shemikashemite
The risk of generating the same GUIDs might be very small, but we're software developers, so we should know that anything that can go wrong, WILL go wrong. So keep your try/catch blocks ;)Angelesangelfish
@adam davis, you said: the total number of unique keys (2^128 or 3.4×10^38) is so large that the probability of the same number being generated twice is very small this is not mean it cannot duplicate. eg: Real number is infinite, but 1 function which generate a real number isn't mean that it cannot generate duplicate realAnglice
From the same article: "we could with high probability generate a GUID collision in only an hour, provided that we got every PC on the planet to dedicate an hour of time to doing so." With devices increasing in number, and their speed increasing, GUID collision will only become more feasible. People should stop scoffing at it.Intelligence
@ChrisMoschini We would then have to store all the GUIDs created, then compare them to each other to find the collision. This would take a very, very, very long time even with all the new devices and all the faster speeds. All this trouble to merely create a collision - but then it becomes much more unlikely that they would actually collide! In other words, there may be several GUIDs that are exactly the same floating around in the world right now, but they will, in all probability, never come into contact with each other, and thus never collide.Stun
@ChrisMoschini So this question addresses whether two GUIDs could be created that are the same, but it doesn't address the even much more unlikely scenario where they would ever come into contact with each other. And the additional devices and speed doesn't make that probability increase substantially, nevermind the fact that the additional devices aren't even creating that many GUIDs in the first place. I'd suspect the average device doesn't create more than a few million in its entire lifetime.Stun
@AdamDavis A lot of assumptions there. All it takes for those GUIDs to collide is, let's say - objects in a procedurally generated universe game, where gamers occasionally bumble into each other. Remember that the storage might be distributed just like the generation. Your assumption devices might generate a few million ever, forever, sounds a lot like 640K is enough for everybody. Anyway, people flippantly dismiss GUID collision and it needs to stop. The known boundaries when it can happen, and when it's likely, are more objective and useful. And closer than oft portrayed.Intelligence
@ChrisMoschini well perhaps you should go through the statistical analysis yourself given today's usage and post an answer. I'll be very interested to read your take on it.Stun
In the news today: spacetelescope.org/news/heic1620 "Observable Universe contains ten times more galaxies than previously thought" -- so, make the number of GUIDs per star one more order of magnitude smaller. Are you beginning to feel worried now?Pennoncel
@MikeNakis hmm, this is troubling. I suggest we immediately colonize each solar system and start the work of divvying up the remaining guids.Stun
Isn't there a risk, therefore, that if each of us generates more than 895,000 GUID then we will get disgruntled cease-and-desist letters from the other 5×10^22 star systems in the observable universe? That could clog up the postal system quite badly.Strife
@Arafangion, GUID is a better word than UUID, because in theory there might be as many "globes" in the universe as there are GUIDS. If all globes start creating GUIDS, there is a big chance that the same GUID will occur on several globes, so then those GUIDS are only unique on that particular globe, and not accross the universe.Jory
@Jory - You are correct, however I think I was replying specifically to the answer here. "Consider the observable universe..."Daric
So, the possible number of combinations (as stated above) is 34,028,236,692,093,846,346,337,460,743,177,000,000 now, imagine a machine that makes 100 000 per scond, now imagine 1 000 000 000 machines like that one, that makes us have 1 000 000 000 000 00 IDs per second, or 10^15, theoretically, you could get a duplicate, BUT, those machines need to also be inserting data in the same database for the duplicate to actually MEAN somethingRubato
@hjavaher's answer was 2013 -- before GPT would've made that easy. +1Capablanca
C
175

If you are scared of the same GUID values then put two of them next to each other.

Guid.NewGuid().ToString() + Guid.NewGuid().ToString();

If you are too paranoid then put three.

Convolvulus answered 9/5, 2014 at 18:4 Comment(8)
You have to be very, very, very, very paranoid to append 3 GUIDs.Akron
@Akron No... very, very, very, very paranoid is 6 GUIDs. Paranoid is one appended, very paranoid is two appended, etc.Fibula
@Fibula I have created a website for calculating your paranoid level jogge.github.io/HowParanoidAmIPostbellum
@Postbellum xD That is amazing, lol. After 9 9's 999999999 in your form, I think Paranoia will a-splode my Browser.Fibula
@Postbellum at any negative number I am just flat paranoid. This is accurateUlrikaumeko
@Postbellum your website crashed after I put that I am level 10,000 paranoid. Now I am even more paranoidAcidfast
That must be one of the best answers I have seen for a while +1 :-)Hunk
@Postbellum Ha, placeholder is number 42 :).Pander
J
77

The simple answer is yes.

Raymond Chen wrote a great article on GUIDs and why substrings of GUIDs are not guaranteed unique. The article goes in to some depth as to the way GUIDs are generated and the data they use to ensure uniqueness, which should go to some length in explaining why they are :-)

Jutland answered 2/9, 2008 at 15:20 Comment(1)
I think Chen's article is referring to V1 of the GUID generation algorithm, which uses a MAC address & timestamp -- the current V4 uses a pseudo-random number instead: en.wikipedia.org/wiki/Globally_Unique_Identifier#AlgorithmEdelweiss
U
49

As a side note, I was playing around with Volume GUIDs in Windows XP. This is a very obscure partition layout with three disks and fourteen volumes.

\\?\Volume{23005604-eb1b-11de-85ba-806d6172696f}\ (F:)
\\?\Volume{23005605-eb1b-11de-85ba-806d6172696f}\ (G:)
\\?\Volume{23005606-eb1b-11de-85ba-806d6172696f}\ (H:)
\\?\Volume{23005607-eb1b-11de-85ba-806d6172696f}\ (J:)
\\?\Volume{23005608-eb1b-11de-85ba-806d6172696f}\ (D:)
\\?\Volume{23005609-eb1b-11de-85ba-806d6172696f}\ (P:)
\\?\Volume{2300560b-eb1b-11de-85ba-806d6172696f}\ (K:)
\\?\Volume{2300560c-eb1b-11de-85ba-806d6172696f}\ (L:)
\\?\Volume{2300560d-eb1b-11de-85ba-806d6172696f}\ (M:)
\\?\Volume{2300560e-eb1b-11de-85ba-806d6172696f}\ (N:)
\\?\Volume{2300560f-eb1b-11de-85ba-806d6172696f}\ (O:)
\\?\Volume{23005610-eb1b-11de-85ba-806d6172696f}\ (E:)
\\?\Volume{23005611-eb1b-11de-85ba-806d6172696f}\ (R:)
                                     | | | | |
                                     | | | | +-- 6f = o
                                     | | | +---- 69 = i
                                     | | +------ 72 = r
                                     | +-------- 61 = a
                                     +---------- 6d = m

It's not that the GUIDs are very similar but the fact that all GUIDs have the string "mario" in them. Is that a coincidence or is there an explanation behind this?

Now, when googling for part 4 in the GUID I found approx 125.000 hits with volume GUIDs.

Conclusion: When it comes to Volume GUIDs they aren't as unique as other GUIDs.

Upgrowth answered 14/1, 2010 at 7:33 Comment(10)
Remember that Super Mario Bros 3 ad from the 80's? All those people yelling "Mario! Mario! Mario!" around the world upset the randomness of the universe a bit.Rebroadcast
If you manually un-install Office 2010 with msiexec, it lists all the MSI GUID's of the office program. They all spell 0FF1CE. Seems like Microsoft have a fairly... loose... interpretation of how to generate a GUID ;)Shooin
These partition GUIDs were all created together at 2009-12-17 @ 2:47:45 PM UTC. They are unique to your machine, but putting "mario" as the node identifier is incorrect - it means they're not RFC-4122-compliant. Likewise, the 0FF1CE GUIDs fall under the "NCS backwards compatibility" section of RFC-4122, but it's unlikely that Microsoft is following the NCS rules for those values.Deutschland
I knew it, the Nintendo Security Administration has compromised the random number generators.Naught
maybe it's this same ball park as the name of the company making a mineral water (heard they lead the market) Evian. Spelled backwards gives Naive :-)Imperfective
They play as well with sending messages by using backward spelling. I.e. a company making bottled water Evian = Naive.Imperfective
@StephenCleary How did you find out when the GUIDs were created?Samaveda
@BhushanFirake: I used to have an online GUID decoder. The code is still around somewhere but the site is no longer live.Deutschland
Those are Type 1 UUIDs - you can tell from the 1 in 11de. The fix 6 bytes of a UUID are the node (i.e. MAC address) of the machine generating the UUID. In this case the MAC address is 80-6d-61-72-69-6f. You'll notice that the high-bit is set; this means it isn't a real MAC address; but instead is a psuedo-randomly generated MAC addressBunt
The fact that the high-bit was set to indicate a "Locally Administered" MAC address is actually the wrong bit to set. This led to a bug being found decades later in UuidCreateSequential. People at Microsoft were mistakenly thinking that the high bit being set makes it a "Localy Administered". That is a good and reasonable guess, but it's wrong. It's actually the 2nd-lowest bit of the highest byte (?! Yes, thank you IEEE). If Windows NT were being written today, that MAC address would probably read 02-6d-61-72-69-6f.Bunt
J
39

It should not happen. However, when .NET is under a heavy load, it is possible to get duplicate guids. I have two different web servers using two different sql servers. I went to merge the data and found I had 15 million guids and 7 duplicates.

Jethro answered 29/1, 2010 at 23:43 Comment(6)
How is this possible on two different machines? I thought part of the GUID was the machine name? (not arguing... just asking)Pilcomayo
This would only be true for v1 guids which uses MAC addresses (not machine name) as part of the GUID generation. The v4, which is the de facto STD no longer uses Mac addresses but a pseudo random number.Hotchpot
Guid.NewGuid always generates v4 GUIDs (and always has). Tim must have had extremely poor entropy sources.Deutschland
Is that have ever been replicated? that's a huge problem if it's the case.Aerography
Same here while Importing very large Datasets. From about 10-100 Million you get duplicates from Guid.NewGuidGraduate
@StephanBaltzer No, that’s simply impossible. If this actually happened to you there was either a bug in your code which e.g. truncated GUIDs or which confused rows of data. In fact, it would be more likely that there’s a bug in the NewGuid implementation than that you’d really observe this collision without a bug. But so far no such bug has been reported so I’d bet a nontrivial amount of money that issue was in your code.Roselba
S
31

Yes, a GUID should always be unique. It is based on both hardware and time, plus a few extra bits to make sure it's unique. I'm sure it's theoretically possible to end up with two identical ones, but extremely unlikely in a real-world scenario.

Here's a great article by Raymond Chen on Guids:

https://blogs.msdn.com/oldnewthing/archive/2008/06/27/8659071.aspx ​ ​ ​

Silkweed answered 2/9, 2008 at 15:19 Comment(3)
This article is rather old and referring to v1 of GUIDs. v4 does not use hardware/time but a random number algorithm instead. en.wikipedia.org/wiki/Globally_unique_identifier#AlgorithmBurrton
This link is brokenOmbudsman
Here is the link: devblogs.microsoft.com/oldnewthing/20080627-00/?p=21823Brade
T
30

Guids are statistically unique. The odds of two different clients generating the same Guid are infinitesimally small (assuming no bugs in the Guid generating code). You may as well worry about your processor glitching due to a cosmic ray and deciding that 2+2=5 today.

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

Triadelphous answered 2/9, 2008 at 15:21 Comment(1)
Depending on the guid version you're using based on the specs. Some guids are time and mac addressed based. Meaning for V2 the guid would have to be generated on the same machine at the same picosecond. This is like throwing a bag of 1000 pennies into the air and they all land heads up in a stack on their sides. It is possible but unlikely to the point that it doesn't bear mentioning as a risk unless lives are at stake.Counter
M
27

Eric Lippert has written a very interesting series of articles about GUIDs.

There are on the order 230 personal computers in the world (and of course lots of hand-held devices or non-PC computing devices that have more or less the same levels of computing power, but lets ignore those). Let's assume that we put all those PCs in the world to the task of generating GUIDs; if each one can generate, say, 220 GUIDs per second then after only about 272 seconds -- one hundred and fifty trillion years -- you'll have a very high chance of generating a collision with your specific GUID. And the odds of collision get pretty good after only thirty trillion years.

Metopic answered 8/6, 2012 at 14:59 Comment(1)
...and he continues in the next paragraph: "But that's looking for a collision with a specific GUID. [...] So if we put those billion PCs to work generating 122-bits-of-randomness GUIDs, the probability that two of them somewhere in there would collide gets really high after about 2^61 GUIDs are generated. Since we're assuming that about 2^30 machines are doing 2^20 GUIDs per second, we'd expect a collision after about 2^11 seconds, which is about an hour." (And finally he explains that, of course, not that many GUIDs are generated.)Sarchet
F
25

Theoretically, no, they are not unique. It's possible to generate an identical guid over and over. However, the chances of it happening are so low that you can assume they are unique.

I've read before that the chances are so low that you really should stress about something else--like your server spontaneously combusting or other bugs in your code. That is, assume it's unique and don't build in any code to "catch" duplicates--spend your time on something more likely to happen (i.e. anything else).

I made an attempt to describe the usefulness of GUIDs to my blog audience (non-technical family memebers). From there (via Wikipedia), the odds of generating a duplicate GUID:

  • 1 in 2^128
  • 1 in 340 undecillion (don’t worry, undecillion is not on the quiz)
  • 1 in 3.4 × 10^38
  • 1 in 340,000,000,000,000,000,000,000,000,000,000,000,000
Folkmoot answered 2/9, 2008 at 15:27 Comment(1)
Actually, I disagree about 'not worrying about it', although from a different stance: if you do detect a GUID collision, then something has gone wrong with your application. I've used GUIDs, for instance, for idempotency, and have got a collision when a command has been sent twice (with the same GUID).Pyotr
Y
22

None seems to mention the actual math of the probability of it occurring.

First, let's assume we can use the entire 128 bit space (Guid v4 only uses 122 bits).

We know that the general probability of NOT getting a duplicate in n picks is:

(1-1/2128)(1-2/2128)...(1-(n-1)/2128)

Because 2128 is much much larger than n, we can approximate this to:

(1-1/2128)n(n-1)/2

And because we can assume n is much much larger than 0, we can approximate that to:

(1-1/2128)n^2/2

Now we can equate this to the "acceptable" probability, let's say 1%:

(1-1/2128)n^2/2 = 0.01

Which we solve for n and get:

n = sqrt(2* log 0.01 / log (1-1/2128))

Which Wolfram Alpha gets to be 5.598318 × 1019

To put that number into perspective, lets take 10000 machines, each having a 4 core CPU, doing 4Ghz and spending 10000 cycles to generate a Guid and doing nothing else. It would then take ~111 years before they generate a duplicate.

Yetah answered 3/1, 2017 at 14:56 Comment(6)
I've edited your post following to this post - please edit if I did a mistake ;).Camisole
Hi @Cine, I have the power to edit your response but have opted not to because I want to get a chance for you to rebut it first, I'll probably come by in a month-ish to formally change it if I don't hear from you. I'm fairly certain your math is wrong though. the real equation for determining a 1% chance is this: ((2^128 - 1) / 2 ^128) ^ ( (n (n-1)) / 2) = .01. Your exponent is wrong. it isn't just n. You need C(n,2) (aka (n*(n-1))/2) to calculate all the combinations when you generate "n" guids. See here for more informationThymelaeaceous
Thanks Cine, I too ended up approximating n^2/2 since its so huge :)Thymelaeaceous
It would take 10000 machines 111 years to generate every single possible GUID, and then generate a duplicate. A duplicate would however occur long before all possible GUIDs have been generated. I think the approximate time-frame would depends on how 'random' the GUID generation process is.Chief
@GeorgeK I think you misunderstood... It would take 10000 machines 111 years to have a 1% chance of encountering a duplicate. But yes, this math ofcourse assumes that the random generator is totally random.Yetah
And suddenly, it does't seem that "universally" unique at all.Wilding
A
10

From http://www.guidgenerator.com/online-guid-generator.aspx

What is a GUID?

GUID (or UUID) is an acronym for 'Globally Unique Identifier' (or 'Universally Unique Identifier'). It is a 128-bit integer number used to identify resources. The term GUID is generally used by developers working with Microsoft technologies, while UUID is used everywhere else.

How unique is a GUID?

128-bits is big enough and the generation algorithm is unique enough that if 1,000,000,000 GUIDs per second were generated for 1 year the probability of a duplicate would be only 50%. Or if every human on Earth generated 600,000,000 GUIDs there would only be a 50% probability of a duplicate.

Auxesis answered 9/5, 2014 at 17:45 Comment(2)
isn't a 50% chance of a duplicate high enough to cause fear?Tranship
@Tranship yeah it's enough to cause fear if your systems are generating 1 billion GUIDs per second. In the extremely unlikely event you are generating that amount then just chain two GUIDs together...Cockspur
R
9

Is a GUID unique 100% of the time?

Not guaranteed, since there are several ways of generating one. However, you can try to calculate the chance of creating two GUIDs that are identical and you get the idea: a GUID has 128 bits, hence, there are 2128 distinct GUIDs – much more than there are stars in the known universe. Read the wikipedia article for more details.

Roselba answered 2/9, 2008 at 15:20 Comment(0)
A
8

I experienced a duplicate GUID.

I use the Neat Receipts desktop scanner and it comes with proprietary database software. The software has a sync to cloud feature, and I kept getting an error upon syncing. A gander at the logs revealed the awesome line:

"errors":[{"code":1,"message":"creator_guid: is already taken","guid":"C83E5734-D77A-4B09-B8C1-9623CAC7B167"}]}

I was a bit in disbelief, but surely enough, when I found a way into my local neatworks database and deleted the record containing that GUID, the error stopped occurring.

So to answer your question with anecdotal evidence, no. A duplicate is possible. But it is likely that the reason it happened wasn't due to chance, but due to standard practice not being adhered to in some way. (I am just not that lucky) However, I cannot say for sure. It isn't my software.

Their customer support was EXTREMELY courteous and helpful, but they must have never encountered this issue before because after 3+ hours on the phone with them, they didn't find the solution. (FWIW, I am very impressed by Neat, and this glitch, however frustrating, didn't change my opinion of their product.)

Animate answered 8/3, 2013 at 19:1 Comment(1)
Don't believe you got a duplicate. There was probably something else involved, like number was not truly random or problem in the sync process, or system tried to record twice, etc. A software issue is much more likely than you getting a duplicate GUID.Tahsildar
C
7

MSDN:

There is a very low probability that the value of the new Guid is all zeroes or equal to any other Guid.

Cromlech answered 2/9, 2008 at 15:20 Comment(0)
I
6

If your system clock is set properly and hasn't wrapped around, and if your NIC has its own MAC (i.e. you haven't set a custom MAC) and your NIC vendor has not been recycling MACs (which they are not supposed to do but which has been known to occur), and if your system's GUID generation function is properly implemented, then your system will never generate duplicate GUIDs.

If everyone on earth who is generating GUIDs follows those rules then your GUIDs will be globally unique.

In practice, the number of people who break the rules is low, and their GUIDs are unlikely to "escape". Conflicts are statistically improbable.

Idolla answered 2/9, 2008 at 16:16 Comment(2)
This would only be true for v1 guids. The v4, which is the de facto STD no longer uses Mac addresses but a pseudo random number.Suint
"then your system will never generate duplicate GUIDs" Even if all the rules were followed for a v1 guid as you say, your system could still generate duplicates. You are more correct at the bottom when you state "Conflicts are statistically improbable."Rrhoea
E
3

GUID algorithms are usually implemented according to the v4 GUID specification, which is essentially a pseudo-random string. Sadly, these fall into the category of "likely non-unique", from Wikipedia (I don't know why so many people ignore this bit): "... other GUID versions have different uniqueness properties and probabilities, ranging from guaranteed uniqueness to likely non-uniqueness."

The pseudo-random properties of V8's JavaScript Math.random() are TERRIBLE at uniqueness, with collisions often coming after only a few thousand iterations, but V8 isn't the only culprit. I've seen real-world GUID collisions using both PHP and Ruby implementations of v4 GUIDs.

Because it's becoming more and more common to scale ID generation across multiple clients, and clusters of servers, entropy takes a big hit -- the chances of the same random seed being used to generate an ID escalate (time is often used as a random seed in pseudo-random generators), and GUID collisions escalate from "likely non-unique" to "very likely to cause lots of trouble".

To solve this problem, I set out to create an ID algorithm that could scale safely, and make better guarantees against collision. It does so by using the timestamp, an in-memory client counter, client fingerprint, and random characters. The combination of factors creates an additive complexity that is particularly resistant to collision, even if you scale it across a number of hosts:

http://usecuid.org/

Esquivel answered 27/6, 2013 at 6:39 Comment(0)
C
3

I have experienced the GUIDs not being unique during multi-threaded/multi-process unit-testing (too?). I guess that has to do with, all other tings being equal, the identical seeding (or lack of seeding) of pseudo random generators. I was using it for generating unique file names. I found the OS is much better at doing that :)

Trolling alert

You ask if GUIDs are 100% unique. That depends on the number of GUIDs it must be unique among. As the number of GUIDs approach infinity, the probability for duplicate GUIDs approach 100%.

Chavannes answered 24/8, 2017 at 12:46 Comment(1)
I experienced that too, but if I talk about it I get ridiculed :DTreatise
R
3

I think that when people bury their thoughts and fears in statistics, they tend to forget the obvious. If a system is truly random, then the result you are least likely to expect (all ones, say) is equally as likely as any other unexpected value (all zeros, say). Neither fact prevents these occurring in succession, nor within the first pair of samples (even though that would be statistically "truly shocking"). And that's the problem with measuring chance: it ignores criticality (and rotten luck) entirely.

IF it ever happened, what's the outcome? Does your software stop working? Does someone get injured? Does someone die? Does the world explode?

The more extreme the criticality, the worse the word "probability" sits in the mouth. In the end, chaining GUIDs (or XORing them, or whatever) is what you do when you regard (subjectively) your particular criticality (and your feeling of "luckiness") to be unacceptable. And if it could end the world, then please on behalf of all of us not involved in nuclear experiments in the Large Hadron Collider, don't use GUIDs or anything else indeterministic!

Remarque answered 26/11, 2021 at 14:34 Comment(0)
C
2

In a more general sense, this is known as the "birthday problem" or "birthday paradox". Wikipedia has a pretty good overview at: Wikipedia - Birthday Problem

In very rough terms, the square root of the size of the pool is a rough approximation of when you can expect a 50% chance of a duplicate. The article includes a probability table of pool size and various probabilities, including a row for 2^128. So for a 1% probability of collision you would expect to randomly pick 2.6*10^18 128-bit numbers. A 50% chance requires 2.2*10^19 picks, while SQRT(2^128) is 1.8*10^19.

Of course, that is just the ideal case of a truly random process. As others mentioned, a lot is riding on the that random aspect - just how good is the generator and seed? It would be nice if there was some hardware support to assist with this process which would be more bullet-proof except that anything can be spoofed or virtualized. I suspect that might be the reason why MAC addresses/time-stamps are no longer incorporated.

Castigate answered 22/9, 2017 at 1:8 Comment(1)
I think the MAC problem was anonymity. I believe using an identifier such as a MAC address in a way that could be reversed was a privacy concern. I believe true random in hardware is very difficult? Cloudflare uses a camera and a row of lava lamps, however I think that with a precise understanding of physics, even that is not random? Cloudflares lava lamp RNG: popularmechanics.com/technology/security/news/a28921/…Spectacles
W
2

The Answer of "Is a GUID is 100% unique?" is simply "No" .

  • If You want 100% uniqueness of GUID then do following.

    1. generate GUID
    2. check if that GUID is Exist in your table column where you are looking for uniquensess
    3. if exist then goto step 1 else step 4
    4. use this GUID as unique.
Wilhelmina answered 14/8, 2018 at 11:55 Comment(2)
This does not make it unique. Your algorithm does not save the newly created GUID in the table. The next time you create a GUID it could collide with one before. If you were to insert the GUID to the table, the GUID could already had been inserted by another peer in between you checked for uniqueness and you inserted the GUID into the table. The GUID is only unique within YOUR system, so if you were to import or merge two databases they could still collide. Also GUID are often used when you do not have access to a centrilized database. If you had why not just pull an ID from the database?Postbellum
I set a unique index on the column and handle the DB exception. And before anyone thinks that exceptions waste valuable computing time: Well, given the probability, I think it's a justifiable investment.Housefather
C
2

For more better result the best way is to append the GUID with the timestamp (Just to make sure that it stays unique)

Guid.NewGuid().ToString() + DateTime.Now.ToString();
Chiekochien answered 20/8, 2019 at 9:57 Comment(4)
What if you get two collisions in the same second?Vigesimal
That's the worst-case but still, we can't have the same two Guid's generated at the same time.Chiekochien
Somewhere they argue, that one should copy from the answer on SO, not the question, but I am not so sure now....Ombudsman
How about Guid.NewGuid().ToString().Replace("-", "") + DateTime.Now.Ticks .... Not questionable about uniqueness and can be used as a primary keyMissilery
S
0

The hardest part is not about generating a duplicated Guid.

The hardest part is designed a database to store all of the generated ones to check if it is actually duplicated.

From WIKI:

For example, the number of random version 4 UUIDs which need to be generated in order to have a 50% probability of at least one collision is 2.71 quintillion, computed as follows:

enter image description here

This number is equivalent to generating 1 billion UUIDs per second for about 85 years, and a file containing this many UUIDs, at 16 bytes per UUID, would be about 45 exabytes, many times larger than the largest databases currently in existence, which are on the order of hundreds of petabytes

Sphenoid answered 9/1, 2019 at 10:9 Comment(0)
G
0

GUID stands for Global Unique Identifier

In Brief: (the clue is in the name)

In Detail: GUIDs are designed to be unique; they are calculated using a random method based on the computers clock and computer itself, if you are creating many GUIDs at the same millisecond on the same machine it is possible they may match but for almost all normal operations they should be considered unique.

Glycoprotein answered 3/4, 2019 at 22:59 Comment(0)
E
-1

So incredibly low as to be almost 0, but never 0. Ask me how I know... I actually came here to find out just how improbable it really was because I just discovered there are two pieces of data who share the same GUID in one of my dbs...

Effete answered 12/10, 2023 at 14:41 Comment(1)
Please don't duplicate existing answersTetreault
H
-2

Enough GUIDs to assign one to each and every hypothetical grain of sand on every hypothetical planet around each and every star in the visible universe.

Enough so that if every computer in the world generates 1000 GUIDs a second for 200 years, there might (MIGHT) be a collision.

Given the number of current local uses for GUIDs (one sequence per table per database for instance) it is extraordinarily unlikely to ever be a problem for us limited creatures (and machines with lifetimes that are usually less than a decade if not a year or two for mobile phones).

... Can we close this thread now?

Horehound answered 9/8, 2021 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.