Is a GUID unique 100% of the time?

前端 未结 22 2475
孤独总比滥情好
孤独总比滥情好 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:32

    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.

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

    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/

    ​​

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

    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?

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

    MSDN:

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

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