Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 227
Charles Follet Avatar asked Oct 03 '16 13:10

Charles Follet


1 Answers

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.

like image 182
fidekild Avatar answered Nov 07 '22 15:11

fidekild