简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM