javarandomgeneratorlong-integersecure-random

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.


Solution

  • 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