【发布时间】:2019-07-03 16:33:38
【问题描述】:
每个人。
我有一个简单的问题。 UUID.randomUUID().getMostSignificantBits() 返回的值是否总是唯一的?
我需要生成一个需要始终唯一的 Long 值。有没有其他方法可以生成一个 Long 来完成这个?
谢谢。
【问题讨论】:
-
return counter++?当然,您不能期望超过 2^64 个唯一长值。 -
Yes, it is theoretically possible for the UUID.randomUUID method to return a duplicate, but this is not at all a realistic concern. The Oracle/OpenJDK implementation uses a cryptographically-strong random number generator. Given that, and given the astronomical range given by so many bits in a UUID, you can generate many millions of such values in your app and still sleep well. Using one of the other variants further reduces the possibility of collisions even closer to zero because of using "space and time", [1] MAC address or name, and [2] current date-time, as constraints. -
@YassinHajaj 对于完整的 UUID 是正确的,但对于截断的 UUID 则不然。由于生日悖论,即使使用完美的 64 位随机数生成器,您也希望在生成 ~2^32 个数字后看到重复。
-
另外,这个问题显然不是它作为副本被关闭的问题的副本。
-
@MikeFHay 如果您觉得应该重新打开它,请投票支持重新打开。但我似乎很清楚它是重复的,因为它回答了第一个问题。第二个问题:还有其他方法吗,可能没有。
标签: java unique uuid long-integer