0

Here is my annotated code.

class RandomThing{

// This is one way to do it, but I want more randomness.
    protected final static Random rand = new Random();

// This is how I do it with SecureRandom.
    protected final static Random rand = new SecureRandom();

// Here is the best way I can get a strong instance, but rand is no longer final.
    protected static Random rand = null;

    static {
        try {
            rand = SecureRandom.getInstanceStrong();
        } catch (NoSuchAlgorithmException ex) {
            // Blow up.
        }
    }
}

How can I have something like this?

protected final static Random rand = SecureRandom.getInstanceStrong();
// not okay because getInstanceStrong throws NoSuchAlgorithmException
Simon Kuang
  • 3,870
  • 4
  • 27
  • 53

0 Answers0