引导字符串与随机字符串
如果我随机生成一个 32 个字符长的字符串,我可以将此字符串用作 GUID 来实现所有意图和目的吗?
我生成的“GUID”与“真正的”GUID 相比,发生冲突的可能性更大还是更小?
任何有关 GUID 以及它们如何与随机字符串进行比较的更具体信息都值得赞赏。
If I randomly generate a string of 32 characters-long can I use this string as a GUID for all intents and purposes?
Will the "GUID" I generate have more or less likelihood of collision than a "real" GUID?
Any more specific info on GUIDs and how they compare to random strings is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
GUID 生成算法考虑日期和时间以及生成随机数创建最终的 128 位值。
如果您只是生成随机字符串而不添加任何其他算法,那么是的,您将面临更大的碰撞风险。 (计算机无法创建真正的随机数,因此其他数据必须合并到 GUID 生成算法中以降低冲突风险。例如,GUID v1 使用计算机的 MAC 地址,尽管该方法已被弃用,因为它标识了生成计算机。)
您可以创建您自己的 GUID 值,但为什么要重新发明已经运行良好的东西呢?
另外,请参阅 Eric Lippert 的回答,了解为什么使用 GUID 优于使用您自己自制的随机 ID 生成器。
GUID-generation algorithms take into account the date and time as well as generating random numbers to create the final 128 bit value.
If you simply generate random strings w/o any other algorithmics thrown in then yes, you will run a much greater risk of collision. (Computers cannot create truly random numbers so other data has to folded into the GUID gen algorithms to lower risk of collision. GUID v1 for example used a computer's MAC address though that approach has been deprecated since it identifies the generating computer.)
You could create your own GUID value but why reinvent something that already works well?
Also, see Eric Lippert's answer as to why using a GUID is superior to using your own, home-brewed random ID generator.
GUID 不是 32 个字符的长字符串。所以不,您不能使用它来代替 GUID。
根据编码的不同,一个字符可以是一个或两个字节,因此 32 个字符可以是 32 字节或 64 字节。 GUID 是 16 字节。如果生成器中具有同等数量的随机性,则字符串产生碰撞的机会就会减少。话虽如此,16 字节中发生冲突的可能性很小。
关键是您必须至少拥有与 Guid 生成器一样好的生成器才能使其物有所值。当你这样做时,请申请专利。
A GUID is not a 32-character long string. So no, you cannot use it in place of a GUID.
Depending on the encoding, a char can be either one or two bytes, so 32 chars can be 32 bytes or 64 bytes. A GUID is 16 bytes. If you have an equivalent amount of randomness in your generator, your string will produce less chance of collision. Saying that, the chance of collision in 16 bytes is pretty unlikely as it is.
The clinch is that you have to have at least as good a generator as the Guid generator to make it worthwhile. When you do that, patent it.
取决于您要比较的 GUID:现在大多数 GUID 都是“版本 4”,这实际上只是一个带有一些浪费位的大随机数。因此,只要您的随机数生成器与用于生成 GUID 的随机数生成器一样好,您的解决方案就会更加独特。
如果它是版本 1 GUID,那么它可能比随机数更唯一(假设它按预期使用:系统时钟不经常重置,系统有网卡,并且 MAC 地址尚未被重置)被篡改)但大多数人不再使用版本 1,因为它泄露了您的 MAC 地址。
Depends on the GUID you're comparing it against: nowadays most GUIDs are "Version 4", which is really just a big random number with some wasted bits. So as long as your random number generator is as good as the one used to generate the GUID, your solution is more unique.
If it's a Version 1 GUID, then it's probably more unique than a random number (assuming it's being used as expected: the system clock isn't being reset very often, the system has a network card, and the MAC address hasn't been tampered with) but most people don't use version 1 anymore because it leaks your MAC address.
这取决于您将使用的算法。如果你有好的发电机,结果会是一样的。
可能性取决于两个生成器的性能(您的生成器与 GUID 生成器)。
It depends on algorithm which you will use. If you have good generator the result would be the same.
The likelihood depends on how good both the generators are (yours vs. GUID one).
我建议使用实际的 guid。您的随机字符串生成器唯一的可能性远远小于 GUI。
I would suggest to use actual guid's. The chances that your random string generator would be unique is far less than that of a guid.
社交 MSDN 给出信息很少,但没有回答您是否更有可能发生碰撞的问题。
Guid Structure 告诉 GUID 不是字符串,而是“A GUID 是一个 128 位整数(16 字节),可以在所有需要唯一标识符的计算机和网络中使用,这样的标识符被重复的可能性非常低。”
Social MSDN gives little info, but doesn't answer your question whether a collision is more likely or not.
Guid Structure tells a GUID is not a string but "A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated."