8

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?

fidekild
  • 161
  • 3
  • 13
Charles Follet
  • 827
  • 1
  • 10
  • 28
  • 2
    Why look at implementation when the public specification already gives you the answer? `Like the global {@link java.util.Random} generator used by the {@link java.lang.Math} class, a {@code ThreadLocalRandom} is initialized with an internally generated seed that may not otherwise be modified.` – Marko Topolnik Oct 03 '16 at 13:57
  • yes, you are right. Should have read it more carefully. – Charles Follet Oct 03 '16 at 14:11

1 Answers1

12

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.

fidekild
  • 161
  • 3
  • 13