dogbane
dogbane

Reputation: 274602

Java 7: ThreadLocalRandom generating the same random numbers

I'm trying out Java 7's ThreadLocalRandom and see that it is generating exactly the same random numbers across multiple threads.

Here is my code, in which I create 5 threads and each thread prints out 5 random numbers:

//5 threads
for(int i = 0; i < 5 ; i++) {
    final Thread thread = new Thread() {
        @Override
        public void run() {
            System.out.print(Thread.currentThread().getName()+":");

            //each thread prints 5 random numbers
            for(int j = 0 ; j < 5; j++) {
                final int random = ThreadLocalRandom.current().nextInt(1,100);
                System.out.print(random + ",");
            }
            System.out.println();
        }
    };
    thread.start();
    thread.join();
}

Output:

Thread-0:1,93,45,75,29,
Thread-1:1,93,45,75,29,
Thread-2:1,93,45,75,29,
Thread-3:1,93,45,75,29,
Thread-4:1,93,45,75,29,

Why am I getting the same random numbers for each thread and for every execution of the program?

Upvotes: 16

Views: 8292

Answers (3)

ratchet freak
ratchet freak

Reputation: 48196

googling for the "ThreadLocalRandom source" gave me http://www.assembla.com/code/scala-eclipse-toolchain/git/nodes/src/forkjoin/scala/concurrent/forkjoin/ThreadLocalRandom.java

long/short of it: it uses a ThreadLocal<ThreadLocalRandom> which calls the no-arg constructor for construction

that no-arg constructor is

/**
 * Constructor called only by localRandom.initialValue.
 * We rely on the fact that the superclass no-arg constructor
 * invokes setSeed exactly once to initialize.
 */
ThreadLocalRandom() {
    super();
}

the no-arg super in Random calls this(long) with a unique seed

HOWEVER that constructor does

public Random(long seed) {
    this.seed = new AtomicLong(initialScramble(seed));
}

i.e. not the expected behavior from documentation

and ThreadLocalRandom doesn't/can't use the private seed

Upvotes: 5

MByD
MByD

Reputation: 137322

Seems like there's an open bug regarding this issue. See here and here

Upvotes: 11

Matt
Matt

Reputation: 5684

Isn't this because the threads are being created at roughly the same time and thus getting seeded the same value from the timer? I was under the impression that was how that worked, though I may be mistaken.

Upvotes: 1

Related Questions