For benchmarking purposes, I'd like to have a SecureRandom produce deterministic output. Is this possible through the use of a seed (or maybe specification of an algorithm)?
import java.security.SecureRandom;
class TestSecureRandom {
public static void main(String [] args) {
SecureRandom rnd = new SecureRandom();
rnd.setSeed(1);
System.out.println(rnd.nextInt());
}
}
For me, the above program produces different values even though the seed is specified.
The simplest way of doing this is probably to make most of your code only depend on Random
, with an instance being injected (e.g. by passing it into a constructor). That way, for testing purposes you can pass in a simple Random
with a fixed seed - but for real runs where security is required, you can pass in an instance of SecureRandom
.
See more on this question at Stackoverflow