1. Overview
1.概述
The SecureRandom class, found in the java.security package, is specifically designed for cryptographic purposes and critical security situations, using algorithms that ensure a high level of unpredictability.
安全随机类位于java.security包中,是专为加密目的和关键安全情况而设计的,使用的算法可确保高度的不可预测性。
In this tutorial, we’ll discuss generating a unique positive long value using SecureRandom and explore how safe it is from collisions when generating multiple values.
在本教程中,我们将讨论使用 SecureRandom 生成唯一的正长值,并探讨在生成多个值时如何避免碰撞。
2. Using nextLong()
2.使用 nextLong()
The nextLong() method of SecureRandom returns a value of type long that is a random 64-bit number. These values are randomly spread over an extremely wide range of values, from Long.MIN_VALUE (-2^63) to Long.MAX_VALUE (2^63 – 1).
SecureRandom 的 nextLong() 方法会返回一个 64 位随机数的 long 类型值。这些值随机分布在极宽的数值范围内,从 Long.MIN_VALUE (-2^63) 到 Long.MAX_VALUE (2^63 – 1)。
This method is inherited from the Random class. However, it is guaranteed to be safer in terms of the collision probability due to using a higher number of seed bits.
该方法继承自 Random 类。不过,由于使用了更多的种子比特,它在碰撞概率方面更安全。
Under the hood, it uses pseudo-random number generator (PRNG), also known as deterministic random bits generator or DRBG, in combination with an entropy source that is provided by the operating system.
在引擎盖下,它使用 伪随机数生成器 (PRNG),也称为确定性随机比特生成器或 DRBG,并结合操作系统提供的熵源。
Let’s see how we can use it to generate a random long value:
让我们看看如何使用它来生成一个随机 长值:
new SecureRandom().nextLong();
If we print a few results from this method call, we might see output like:
如果我们打印这个方法调用的一些结果,我们可能会看到类似的输出:
4926557902899092186
-2282075914544479463
-4653180235857827604
6589027106659854836
So, if we only need positive values, then we need to additionally use Math.abs():
因此,如果我们只需要正值,则需要额外使用 Math.abs() :
SecureRandom secureRandom = new SecureRandom();
long randomPositiveLong = Math.abs(secureRandom.nextLong());
In this way, the results are guaranteed to always be positive:
这样,就能保证结果始终是积极的:
assertThat(randomPositiveLong).isNotNegative();
3. Collision Probability
3.碰撞概率
Because we need to generate unique values, it is important to ensure the probability of a collision is sufficiently low.
由于我们需要生成唯一值,因此必须确保碰撞的概率足够低。
As we noted above, the nextLong() method generates a 64-bit random long value ranging from -2^63 to 2^63 – 1. Subsequently, by applying Math.abs(), we eliminate any negative sign. Therefore, the randomness of the 0 to 2^63 range is reduced by a factor of 2. Consequently, the probability of collision is calculated as 1 / 2^62. In decimal form, this probability is approximately 0.000000000000000000216840434497100900. For most practical applications, we can consider this low probability to be insignificant.
如上所述,nextLong() 方法会生成一个从 -2^63 到 2^63 – 1 的 64 位随机 long 值。随后,通过应用 Math.abs() 方法,我们消除了任何负号。因此,0 至 2^63 范围内的随机性降低了 2 倍。 因此,碰撞概率的计算结果为 1 / 2^62。以十进制形式表示,该概率约为 0.00000000000000216840434497100900。对于大多数实际应用,我们可以认为这种低概率是微不足道的。
Assuming it generates one value per second, on average, only one collision would occur in a period of approximately (2^62) / (60) / (60) / (24) / (365.25) years, or around 146,135,511,523 years. This extended timeframe further underscores the rarity of collision events.
假设它每秒产生一个值,那么平均而言,在大约 (2^62) / (60) / (60) / (24) / (365.25) 年的时间内,即大约 146,135,511,523 年,只会发生一次碰撞。这一延长的时间范围进一步强调了碰撞事件的罕见性。
4. Conclusion
4.结论
In this article, we’ve discussed how to generate unique positive long values with SecureRandom. This approach is considered effective because it ensures a high level of unpredictability, and the probability of collision is insignificant for most applications, so it is suitable for use in a wide variety of circumstances.
在本文中,我们讨论了如何使用 SecureRandom 生成唯一的正长值。这种方法被认为是有效的,因为它确保了高度的不可预测性,而且对于大多数应用来说,碰撞的概率微乎其微,因此适合在各种情况下使用。
As always, the full source code is available over on GitHub.
与往常一样,完整的源代码可在 GitHub 上获取。