Charles Follet
Charles Follet

Reputation: 817

ThreadLocalRandom setSeed

Is it possible to provide a Seed to a ThreadLocalRandom?

It looks like it isn't.

/**
 * Throws {@code UnsupportedOperationException}.  Setting seeds in
 * this generator is not supported.
 *
 * @throws UnsupportedOperationException always
 */
public void setSeed(long seed) {
    if (initialized)
        throw new UnsupportedOperationException();
    rnd = (seed ^ multiplier) & mask;
}

So can we use ThreadLocalRandom using seed or it's not designed for that?

Upvotes: 8

Views: 5449

Answers (1)

fidekild
fidekild

Reputation: 191

As @Marko Topolnik commented, ThreadLocalRandom does not allow to set your own seed. You can bypass this problem by using a ThreadLocal<Random>, as discussed in this question.

Upvotes: 15

Related Questions