问题描述
考虑到Java SecureRandom.nextLong()
是否从仅使用48位的 Random
继承,它是否返回所有可能的值?如果没有,我是否仍可以通过修改Random类以及如何做到这一点在Java中完成呢?我只想使用一个全随机的长数生成器,如果可能的话,可以返回所有可能的长值.
Does Java SecureRandom.nextLong()
return all possible values given it inherits from Random
which uses only 48 bits? If not, can I still do it in Java maybe by modifying the Random class and how to do it? I just want to use an all random long number generator where all possible long values can be returned, if possible.
推荐答案
尽管SecureRandom继承自Random,但它使用的数学方式不同或具有相同的限制.最终将产生所有可能的64位值.
While SecureRandom inherits from Random, it doesn't use the same maths or have the same limitation. It will produce all possible 64-bit values eventually.
该类委托许多可能的实现之一.您可以通过调用 SecureRandom.getInstance(algorithm)
This class delegates to one of many possible implementations. You can select one by calling SecureRandom.getInstance(algorithm)
注意:某些实现使用计算机中的熵使结果随机,而不是纯伪随机.
Note: some implementations use entropy in your computer to make the results random, rather than purely pseudo random.
SecureRandom不使用其父级的任何方法,例如
SecureRandom doesn't use any of the methods of it's parent e.g.
/**
* The provider implementation.
*/
private SecureRandomSpi secureRandomSpi = null;
public void nextBytes(byte[] bytes) {
secureRandomSpi.engineNextBytes(bytes);
}
此方法委托一个完全不同的实现.
This method delegates to a completely different implementation.
相关链接如何解决缓慢的Java"SecureRandom"问题?使用/dev/random
Related link How to solve slow Java `SecureRandom`? due to using /dev/random
这篇关于如何在Java中生成所有可能的64位随机值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!