3

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.

The Impaler
  • 45,731
  • 9
  • 39
  • 76
mj1261829
  • 1,200
  • 3
  • 26
  • 53
  • I'm a little confused by the question's wording. I'm guessing you don't want to generate all possible Long values. I'm guessing that you want a random number from the set of all possible Long values? – Software Engineer Aug 05 '18 at 17:46
  • If you truly want a random number (not based on discrete mathematics) you should visit some online sites. For example: [random.org](https://www.random.org/) – zlakad Aug 05 '18 at 17:47
  • 2
    @zlakad unless you want it cryptographically secure in which case you definitely *don't* want to get random data from another source. – Peter Lawrey Aug 05 '18 at 17:55
  • @PeterLawrey, yes, I see what you mean. I wrote an algorithm in assembler for Z80 processor for generations of random bites back in 1982-3. God, I forgot everything about it! – zlakad Aug 05 '18 at 17:58
  • Yes, from the set of all possible long values and not necessary all long values. – mj1261829 Aug 05 '18 at 18:01
  • @zlakad web sites could record your random numbers or be hacked to record them. Why would any on bother? When you have a private keys worth ~$1 bn https://etherscan.io/accounts recording sources of random data becomes interesting to some. – Peter Lawrey Aug 05 '18 at 18:01
  • @PeterLawrey Agreed! – zlakad Aug 05 '18 at 18:05

1 Answers1

7

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

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130