Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate all possible 64 bit random values in 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.

like image 788
mj1261829 Avatar asked Dec 17 '25 19:12

mj1261829


1 Answers

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

like image 51
Peter Lawrey Avatar answered Dec 19 '25 13:12

Peter Lawrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!