How to generate all possible 64 bit random values in java?

醉酒当歌 提交于 2021-01-27 12:50:56

问题


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.


回答1:


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.

This class provides a cryptographically strong random number generator (RNG).

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.

this uses s 48 bit algorighm

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.

Related link How to solve slow Java `SecureRandom`? due to using /dev/random



来源:https://stackoverflow.com/questions/51696514/how-to-generate-all-possible-64-bit-random-values-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!