如何确保生成的guid全局唯一?

发布于 2024-09-28 05:35:04 字数 142 浏览 5 评论 0原文

假设我想将 guid 设置为我的应用程序的程序集 guid。从网上搜索到,我们可以使用(new Guid()).Next()来获取新的唯一值。

我不明白如何保证我的指南相对于其他指南是独一无二的?如果您知道该怎么做,请解释一下。

Let's say I want to set a guid to be my application's assembly guid. As searched from internet, we can use (new Guid()).Next() to get a new unique value.

I cannot figure out how my guid is warranted to be unique against others? Please explain if you know how to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浊酒尽余欢 2024-10-05 05:35:04

你拥有的唯一保证是概率站在你这一边。 2^128 个可能的 GUID 以及创建过程中的一些巧妙之处使得您不太可能看到重复的内容。

看来 V4 是现在 Windows 上的标准 GUID。如果这个纯粹基于伪随机数生成器,正如维基百科似乎表明的那样,它会受到生日的影响问题

我见过几个使用 128 位的示例,表明重复几乎是不可能的。这些人常常会错过两件事。生日问题以及 V4 GUID 实际上是 124 位。

您需要 1/2+sqrt(1/4-2*2^124*ln(0,5)) ≈ 5.4*10^18 GUID 才能达到 50% 的重复几率。这仍然是很多,但 50% 可能不是您正在寻找的交易。假设您希望它是百万分之一才能获得重复项,那么您可以使用 sqrt(2*2^124*ln(1/(1-0,000001))) ≈ 6, 5*10^15 GUID。如果您每秒创建 1000 个 GUID,那么您可以继续这样做近 206667 年,然后才达到百万分之一的重复风险。 6,52191054316287e15/(3600*24*365,25*1000) ≈ 206666,874006986

我获得所有这些计算正确的机会→0。

The only guarantee you have is that probability is on your side. 2^128 possible GUIDs and some cleverness in the creation process makes it very unlikely you will ever see a duplicate.

It seems V4 is the standard GUID on Windows now. If that one is purely based on a pseudo-random number generator, as Wikipedia seems to indicate, it's affected by the Birthday problem.

I've seen several examples using 128-bits to show that a duplicate is almost impossible. Those often miss two things. The Birthday problem and that a V4 GUID actually is 124 bits.

You need 1/2+sqrt(1/4-2*2^124*ln(0,5)) ≈ 5.4*10^18 GUIDs to reach a 50% chance of a duplicate. That is still a lot, but 50% may not be the deal you are looking for. Say you want it to be one in a million to get a duplicate, then you can have sqrt(2*2^124*ln(1/(1-0,000001))) ≈ 6,5*10^15 GUIDs. If you create a thousand GUIDs per second you could keep on doing that for almost 206667 years before reaching a one to a million risk of getting a duplicate. 6,52191054316287e15/(3600*24*365,25*1000) ≈ 206666,874006986

The chance of me getting all of those calculations correct →0.

梨涡 2024-10-05 05:35:04

事实并非如此,但它的生成方式和表示方式使得在这个世纪生成两个相同 GUID 的概率几乎为零。

请参阅:GUID 不唯一的简单证明

It isnt, but the way it is generated and the way it is represented makes probability of generating two same GUIDs in this milenium almost zero.

See: Simple proof that GUID is not unique

慕巷 2024-10-05 05:35:04

来自 http://en.wikipedia.org/wiki/Globally_unique_identifier

算法

OSF 指定的 在生成新 (V1) GUID 的算法中,用户的网卡 MAC 地址用作最后一组 GUID 数字的基础,这意味着可以将文档追溯到创建它的计算机。这个隐私漏洞在定位 Melissa 蠕虫病毒的创建者时被利用[2]。大多数其他数字基于生成 GUID 时的时间。

包含MAC地址和时间的V1 GUID可以通过第三组数字的第一个位置中的数字“1”来标识,例如{2f1e4fc0-81fd-11da-9156-00036a0f876a}。

V4 GUID 使用后来的算法,它是一个伪随机数。它们的相同位置有一个“4”,例如 {38a52be4-9352-453e-af97-5c3b448652f0}。更具体地说,“data3”位模式在第一种情况下为 0001xxxxxxxxxxxx,在第二种情况下为 0100xxxxxxxxxxxx。对WinAPI GUID生成器的密码分析表明,由于V4 GUID的序列是伪随机的;如果充分了解内部状态,就可以预测先前和后续的值。[3]。

From http://en.wikipedia.org/wiki/Globally_unique_identifier:

Algorithm

In the OSF-specified algorithm for generating new (V1) GUIDs, the user's network card MAC address is used as a base for the last group of GUID digits, which means, for example, that a document can be tracked back to the computer that created it. This privacy hole was used when locating the creator of the Melissa worm[2]. Most of the other digits are based on the time while generating the GUID.

V1 GUIDs which contain a MAC address and time can be identified by the digit "1" in the first position of the third group of digits, for example {2f1e4fc0-81fd-11da-9156-00036a0f876a}.

V4 GUIDs use the later algorithm, which is a pseudo-random number. These have a "4" in the same position, for example {38a52be4-9352-453e-af97-5c3b448652f0}. More specifically, the 'data3' bit pattern would be 0001xxxxxxxxxxxx in the first case, and 0100xxxxxxxxxxxx in the second. Cryptanalysis of the WinAPI GUID generator shows that, since the sequence of V4 GUIDs is pseudo-random; given full knowledge of the internal state, it is possible to predict previous and subsequent values.[3].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文