假设 GUID 总是唯一的是否安全? [英] Is it safe to assume a GUID will always be unique?

查看:20
本文介绍了假设 GUID 总是唯一的是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道发生冲突的可能性很小,但如果我生成了一批 1000 个 GUID(例如),是否可以假设它们都是唯一的以保存每个 GUID 的测试?

I know there is a minute possibility of a clash but if I generated a batch of 1000 GUIDs (for example), would it be safe to assume they're all unique to save testing each one?

奖励问题

测试 GUID 唯一性的最佳方法是什么?布隆过滤器可能吗?

An optimal way to test a GUID for uniqueness? Bloom filter maybe?

推荐答案

是的,你可以.由于 GUID 的长度为 128 位,因此无可否认存在发生冲突的微小可能性——但分钟"一词还远远不够强大.有这么多的 GUID,如果你随机生成 数万亿个 GUID,你仍然更有可能被陨石击中,而不是一次碰撞(来自 维基百科).如果您不是随机生成它们,而是例如使用 MAC 地址和时间戳算法,那么它们也将是唯一的,因为 MAC 地址在计算机中是唯一的和时间戳在您的计算机上是唯一的.

Yes, you can. Since GUIDs are 128 bits long, there is admittedly a minute possibility of a clash—but the word "minute" is nowhere near strong enough. There are so many GUIDs that if you generate several trillion of them randomly, you're still more likely to get hit by a meteorite than to have even one collision (from Wikipedia). And if you aren't generating them randomly, but are e.g. using the MAC-address-and-time-stamp algorithm, then they're also going to be unique, as MAC addresses are unique among computers and time stamps are unique on your computer.

编辑 1:要回答您的额外问题,测试一组 GUID 的唯一性的最佳方法是假设它们都是唯一的.为什么?因为,考虑到您生成的 GUID 数量,GUID 碰撞的几率小于宇宙射线在您的计算机内存中翻转一点并搞砸您关心的任何准确"算法给出的答案的几率跑步.(参见这个 StackOverflow 答案 用于数学.)

Edit 1: To answer your bonus question, the optimal way to test a set of GUIDs for uniqueness is to just assume that they are all are unique. Why? Because, given the number of GUIDs you're generating, the odds of a GUID collision are smaller than the odds of a cosmic ray flipping a bit in your computer's memory and screwing up the answer given by any "accurate" algorithm you'd care to run. (See this StackOverflow answer for the math.)

有大量大量的 GUID.引用道格拉斯·亚当斯的银河系漫游指南:

There are an enormous number of GUIDs out there. To quote Douglas Adams's Hitchhiker's Guide to the Galaxy:

空间,"它说,很大.真的很大.你不会相信它有多么大得令人难以置信.我的意思是你可能认为通往化学家的道路很长,但这只是花生到太空,听着……"

"Space," it says, "is big. Really big. You just won't believe how vastly hugely mindbogglingly big it is. I mean you may think it's a long way down the road to the chemist, but that's just peanuts to space, listen…"

而且由于宇宙中有 大约 7×1022 颗恒星,不到 2128 个 GUID,那么每个 GUID 大约有 4.86×1015——几乎五千万——GUID星星.如果这些恒星中的每一个都有一个像我们这样人口激增的世界,那么在每一颗恒星周围,每个曾经生活过的人类或外星人 将有权获得超过四万五千个 GUID.为历史上的每一个人,在宇宙中的每一颗恒星.GUID 空间与整个宇宙的大小处于同一级别.你不必担心.

And since there are about 7×1022 stars in the universe, and just under 2128 GUIDs, then there are approximately 4.86×1015—almost five quadrillion—GUIDs for every single star. If every one of those stars had a world with a thriving population like ours, then around each and every star, every human or alien who had ever lived would be entitled to over forty-five thousand GUIDs. For every person in history at every star in the universe. The GUID space is at the same level of hugeness as the size of the entire universe. You do not need to worry.

(编辑 2: 反思:哇.我没有意识到我自己这意味着什么.GUID 空间大得难以理解.我有点敬畏它.)

(Edit 2: Reflecting on this: wow. I hadn't realized myself what this meant. The GUID space is incomprehensibly massive. I'm sort of in awe of it.)

这篇关于假设 GUID 总是唯一的是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆