I'm looking to use (seeded) Random
objects across multiple threads, and the javadocs pointed me to ThreadLocalRandom
which looks great except I can't set the seed, so I can't ensure consistency among different threads or runs. Is there any practical reason to use ThreadLocalRandom
or would it be acceptable to do something like the following:
// Pass returned ThreadLocal object to all threads which need it
public static ThreadLocal<Random> threadRandom(final long seed) {
return new ThreadLocal<Random>(){
@Override
protected Random initialValue() {
return new Random(seed);
}
};
}