val n = 100
Random.nextInt(n)
ThreadLocalRandom.current().nextInt(n)
scala.util.Random#nextInt
uses AtomicInteger
at java.util.Random#next
for generating thread-safe random.
On the one hand, ThreadLocalRandom
overrides next
method to make it non thread-safe and faster.
protected int next(int bits) {
long oldseed, nextseed;
AtomicLong seed = this.seed;
do {
oldseed = seed.get();
nextseed = (oldseed * multiplier + addend) & mask;
} while (!seed.compareAndSet(oldseed, nextseed));
return (int)(nextseed >>> (48 - bits));
}
protected int next(int bits) {
this.rnd = this.rnd * 25214903917L + 11L & 281474976710655L;
return (int)(this.rnd >>> 48 - bits);
}