jeudi 20 février 2020

Should SecureRandom be used as singleton or a new object should be created each time random number is generated?

I am using SecureRandom to generate a random numbers.

Does it make any difference with respect to predictability of next number generated if the SecureRandom object is a singleton or a new object is created every time a random number is generated?

Singleton:

public RequestIdGenerator {
    private static SecureRandom secureRandom = new SecureRandom();

    public static int generateRequestId() {
        secureRandom.nextInt(100_000_000);
    }
}

vs.

New object for each time random number is generated:

public RequestIdGenerator {

    public static int generateRequestId() {
        new SecureRandom().nextInt(100_000_000);
    }
}

This question arose after reading about this answer related to 'Predictability of Linear Congruential Generators'.




Aucun commentaire:

Enregistrer un commentaire