问题
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