Is a GUID unique 100% of the time?

前端 未结 22 2473
孤独总比滥情好
孤独总比滥情好 2020-11-22 01:31

Is a GUID unique 100% of the time?

Will it stay unique over multiple threads?

相关标签:
22条回答
  • 2020-11-22 02:12

    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.

    0 讨论(0)
  • 2020-11-22 02:15

    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.

    0 讨论(0)
  • 2020-11-22 02:15

    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.

    0 讨论(0)
  • 2020-11-22 02:16

    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.
    0 讨论(0)
  • 2020-11-22 02:17

    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.)

    0 讨论(0)
  • 2020-11-22 02:18

    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.

    0 讨论(0)
提交回复
热议问题