GUID 100%是唯一的吗?
它会在多个线程中保持唯一吗?
GUID 100%是唯一的吗?
它会在多个线程中保持唯一吗?
当前回答
Eric Lippert写了一系列关于guid的非常有趣的文章。
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.
GUID指南,第一部分 GUID指南,第二部分 GUID指南,第三部分
其他回答
顺便说一句,我在Windows XP中使用了Volume guid。这是一个非常模糊的分区布局,有3个磁盘和14个卷。
\\?\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
这并不是说guid非常相似,而是所有的guid都有字符串“mario”。这是巧合还是另有原因?
现在,当在GUID中搜索第4部分时,我发现大约有125,000个与音量GUID相关的点击。
结论:当涉及到Volume guid时,它们并不像其他guid那样独特。
“GUID是100%唯一的吗?”的答案是“不是”。
如果你想要GUID的100%唯一性,然后做下面的事情。 生成GUID 检查GUID是否存在于您正在寻找唯一性的表列中 如果存在,则转步骤1,否则转步骤4 使用这个GUID作为唯一的。
在多线程/多进程单元测试期间,我经历过guid不是唯一的(也是?)我想这与所有其他条件相同的情况下,伪随机生成器的相同播种(或缺乏播种)有关。我用它来生成唯一的文件名。我发现操作系统在这方面做得更好:)
恶意破坏预警
你问guid是否100%唯一。这取决于它在guid中必须是唯一的。当guid的数量接近无穷大时,重复guid的概率接近100%。
GUID 100%是唯一的吗?
不能保证,因为有几种方法可以生成一个。然而,您可以尝试计算创建两个完全相同的GUID的几率,您就会明白:一个GUID有128位,因此,有2128个不同的GUID——比已知宇宙中的恒星要多得多。阅读维基百科的文章了解更多细节。
这种情况不应该发生。然而,当. net负载过重时,可能会得到重复的guid。我有两个不同的web服务器使用两个不同的sql服务器。我去合并数据,发现我有1500万个guids和7个副本。