GUID 100%是唯一的吗?

它会在多个线程中保持唯一吗?


当前回答

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.

如果真的发生了,结果是什么?你的软件停止工作了吗?有人受伤了吗?有人死了吗?世界会爆炸吗?

临界性越极端,“概率”这个词就越难听。最后,链接guid(或XORing它们,或其他什么)是当你(主观地)认为你的特定临界性(和你的“幸运”感觉)是不可接受的时候所做的事情。如果它能毁灭世界,那么请代表我们所有没有参与大型强子对撞机核试验的人,不要使用guid或任何其他不确定的东西!

其他回答

简单的答案是肯定的。

Raymond Chen写了一篇关于guid和为什么guid的子字符串不能保证唯一的文章。这篇文章深入探讨了guid的生成方式以及它们用来确保唯一性的数据,这应该会花一些篇幅来解释它们为什么会这样:-)

似乎没有人提到它发生概率的实际数学计算。

首先,让我们假设我们可以使用整个128位空间(Guid v4只使用122位)。

我们知道在n次选择中没有得到重复的一般概率是:

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

因为2128比n大得多,我们可以将其近似为:

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

因为我们可以假设n比0大很多很多,我们可以把它近似为:

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

现在我们可以将其等同于“可接受的”概率,假设是1%:

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

我们解出n,得到

N =√(2* log 0.01 / log (1-1/2128))

哪个Wolfram Alpha得到5.598318 × 1019

为了更好地理解这个数字,让我们以10000台机器为例,每台机器都有一个4核CPU,工作4Ghz,花费10000个周期来生成一个Guid,其他什么都不做。然后需要大约111年才能产生一个副本。

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.

如果真的发生了,结果是什么?你的软件停止工作了吗?有人受伤了吗?有人死了吗?世界会爆炸吗?

临界性越极端,“概率”这个词就越难听。最后,链接guid(或XORing它们,或其他什么)是当你(主观地)认为你的特定临界性(和你的“幸运”感觉)是不可接受的时候所做的事情。如果它能毁灭世界,那么请代表我们所有没有参与大型强子对撞机核试验的人,不要使用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作为唯一的。