Several of the examples here create a new Random
instance, but this is unnecessary. There is also no reason to use synchronized
as one solution does. Instead, take advantage of the methods on the ThreadLocalRandom
class:
double randomGenerator() {
return ThreadLocalRandom.current().nextDouble(0.5);
}