GUID 100%是唯一的吗?

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


当前回答

是的,GUID应该总是唯一的。它是基于硬件和时间,加上一些额外的位,以确保它是唯一的。我相信理论上有可能得到两个一模一样的,但在现实场景中极不可能。

以下是Raymond Chen关于Guids的一篇很棒的文章:

https://blogs.msdn.com/oldnewthing/archive/2008/06/27/8659071.aspx ​ ​ ​

其他回答

如果你的系统时钟设置正确,没有被环绕,如果你的网卡有自己的MAC(即你没有设置自定义MAC),你的网卡供应商没有回收MAC(他们不应该这样做,但已经知道发生了),如果你的系统的GUID生成功能正确实现,那么你的系统将永远不会生成重复的GUID。

如果地球上每个生成guid的人都遵循这些规则,那么您的guid将是全局唯一的。

在实践中,违反规则的人数很少,他们的guid不太可能“逃脱”。冲突在统计上是不可能发生的。

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或任何其他不确定的东西!

为了获得更好的结果,最好的方法是将GUID与时间戳附加在一起(只是为了确保它保持唯一)

Guid.NewGuid().ToString() + DateTime.Now.ToString();

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指南,第三部分

足够的guid来为可见宇宙中每一颗恒星周围的每一颗假设行星上的每一颗假设沙粒分配一个guid。

以至于如果世界上的每台计算机每秒生成1000个guid,持续200年,就可能发生碰撞。

考虑到当前guid的本地使用数量(例如,每个数据库每个表一个序列),这对于我们这些有限的生物来说是非常不可能成为问题的(对于手机来说,寿命通常不到十年,如果不是一两年的话)。

... 我们现在可以结束这个话题了吗?