<p>Is a GUID unique 100% of the time?</p>

<p>Will it stay unique over multiple threads?</p>

[{'answer_id': 39776, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 10, 'selected': True, 'text': '<blockquote>\n<p>While each generated GUID is not\nguaranteed to be unique, the total\nnumber of unique keys (2<sup>128</sup> or\n3.4×10<sup>38</sup>) is so large that the probability of the same number being\ngenerated twice is very small. For\nexample, consider the observable\nuniverse, which contains about 5×10<sup>22</sup>\nstars; every star could then have\n6.8×10<sup>15</sup> universally unique GUIDs.</p>\n</blockquote>\n<p>From <a href="https://en.wikipedia.org/wiki/Globally_Unique_Identifier" rel="noreferrer">Wikipedia</a>.</p>\n<hr />\n<p>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.</p>\n<p><a href="https://ericlippert.com/2012/04/24/guid-guide-part-one/" rel="noreferrer">https://ericlippert.com/2012/04/24/guid-guide-part-one/</a></p>\n<p><a href="https://ericlippert.com/2012/04/30/guid-guide-part-two/" rel="noreferrer">https://ericlippert.com/2012/04/30/guid-guide-part-two/</a></p>\n<p><a href="https://ericlippert.com/2012/05/07/guid-guide-part-three/" rel="noreferrer">https://ericlippert.com/2012/05/07/guid-guide-part-three/</a></p>\n<p>\u200b\u200b</p>\n'}, {'answer_id': 39779, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 5, 'selected': False, 'text': '<p>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.  </p>\n\n<p>Here\'s a great article by Raymond Chen on Guids: </p>\n\n<p><a href="https://blogs.msdn.com/oldnewthing/archive/2008/06/27/8659071.aspx" rel="noreferrer">https://blogs.msdn.com/oldnewthing/archive/2008/06/27/8659071.aspx</a>\n\u200b\n\u200b\n\u200b</p>\n'}, {'answer_id': 39781, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 6, 'selected': False, 'text': '<p>The simple answer is yes.</p>\n\n<p>Raymond Chen wrote a <a href="https://devblogs.microsoft.com/oldnewthing/20080627-00/?p=21823" rel="noreferrer">great article</a> on GUIDs and why substrings of GUIDs are <em>not</em> 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 <em>why</em> they are :-)</p>\n'}, {'answer_id': 39782, 'author': 'Jakub Šturc', 'author_id': 2361, 'author_profile': 'https://Stackoverflow.com/users/2361', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx" rel="noreferrer">MSDN</a>: </p>\n\n<blockquote>\n  <p>There is a very low probability that the value of the new Guid is all zeroes or equal to any other Guid. </p>\n</blockquote>\n'}, {'answer_id': 39784, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>Is a GUID unique 100% of the time?</p>\n</blockquote>\n\n<p>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 2<sup>128</sup> distinct GUIDs – <strong>much</strong> more than there are stars in the known universe. Read the <a href="http://en.wikipedia.org/wiki/GUID" rel="noreferrer">wikipedia article</a> for more details.</p>\n'}, {'answer_id': 39788, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 5, 'selected': False, 'text': '<p>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.</p>\n\n<p>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?</p>\n'}, {'answer_id': 39803, 'author': 'Michael Haren', 'author_id': 29, 'author_profile': 'https://Stackoverflow.com/users/29', 'pm_score': 5, 'selected': False, 'text': '<p>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.</p>\n\n<p>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. <em>anything</em> else).</p>\n\n<p>I <a href="http://mharen.blogspot.com/2008/01/pick-number-any-number.html" rel="noreferrer">made an attempt</a> 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:</p>\n\n<ul>\n<li>1 in 2^128</li>\n<li>1 in 340 undecillion (don’t worry, undecillion is not on the\nquiz)</li>\n<li>1 in 3.4 × 10^38</li>\n<li>1 in 340,000,000,000,000,000,000,000,000,000,000,000,000</li>\n</ul>\n'}, {'answer_id': 39924, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n\n<p>If everyone on earth who is generating GUIDs follows those rules then your GUIDs will be globally unique.</p>\n\n<p>In practice, the number of people who break the rules is low, and their GUIDs are unlikely to "escape".  Conflicts are statistically improbable.</p>\n'}, {'answer_id': 2062754, 'author': 'Jonas Engström', 'author_id': 7634, 'author_profile': 'https://Stackoverflow.com/users/7634', 'pm_score': 6, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>\\\\?\\Volume{23005604-eb1b-11de-85ba-806d6172696f}\\ (F:)\n\\\\?\\Volume{23005605-eb1b-11de-85ba-806d6172696f}\\ (G:)\n\\\\?\\Volume{23005606-eb1b-11de-85ba-806d6172696f}\\ (H:)\n\\\\?\\Volume{23005607-eb1b-11de-85ba-806d6172696f}\\ (J:)\n\\\\?\\Volume{23005608-eb1b-11de-85ba-806d6172696f}\\ (D:)\n\\\\?\\Volume{23005609-eb1b-11de-85ba-806d6172696f}\\ (P:)\n\\\\?\\Volume{2300560b-eb1b-11de-85ba-806d6172696f}\\ (K:)\n\\\\?\\Volume{2300560c-eb1b-11de-85ba-806d6172696f}\\ (L:)\n\\\\?\\Volume{2300560d-eb1b-11de-85ba-806d6172696f}\\ (M:)\n\\\\?\\Volume{2300560e-eb1b-11de-85ba-806d6172696f}\\ (N:)\n\\\\?\\Volume{2300560f-eb1b-11de-85ba-806d6172696f}\\ (O:)\n\\\\?\\Volume{23005610-eb1b-11de-85ba-806d6172696f}\\ (E:)\n\\\\?\\Volume{23005611-eb1b-11de-85ba-806d6172696f}\\ (R:)\n                                     | | | | |\n                                     | | | | +-- 6f = o\n                                     | | | +---- 69 = i\n                                     | | +------ 72 = r\n                                     | +-------- 61 = a\n                                     +---------- 6d = m\n</code></pre>\n\n<p>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?</p>\n\n<p>Now, when <a href="http://www.google.se/search?hl=sv&amp;rlz=1C1CHMB_enSE353SE353&amp;q=806d6172696f&amp;btnG=S%C3%B6k&amp;meta=&amp;aq=f&amp;oq=" rel="noreferrer">googling for part 4</a> in the GUID I found approx 125.000 hits with volume GUIDs.</p>\n\n<p><strong>Conclusion:</strong> When it comes to Volume GUIDs they aren\'t as unique as other GUIDs.</p>\n'}, {'answer_id': 2166066, 'author': 'Tim', 'author_id': 262234, 'author_profile': 'https://Stackoverflow.com/users/262234', 'pm_score': 5, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 10951301, 'author': 'Paolo Moretti', 'author_id': 63011, 'author_profile': 'https://Stackoverflow.com/users/63011', 'pm_score': 5, 'selected': False, 'text': '<p>Eric Lippert has written a very interesting series of articles about GUIDs.</p>\n\n<blockquote>\n  <p>There are on the order 2<sup>30</sup> personal computers in the world (and of\n  course lots of hand-held devices or non-PC computing devices that have\n  more or less the same levels of computing power, but lets ignore\n  those). Let\'s assume that we put all those PCs in the world to the\n  task of generating GUIDs; if each one can generate, say, 2<sup>20</sup> GUIDs per\n  second then after only about 2<sup>72</sup> seconds -- <strong>one hundred and fifty\n  trillion years</strong> -- you\'ll have a <em>very high</em> chance of generating a\n  collision with your specific GUID. And the odds of collision get\n  pretty good after only thirty trillion years.</p>\n</blockquote>\n\n<ul>\n<li><a href="https://ericlippert.com/2012/04/24/guid-guide-part-one/" rel="noreferrer">GUID Guide, part one</a></li>\n<li><a href="https://ericlippert.com/2012/04/30/guid-guide-part-two/" rel="noreferrer">GUID Guide, part two</a></li>\n<li><a href="https://ericlippert.com/2012/05/07/guid-guide-part-three/" rel="noreferrer">GUID Guide, part three</a></li>\n</ul>\n'}, {'answer_id': 15301346, 'author': 'exintrovert', 'author_id': 2149629, 'author_profile': 'https://Stackoverflow.com/users/2149629', 'pm_score': 3, 'selected': False, 'text': '<p>I experienced a duplicate GUID.</p>\n\n<p>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:</p>\n\n<blockquote>\n  <p>"errors":[{"code":1,"message":"creator_guid: is already\n  taken","guid":"C83E5734-D77A-4B09-B8C1-9623CAC7B167"}]}</p>\n</blockquote>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>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.)</p>\n'}, {'answer_id': 17336173, 'author': 'Eric Elliott', 'author_id': 1043390, 'author_profile': 'https://Stackoverflow.com/users/1043390', 'pm_score': 2, 'selected': False, 'text': '<p>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 <strong>"likely non-unique"</strong>, 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."</p>\n\n<p>The pseudo-random properties of V8\'s JavaScript <code>Math.random()</code> 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.</p>\n\n<p>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".</p>\n\n<p>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:</p>\n\n<p><a href="http://usecuid.org/" rel="nofollow">http://usecuid.org/</a></p>\n'}, {'answer_id': 23570942, 'author': 'Tono Nam', 'author_id': 637142, 'author_profile': 'https://Stackoverflow.com/users/637142', 'pm_score': 3, 'selected': False, 'text': '<p>From <a href="http://www.guidgenerator.com/online-guid-generator.aspx" rel="nofollow noreferrer">http://www.guidgenerator.com/online-guid-generator.aspx</a></p>\n<blockquote>\n<p><strong>What is a GUID?</strong></p>\n<p>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.</p>\n<p><strong>How unique is a GUID?</strong></p>\n<p>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.</p>\n</blockquote>\n'}, {'answer_id': 23571255, 'author': 'Bura Chuhadar', 'author_id': 2493887, 'author_profile': 'https://Stackoverflow.com/users/2493887', 'pm_score': 7, 'selected': False, 'text': '<p>If you are scared of the same GUID values then put two of them next to each other. </p>\n\n<pre><code>Guid.NewGuid().ToString() + Guid.NewGuid().ToString();\n</code></pre>\n\n<p>If you are too paranoid then put three.</p>\n'}, {'answer_id': 41446264, 'author': 'Cine', 'author_id': 264022, 'author_profile': 'https://Stackoverflow.com/users/264022', 'pm_score': 4, 'selected': False, 'text': '<p>None seems to mention the actual math of the probability of it occurring.</p>\n\n<p>First, let\'s assume we can use the entire 128 bit space (Guid v4 only uses 122 bits).</p>\n\n<p>We know that the general probability of NOT getting a duplicate in <code>n</code> picks is:</p>\n\n<blockquote>\n  <p>(1-1/2<sup>128</sup>)(1-2/2<sup>128</sup>)...(1-(n-1)/2<sup>128</sup>)</p>\n</blockquote>\n\n<p>Because 2<sup>128</sup> is much much larger than <code>n</code>, we can approximate this to:</p>\n\n<blockquote>\n  <p>(1-1/2<sup>128</sup>)<sup>n(n-1)/2</sup></p>\n</blockquote>\n\n<p>And because we can assume <code>n</code> is much much larger than 0, we can approximate that to:</p>\n\n<blockquote>\n  <p>(1-1/2<sup>128</sup>)<sup>n^2/2</sup></p>\n</blockquote>\n\n<p>Now we can equate this to the "acceptable" probability, let\'s say 1%:</p>\n\n<blockquote>\n  <p>(1-1/2<sup>128</sup>)<sup>n^2/2</sup> = 0.01</p>\n</blockquote>\n\n<p>Which we solve for <code>n</code> and get:</p>\n\n<blockquote>\n  <p>n = sqrt(2* log 0.01 / log (1-1/2<sup>128</sup>))</p>\n</blockquote>\n\n<p>Which Wolfram Alpha gets to be <strong>5.598318 × 10<sup>19</sup></strong></p>\n\n<p>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.</p>\n'}, {'answer_id': 45862052, 'author': 'Robert Jørgensgaard Engdahl', 'author_id': 2154774, 'author_profile': 'https://Stackoverflow.com/users/2154774', 'pm_score': 2, 'selected': False, 'text': '<p>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 :)</p>\n\n<h2>Trolling alert</h2>\n\n<p>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%.</p>\n'}, {'answer_id': 46355377, 'author': 'mszil', 'author_id': 6549903, 'author_profile': 'https://Stackoverflow.com/users/6549903', 'pm_score': 2, 'selected': False, 'text': '<p>In a more general sense, this is known as the "birthday problem" or "birthday paradox". Wikipedia has a pretty good overview at: \n<a href="https://en.wikipedia.org/wiki/Birthday_problem" rel="nofollow noreferrer">Wikipedia - Birthday Problem</a></p>\n\n<p>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.</p>\n\n<p>Of course, that is just the ideal case of a truly random process. As others mentioned, a lot is riding on the that <em>random</em> 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.</p>\n'}, {'answer_id': 51840846, 'author': 'Baba Khedkar', 'author_id': 6814735, 'author_profile': 'https://Stackoverflow.com/users/6814735', 'pm_score': 2, 'selected': False, 'text': '<p>The Answer of <em>"Is a GUID is 100% unique?"</em> is simply <em>"No"</em> .</p>\n\n<ul>\n<li><p>If You want 100% uniqueness of GUID then do following.</p>\n\n<ol>\n<li>generate GUID</li>\n<li>check if that GUID is Exist in your table column where you are looking for uniquensess</li>\n<li>if exist then goto step 1 else step 4</li>\n<li>use this GUID as unique.</li>\n</ol></li>\n</ul>\n'}, {'answer_id': 54107635, 'author': 'Trong Hiep Le', 'author_id': 10627940, 'author_profile': 'https://Stackoverflow.com/users/10627940', 'pm_score': 0, 'selected': False, 'text': '<p>The hardest part is not about generating a duplicated Guid.</p>\n\n<p>The hardest part is designed a database to store all of the generated ones to check if it is actually duplicated.</p>\n\n<p>From WIKI:</p>\n\n<p>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:</p>\n\n<p><a href="https://i.stack.imgur.com/bYidE.png" rel="nofollow noreferrer">enter image description here</a></p>\n\n<p>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</p>\n'}, {'answer_id': 55505523, 'author': 'Benjamin Roberts', 'author_id': 5243317, 'author_profile': 'https://Stackoverflow.com/users/5243317', 'pm_score': 0, 'selected': False, 'text': '<p>GUID stands for Global Unique Identifier </p>\n\n<p>In Brief:\n(the clue is in the name)</p>\n\n<p>In Detail:\nGUIDs 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.</p>\n'}, {'answer_id': 57571121, 'author': 'Adithya Sai', 'author_id': 3229075, 'author_profile': 'https://Stackoverflow.com/users/3229075', 'pm_score': 2, 'selected': False, 'text': '<p>For more better result the best way is to append the GUID with the timestamp (Just to make sure that it stays unique)</p>\n\n<pre><code>Guid.NewGuid().ToString() + DateTime.Now.ToString();\n</code></pre>\n'}, {'answer_id': 68716351, 'author': 'William M. Rawls', 'author_id': 1109022, 'author_profile': 'https://Stackoverflow.com/users/1109022', 'pm_score': -1, 'selected': False, 'text': '<p>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.</p>\n<p>Enough so that if every computer in the world generates 1000 GUIDs a second for 200 years, there might (MIGHT) be a collision.</p>\n<p>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).</p>\n<p>... Can we close this thread now?</p>\n'}, {'answer_id': 70126049, 'author': 'Alex T', 'author_id': 2658643, 'author_profile': 'https://Stackoverflow.com/users/2658643', 'pm_score': 0, 'selected': False, 'text': "<p>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 &quot;truly shocking&quot;).  And that's the problem with measuring chance: it ignores criticality (and rotten luck) entirely.</p>\n<p>IF it ever happened, what's the outcome?  Does your software stop working?  Does someone get injured?  Does someone die?  Does the world explode?</p>\n<p>The more extreme the criticality, the worse the word &quot;probability&quot; 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 &quot;luckiness&quot;) 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!</p>\n"}]
['https://Stackoverflow.com/questions/39771', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2469/']