Java randomUUID 是否提供唯一 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20999792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does randomUUID give a unique id?
提问by birdy
I am trying to create session tokens for my REST API. Each time the user logs in I am creating a new token by
我正在尝试为我的 REST API 创建会话令牌。每次用户登录时,我都会创建一个新令牌
UUID token = UUID.randomUUID();
user.setSessionId(token.toString());
Sessions.INSTANCE.sessions.put(user.getName(), user.getSessionId());
However, I am not sure how to protect against duplicate sessionTokens.
但是,我不确定如何防止重复的 sessionToken。
For example: Can there be a scenario when user1 signs in and gets a token 87955dc9-d2ca-4f79-b7c8-b0223a32532a
and user2 signs in and alsogets a token 87955dc9-d2ca-4f79-b7c8-b0223a32532a
.
例如:难道还有一个场景时user1的迹象,并且获得令牌87955dc9-d2ca-4f79-b7c8-b0223a32532a
和user2迹象,并也得到了令牌87955dc9-d2ca-4f79-b7c8-b0223a32532a
。
Is there a better way of doing this?
有没有更好的方法来做到这一点?
采纳答案by Tom G
If you get a UUID collision, go play the lottery next.
如果您遇到 UUID 冲突,请接着玩彩票。
From Wikipedia:
来自维基百科:
Randomly generated UUIDs have 122 random bits. Out of a total of 128 bits, four bits are used for the version ('Randomly generated UUID'), and two bits for the variant ('Leach-Salz').
With random UUIDs, the chance of two having the same value can be calculated using probability theory (Birthday paradox). Using the approximation
p(n)\approx 1-e^{-\tfrac{n^2}{{2x}}}
these are the probabilities of an accidental clash after calculating n UUIDs, with x=2122:
n probability 68,719,476,736 = 236 0.0000000000000004 (4 × 10?16) 2,199,023,255,552 = 241 0.0000000000004 (4 × 10?13) 70,368,744,177,664 = 246 0.0000000004 (4 × 10?10)
To put these numbers into perspective, the annual risk of someone being hit by a meteorite is estimated to be one chance in 17 billion, which means the probability is about 0.00000000006 (6 × 10?11), equivalent to the odds of creating a few tens of trillions of > UUIDs in a year and having one duplicate. In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs.
随机生成的 UUID 有 122 个随机位。在总共 128 位中,四位用于版本('随机生成的 UUID'),两位用于变体('Leach-Salz')。
对于随机 UUID,可以使用概率论(生日悖论)计算两个具有相同值的机会。使用近似值
p(n)\approx 1-e^{-\tfrac{n^2}{{2x}}}
这些是计算 n 个 UUID 后发生意外碰撞的概率,其中 x=2122:
n 概率 68,719,476,736 = 236 0.0000000000000004 (4 × 10?16) 2,199,023,255,552 = 241 0.0000000000004 (4), 7000000000004 (4), 7000000000004 (4), 70000000004) (4), 70000000004)
从这些数字来看,每年有人被陨石击中的风险估计为 170 亿分之一,这意味着概率约为 0.00000000006 (6 × 10?11),相当于创造几个的几率一年内有数十万亿个 > UUID 并且有一个重复。换句话说,只有在接下来的 100 年中每秒生成 10 亿个 UUID 之后,仅创建一个重复项的概率约为 50%。如果地球上每个人都拥有 6 亿个 UUID,一个重复的概率约为 50%。
回答by SethB
Oracle UUID document. http://docs.oracle.com/javase/7/docs/api/java/util/UUID.html
Oracle UUID 文档。http://docs.oracle.com/javase/7/docs/api/java/util/UUID.html
They use this algorithm from the The Internet Engineering Task Force. http://www.ietf.org/rfc/rfc4122.txt
他们使用来自互联网工程任务组的这个算法。http://www.ietf.org/rfc/rfc4122.txt
A quote from the abstract.
摘自摘要。
A UUID is 128 bits long, and can guaranteeuniqueness across space and time.
UUID 的长度为 128 位,可以保证跨空间和时间的唯一性。
While the abstract claims a guarantee, there are only 3.4 x 10^38
combinations. CodeChimp
虽然摘要要求保证,但只有3.4 x 10^38
组合。代码黑猩猩
回答by rgoers
Since a UUID has a finite size there is no way for it to be unique across all of space and time.
由于 UUID 的大小是有限的,因此它不可能在所有空间和时间中都是唯一的。
If you need a UUID that is guaranteed to be unique within any reasonable use case you can use Log4j 2's Uuid.getTimeBasedUuid(). It is guaranteed to be unique for about 8,900 years so long as you generate less than 10,000 UUIDs per millisecond.
如果您需要在任何合理用例中保证唯一的 UUID,您可以使用 Log4j 2 的 Uuid.getTimeBasedUuid()。只要每毫秒生成的 UUID 少于 10,000 个,它就可以保证在大约 8,900 年内是唯一的。