Top Description Inners Fields Constructors Methods
java.util.concurrent

public final Class ThreadLocalRandom

extends Random
Class Inheritance
Annotations
@RandomGeneratorProperties
name:ThreadLocalRandom
i:64
j:0
k:0
equidistribution:1
Imports
java.io.ObjectStreamField, java.security.AccessControlContext, java.util.Random, java.util.concurrent.atomic.AtomicInteger, .AtomicLong, java.util.stream.DoubleStream, .IntStream, .LongStream, jdk.internal.access.JavaUtilConcurrentTLRAccess, .SharedSecrets, jdk.internal.util.random.RandomSupport, jdk.internal.util.random.RandomSupport.*, jdk.internal.misc.Unsafe, .VM

A random number generator (with period 264) isolated to the current thread. Like the global java.util.Random generator used by the java.lang.Math class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified. When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention. Use of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask) use random numbers in parallel in thread pools.

Usages of this class should typically be of the form: ThreadLocalRandom.current().nextX(...) (where X is Int, Long, etc). When all usages are of this form, it is never possible to accidentally share a ThreadLocalRandom across multiple threads.

This class also provides additional commonly used bounded random generation methods.

Instances of ThreadLocalRandom are not cryptographically secure. Consider instead using java.security.SecureRandom in security-sensitive applications. Additionally, default-constructed instances do not use a cryptographically random seed unless the system property java.util.secureRandomSeed is set to true.

Author
Doug Lea
Since
1.7

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class

Field Summary

Modifier and TypeField and Description
private static final long
GOLDEN_GAMMA

The seed increment.

private static final long
private static final long
pack-priv boolean
initialized

Field used only during singleton initialization.

private static final ThreadLocalRandom
instance

The common ThreadLocalRandom

private static final long
private static final int
PROBE_INCREMENT

The increment for generating probe values.

private static final AtomicInteger
probeGenerator

Generates per-thread initialization/probe field

private static final long
private static final long
private static final AtomicLong
seeder

The next seed for default constructors.

private static final long
SEEDER_INCREMENT

The increment of seeder per new instance.

private static final ObjectStreamField[]
private static final long
private static final long
private static final Unsafe
U

Constructor Summary

AccessConstructor and Description
private
ThreadLocalRandom()

Constructor used only for static singleton

Method Summary

Modifier and TypeMethod and Description
pack-priv static final int
advanceProbe(int probe)

Pseudo-randomly advances and records the given probe value for the given thread.

public static ThreadLocalRandom

Returns:

the current thread's ThreadLocalRandom
current
()

Returns the current thread's ThreadLocalRandom object.

public DoubleStream
doubles(long
the number of values to generate
streamSize
)

Overrides java.util.Random.doubles.

Overrides default java.util.random.RandomGenerator.doubles.

Returns a stream producing the given streamSize number of pseudorandom double values, each between zero (inclusive) and one (exclusive).
public DoubleStream
doubles()

Overrides java.util.Random.doubles.

Overrides default java.util.random.RandomGenerator.doubles.

Returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).
public DoubleStream
doubles(long
the number of values to generate
streamSize
,
double
the origin (inclusive) of each random value
randomNumberOrigin
,
double
the bound (exclusive) of each random value
randomNumberBound
)

Overrides java.util.Random.doubles.

Overrides default java.util.random.RandomGenerator.doubles.

Returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
public DoubleStream
doubles(double
the origin (inclusive) of each random value
randomNumberOrigin
,
double
the bound (exclusive) of each random value
randomNumberBound
)

Overrides java.util.Random.doubles.

Overrides default java.util.random.RandomGenerator.doubles.

Returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
pack-priv static final void
eraseThreadLocals(Thread thread)

Erases ThreadLocals by nulling out Thread maps.

pack-priv static final int
getProbe()

Returns the probe value for the current thread without forcing initialization.

public IntStream
ints(long
the number of values to generate
streamSize
)

Overrides java.util.Random.ints.

Overrides default java.util.random.RandomGenerator.ints.

Returns a stream producing the given streamSize number of pseudorandom int values.
public IntStream
ints()

Overrides java.util.Random.ints.

Overrides default java.util.random.RandomGenerator.ints.

Returns an effectively unlimited stream of pseudorandom int values.
public IntStream
ints(long
the number of values to generate
streamSize
,
int
the origin (inclusive) of each random value
randomNumberOrigin
,
int
the bound (exclusive) of each random value
randomNumberBound
)

Overrides java.util.Random.ints.

Overrides default java.util.random.RandomGenerator.ints.

Returns a stream producing the given streamSize number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
public IntStream
ints(int
the origin (inclusive) of each random value
randomNumberOrigin
,
int
the bound (exclusive) of each random value
randomNumberBound
)

Overrides java.util.Random.ints.

Overrides default java.util.random.RandomGenerator.ints.

Returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
pack-priv static final void
localInit()

Initialize Thread fields for the current thread.

public LongStream
longs(long
the number of values to generate
streamSize
)

Overrides java.util.Random.longs.

Overrides default java.util.random.RandomGenerator.longs.

Returns a stream producing the given streamSize number of pseudorandom long values.
public LongStream
longs()

Overrides java.util.Random.longs.

Overrides default java.util.random.RandomGenerator.longs.

Returns an effectively unlimited stream of pseudorandom long values.
public LongStream
longs(long
the number of values to generate
streamSize
,
long
the origin (inclusive) of each random value
randomNumberOrigin
,
long
the bound (exclusive) of each random value
randomNumberBound
)

Overrides java.util.Random.longs.

Overrides default java.util.random.RandomGenerator.longs.

Returns a stream producing the given streamSize number of pseudorandom long, each conforming to the given origin (inclusive) and bound (exclusive).
public LongStream
longs(long
the origin (inclusive) of each random value
randomNumberOrigin
,
long
the bound (exclusive) of each random value
randomNumberBound
)

Overrides java.util.Random.longs.

Overrides default java.util.random.RandomGenerator.longs.

Returns an effectively unlimited stream of pseudorandom long values, each conforming to the given origin (inclusive) and bound (exclusive).
private static int
mix32(long z)

protected int

Returns:

the next pseudorandom value from this random number generator's sequence
next
(int
random bits
bits
)

Overrides java.util.Random.next.

Generates a pseudorandom number with the indicated number of low-order bits.
public boolean
nextBoolean()

Overrides java.util.Random.nextBoolean.

Overrides default java.util.random.RandomGenerator.nextBoolean.

Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
public double
nextDouble()

Overrides java.util.Random.nextDouble.

Overrides default java.util.random.RandomGenerator.nextDouble.

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
public double
nextDouble(double
the upper bound (exclusive) for the returned value. Must be positive and finite
bound
)

Overrides default java.util.random.RandomGenerator.nextDouble.

Returns a pseudorandomly chosen double value between zero (inclusive) and the specified bound (exclusive).
public double
nextDouble(double
the least value that can be returned
origin
,
double
the upper bound (exclusive) for the returned value
bound
)

Overrides default java.util.random.RandomGenerator.nextDouble.

Returns a pseudorandomly chosen double value between the specified origin (inclusive) and the specified bound (exclusive).
public float
nextFloat()

Overrides java.util.Random.nextFloat.

Overrides default java.util.random.RandomGenerator.nextFloat.

Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.
public float
nextFloat(float
the upper bound (exclusive) for the returned value. Must be positive and finite
bound
)

Overrides default java.util.random.RandomGenerator.nextFloat.

Returns a pseudorandomly chosen float value between zero (inclusive) and the specified bound (exclusive).
public float
nextFloat(float
the least value that can be returned
origin
,
float
the upper bound (exclusive)
bound
)

Overrides default java.util.random.RandomGenerator.nextFloat.

Returns a pseudorandomly chosen float value between the specified origin (inclusive) and the specified bound (exclusive).
public int
nextInt()

Overrides java.util.Random.nextInt.

Overrides default java.util.random.RandomGenerator.nextInt.

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
public int
nextInt(int
the upper bound (exclusive). Must be positive.
bound
)

Overrides java.util.Random.nextInt.

Overrides default java.util.random.RandomGenerator.nextInt.

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
public int
nextInt(int
the least value that can be returned
origin
,
int
the upper bound (exclusive) for the returned value
bound
)

Overrides default java.util.random.RandomGenerator.nextInt.

Returns a pseudorandomly chosen int value between the specified origin (inclusive) and the specified bound (exclusive).
public long
nextLong()

Overrides java.util.Random.nextLong.

Implements java.util.random.RandomGenerator.nextLong.

Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
public long
nextLong(long
the upper bound (exclusive) for the returned value. Must be positive.
bound
)

Overrides default java.util.random.RandomGenerator.nextLong.

Returns a pseudorandomly chosen long value between zero (inclusive) and the specified bound (exclusive).
public long
nextLong(long
the least value that can be returned
origin
,
long
the upper bound (exclusive) for the returned value
bound
)

Overrides default java.util.random.RandomGenerator.nextLong.

Returns a pseudorandomly chosen long value between the specified origin (inclusive) and the specified bound (exclusive).
pack-priv static final int
nextSecondarySeed()

Returns the pseudo-randomly initialized or updated secondary seed.

pack-priv final long
nextSeed()

Update the thread local seed value by adding to it the sum of GOLDEN_GAMMA (an odd value) and twice the thread id. This sum is always odd (to guarantee that the generator has maximum period) and is different for different threads.

private Object

Returns:

the current thread's ThreadLocalRandom
readResolve
()

Returns the current thread's ThreadLocalRandom.

pack-priv static final void
setInheritedAccessControlContext(Thread thread, AccessControlContext acc)

Deprecated as a consequence of AccessControlContext being deprecated.
public void
setSeed(long
the seed value
seed
)

Overrides java.util.Random.setSeed.

Throws UnsupportedOperationException.
private void
writeObject(ObjectOutputStream
the stream
s
)

Hides java.util.Random.writeObject.

Saves the ThreadLocalRandom to a stream (that is, serializes it).
Inherited from java.util.Random:
fromnextBytesnextGaussian

Field Detail

GOLDEN_GAMMAback to summary
private static final long GOLDEN_GAMMA

The seed increment. This must be an odd value for the generator to have the maximum period (2 to the 64th power). The value 0x9e3779b97f4a7c15L is odd, and moreover consists of the first 64 bits of the fractional part of the golden ratio, which is known to generate good Weyl sequences.

INHERITABLETHREADLOCALSback to summary
private static final long INHERITABLETHREADLOCALS
INHERITEDACCESSCONTROLCONTEXTback to summary
private static final long INHERITEDACCESSCONTROLCONTEXT
initializedback to summary
pack-priv boolean initialized

Field used only during singleton initialization. True when constructor completes.

instanceback to summary
private static final ThreadLocalRandom instance

The common ThreadLocalRandom

PROBEback to summary
private static final long PROBE
PROBE_INCREMENTback to summary
private static final int PROBE_INCREMENT

The increment for generating probe values.

probeGeneratorback to summary
private static final AtomicInteger probeGenerator

Generates per-thread initialization/probe field

SECONDARYback to summary
private static final long SECONDARY
SEEDback to summary
private static final long SEED
seederback to summary
private static final AtomicLong seeder

The next seed for default constructors.

SEEDER_INCREMENTback to summary
private static final long SEEDER_INCREMENT

The increment of seeder per new instance.

serialPersistentFieldsback to summary
private static final ObjectStreamField[] serialPersistentFields

Hides java.util.Random.serialPersistentFields.

Serial Fields:
rnd:long
seed for random computations
initialized:boolean
always true
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.Random.serialVersionUID.

THREADLOCALSback to summary
private static final long THREADLOCALS
Uback to summary
private static final Unsafe U

Constructor Detail

ThreadLocalRandomback to summary
private ThreadLocalRandom()

Constructor used only for static singleton

Method Detail

advanceProbeback to summary
pack-priv static final int advanceProbe(int probe)

Pseudo-randomly advances and records the given probe value for the given thread.

currentback to summary
public static ThreadLocalRandom current()

Returns the current thread's ThreadLocalRandom object. Methods of this object should be called only by the current thread, not by other threads.

Returns:ThreadLocalRandom

the current thread's ThreadLocalRandom

doublesback to summary
public DoubleStream doubles(long streamSize)

Overrides java.util.Random.doubles.

Overrides default java.util.random.RandomGenerator.doubles.

Doc from java.util.Random.doubles.

Returns a stream producing the given streamSize number of pseudorandom double values, each between zero (inclusive) and one (exclusive).

A pseudorandom double value is generated as if it's the result of calling the method nextDouble().

Parameters
streamSize:long

the number of values to generate

Returns:DoubleStream

a stream of double values

Annotations
@Override
Exceptions
IllegalArgumentException:
if streamSize is less than zero
Since
1.8
doublesback to summary
public DoubleStream doubles()

Overrides java.util.Random.doubles.

Overrides default java.util.random.RandomGenerator.doubles.

Doc from java.util.Random.doubles.

Returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).

A pseudorandom double value is generated as if it's the result of calling the method nextDouble().

Implementation Note

This method is implemented to be equivalent to doubles(Long.MAX_VALUE).

Returns:DoubleStream

a stream of pseudorandom double values

Annotations
@Override
Since
1.8
doublesback to summary
public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)

Overrides java.util.Random.doubles.

Overrides default java.util.random.RandomGenerator.doubles.

Doc from java.util.Random.doubles.

Returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

Parameters
streamSize:long

the number of values to generate

randomNumberOrigin:double

the origin (inclusive) of each random value

randomNumberBound:double

the bound (exclusive) of each random value

Returns:DoubleStream

a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if streamSize is less than zero, or randomNumberOrigin is not finite, or randomNumberBound is not finite, or randomNumberOrigin is greater than or equal to randomNumberBound
Since
1.8
doublesback to summary
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)

Overrides java.util.Random.doubles.

Overrides default java.util.random.RandomGenerator.doubles.

Doc from java.util.Random.doubles.

Returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

Implementation Note

This method is implemented to be equivalent to doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound).

Parameters
randomNumberOrigin:double

the origin (inclusive) of each random value

randomNumberBound:double

the bound (exclusive) of each random value

Returns:DoubleStream

a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if randomNumberOrigin is not finite, or randomNumberBound is not finite, or randomNumberOrigin is greater than or equal to randomNumberBound
Since
1.8
eraseThreadLocalsback to summary
pack-priv static final void eraseThreadLocals(Thread thread)

Erases ThreadLocals by nulling out Thread maps.

getProbeback to summary
pack-priv static final int getProbe()

Returns the probe value for the current thread without forcing initialization. Note that invoking ThreadLocalRandom.current() can be used to force initialization on zero return.

intsback to summary
public IntStream ints(long streamSize)

Overrides java.util.Random.ints.

Overrides default java.util.random.RandomGenerator.ints.

Doc from java.util.Random.ints.

Returns a stream producing the given streamSize number of pseudorandom int values.

A pseudorandom int value is generated as if it's the result of calling the method nextInt().

Parameters
streamSize:long

the number of values to generate

Returns:IntStream

a stream of pseudorandom int values

Annotations
@Override
Exceptions
IllegalArgumentException:
if streamSize is less than zero
Since
1.8
intsback to summary
public IntStream ints()

Overrides java.util.Random.ints.

Overrides default java.util.random.RandomGenerator.ints.

Doc from java.util.Random.ints.

Returns an effectively unlimited stream of pseudorandom int values.

A pseudorandom int value is generated as if it's the result of calling the method nextInt().

Implementation Note

This method is implemented to be equivalent to ints(Long.MAX_VALUE).

Returns:IntStream

a stream of pseudorandom int values

Annotations
@Override
Since
1.8
intsback to summary
public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)

Overrides java.util.Random.ints.

Overrides default java.util.random.RandomGenerator.ints.

Doc from java.util.Random.ints.

Returns a stream producing the given streamSize number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).

A pseudorandom int value is generated as if it's the result of calling the following method with the origin and bound:

 int nextInt(int origin, int bound) {
  int n = bound - origin;
  if (n > 0) {
    return nextInt(n) + origin;
  }
  else {  // range not representable as int
    int r;
    do {
      r = nextInt();
    } while (r < origin || r >= bound);
    return r;
  }
}
Parameters
streamSize:long

the number of values to generate

randomNumberOrigin:int

the origin (inclusive) of each random value

randomNumberBound:int

the bound (exclusive) of each random value

Returns:IntStream

a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if streamSize is less than zero, or randomNumberOrigin is greater than or equal to randomNumberBound
Since
1.8
intsback to summary
public IntStream ints(int randomNumberOrigin, int randomNumberBound)

Overrides java.util.Random.ints.

Overrides default java.util.random.RandomGenerator.ints.

Doc from java.util.Random.ints.

Returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).

A pseudorandom int value is generated as if it's the result of calling the following method with the origin and bound:

 int nextInt(int origin, int bound) {
  int n = bound - origin;
  if (n > 0) {
    return nextInt(n) + origin;
  }
  else {  // range not representable as int
    int r;
    do {
      r = nextInt();
    } while (r < origin || r >= bound);
    return r;
  }
}

Implementation Note

This method is implemented to be equivalent to ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound).

Parameters
randomNumberOrigin:int

the origin (inclusive) of each random value

randomNumberBound:int

the bound (exclusive) of each random value

Returns:IntStream

a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if randomNumberOrigin is greater than or equal to randomNumberBound
Since
1.8
localInitback to summary
pack-priv static final void localInit()

Initialize Thread fields for the current thread. Called only when Thread.threadLocalRandomProbe is zero, indicating that a thread local seed value needs to be generated. Note that even though the initialization is purely thread-local, we need to rely on (static) atomic generators to initialize the values.

longsback to summary
public LongStream longs(long streamSize)

Overrides java.util.Random.longs.

Overrides default java.util.random.RandomGenerator.longs.

Doc from java.util.Random.longs.

Returns a stream producing the given streamSize number of pseudorandom long values.

A pseudorandom long value is generated as if it's the result of calling the method nextLong().

Parameters
streamSize:long

the number of values to generate

Returns:LongStream

a stream of pseudorandom long values

Annotations
@Override
Exceptions
IllegalArgumentException:
if streamSize is less than zero
Since
1.8
longsback to summary
public LongStream longs()

Overrides java.util.Random.longs.

Overrides default java.util.random.RandomGenerator.longs.

Doc from java.util.Random.longs.

Returns an effectively unlimited stream of pseudorandom long values.

A pseudorandom long value is generated as if it's the result of calling the method nextLong().

Implementation Note

This method is implemented to be equivalent to longs(Long.MAX_VALUE).

Returns:LongStream

a stream of pseudorandom long values

Annotations
@Override
Since
1.8
longsback to summary
public LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound)

Overrides java.util.Random.longs.

Overrides default java.util.random.RandomGenerator.longs.

Doc from java.util.Random.longs.

Returns a stream producing the given streamSize number of pseudorandom long, each conforming to the given origin (inclusive) and bound (exclusive).

A pseudorandom long value is generated as if it's the result of calling the following method with the origin and bound:

 long nextLong(long origin, long bound) {
  long r = nextLong();
  long n = bound - origin, m = n - 1;
  if ((n & m) == 0L)  // power of two
    r = (r & m) + origin;
  else if (n > 0L) {  // reject over-represented candidates
    for (long u = r >>> 1;            // ensure nonnegative
         u + m - (r = u % n) < 0L;    // rejection check
         u = nextLong() >>> 1) // retry
        ;
    r += origin;
  }
  else {              // range not representable as long
    while (r < origin || r >= bound)
      r = nextLong();
  }
  return r;
}
Parameters
streamSize:long

the number of values to generate

randomNumberOrigin:long

the origin (inclusive) of each random value

randomNumberBound:long

the bound (exclusive) of each random value

Returns:LongStream

a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if streamSize is less than zero, or randomNumberOrigin is greater than or equal to randomNumberBound
Since
1.8
longsback to summary
public LongStream longs(long randomNumberOrigin, long randomNumberBound)

Overrides java.util.Random.longs.

Overrides default java.util.random.RandomGenerator.longs.

Doc from java.util.Random.longs.

Returns an effectively unlimited stream of pseudorandom long values, each conforming to the given origin (inclusive) and bound (exclusive).

A pseudorandom long value is generated as if it's the result of calling the following method with the origin and bound:

 long nextLong(long origin, long bound) {
  long r = nextLong();
  long n = bound - origin, m = n - 1;
  if ((n & m) == 0L)  // power of two
    r = (r & m) + origin;
  else if (n > 0L) {  // reject over-represented candidates
    for (long u = r >>> 1;            // ensure nonnegative
         u + m - (r = u % n) < 0L;    // rejection check
         u = nextLong() >>> 1) // retry
        ;
    r += origin;
  }
  else {              // range not representable as long
    while (r < origin || r >= bound)
      r = nextLong();
  }
  return r;
}

Implementation Note

This method is implemented to be equivalent to longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound).

Parameters
randomNumberOrigin:long

the origin (inclusive) of each random value

randomNumberBound:long

the bound (exclusive) of each random value

Returns:LongStream

a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if randomNumberOrigin is greater than or equal to randomNumberBound
Since
1.8
mix32back to summary
private static int mix32(long z)
nextback to summary
protected int next(int bits)

Overrides java.util.Random.next.

Generates a pseudorandom number with the indicated number of low-order bits. Because this class has no subclasses, this method cannot be invoked or overridden.

Parameters
bits:int

random bits

Returns:int

the next pseudorandom value from this random number generator's sequence

nextBooleanback to summary
public boolean nextBoolean()

Overrides java.util.Random.nextBoolean.

Overrides default java.util.random.RandomGenerator.nextBoolean.

Doc from java.util.Random.nextBoolean.

Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence. The general contract of nextBoolean is that one boolean value is pseudorandomly generated and returned. The values true and false are produced with (approximately) equal probability.

Returns:boolean

the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence

Annotations
@Override
nextDoubleback to summary
public double nextDouble()

Overrides java.util.Random.nextDouble.

Overrides default java.util.random.RandomGenerator.nextDouble.

Doc from java.util.Random.nextDouble.

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

The general contract of nextDouble is that one double value, chosen (approximately) uniformly from the range 0.0d (inclusive) to 1.0d (exclusive), is pseudorandomly generated and returned.

Returns:double

the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence

Annotations
@Override
nextDoubleback to summary
public double nextDouble(double bound)

Overrides default java.util.random.RandomGenerator.nextDouble.

Doc from java.util.random.RandomGenerator.nextDouble.

Returns a pseudorandomly chosen double value between zero (inclusive) and the specified bound (exclusive).

Implementation Note

The default implementation checks that bound is a positive finite double. Then invokes nextDouble(), scaling the result so that the final result lies between 0.0 (inclusive) and bound (exclusive).

Parameters
bound:double

the upper bound (exclusive) for the returned value. Must be positive and finite

Returns:double

a pseudorandomly chosen double value between zero (inclusive) and the bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if bound is not both positive and finite
nextDoubleback to summary
public double nextDouble(double origin, double bound)

Overrides default java.util.random.RandomGenerator.nextDouble.

Doc from java.util.random.RandomGenerator.nextDouble.

Returns a pseudorandomly chosen double value between the specified origin (inclusive) and the specified bound (exclusive).

Implementation Note

The default implementation verifies that the origin and bound are valid, then invokes nextDouble() scaling and translating the result to fit between origin and bound( exclusive).

Parameters
origin:double

the least value that can be returned

bound:double

the upper bound (exclusive) for the returned value

Returns:double

a pseudorandomly chosen double value between the origin (inclusive) and the bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if origin is not finite, or bound is not finite, or origin is greater than or equal to bound
nextFloatback to summary
public float nextFloat()

Overrides java.util.Random.nextFloat.

Overrides default java.util.random.RandomGenerator.nextFloat.

Doc from java.util.Random.nextFloat.

Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

The general contract of nextFloat is that one float value, chosen (approximately) uniformly from the range 0.0f (inclusive) to 1.0f (exclusive), is pseudorandomly generated and returned. All 224 possible float values of the form m x 2-24, where m is a positive integer less than 224, are produced with (approximately) equal probability.

Returns:float

the next pseudorandom, uniformly distributed float value between 0.0f and 1.0f from this random number generator's sequence

Annotations
@Override
nextFloatback to summary
public float nextFloat(float bound)

Overrides default java.util.random.RandomGenerator.nextFloat.

Doc from java.util.random.RandomGenerator.nextFloat.

Returns a pseudorandomly chosen float value between zero (inclusive) and the specified bound (exclusive).

Implementation Note

The default implementation checks that bound is a positive finite float. Then invokes nextFloat(), scaling the result so that the final result lies between 0.0f (inclusive) and bound (exclusive).

Parameters
bound:float

the upper bound (exclusive) for the returned value. Must be positive and finite

Returns:float

a pseudorandomly chosen float value between zero (inclusive) and the bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if bound is not both positive and finite
nextFloatback to summary
public float nextFloat(float origin, float bound)

Overrides default java.util.random.RandomGenerator.nextFloat.

Doc from java.util.random.RandomGenerator.nextFloat.

Returns a pseudorandomly chosen float value between the specified origin (inclusive) and the specified bound (exclusive).

Implementation Note

The default implementation verifies that the origin and bound are valid then invokes nextFloat() scaling and translating the result to fit between origin and bound (exclusive).

Parameters
origin:float

the least value that can be returned

bound:float

the upper bound (exclusive)

Returns:float

a pseudorandomly chosen float value between the origin (inclusive) and the bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if origin is not finite, or bound is not finite, or origin is greater than or equal to bound
nextIntback to summary
public int nextInt()

Overrides java.util.Random.nextInt.

Overrides default java.util.random.RandomGenerator.nextInt.

Doc from java.util.Random.nextInt.

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. The general contract of nextInt is that one int value is pseudorandomly generated and returned. All 232 possible int values are produced with (approximately) equal probability.

Returns:int

the next pseudorandom, uniformly distributed int value from this random number generator's sequence

Annotations
@Override
nextIntback to summary
public int nextInt(int bound)

Overrides java.util.Random.nextInt.

Overrides default java.util.random.RandomGenerator.nextInt.

Doc from java.util.Random.nextInt.

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned. All bound possible int values are produced with (approximately) equal probability.

Parameters
bound:int

the upper bound (exclusive). Must be positive.

Returns:int

the next pseudorandom, uniformly distributed int value between zero (inclusive) and bound (exclusive) from this random number generator's sequence

Annotations
@Override
Exceptions
IllegalArgumentException:
if bound is not positive
nextIntback to summary
public int nextInt(int origin, int bound)

Overrides default java.util.random.RandomGenerator.nextInt.

Doc from java.util.random.RandomGenerator.nextInt.

Returns a pseudorandomly chosen int value between the specified origin (inclusive) and the specified bound (exclusive).

Parameters
origin:int

the least value that can be returned

bound:int

the upper bound (exclusive) for the returned value

Returns:int

a pseudorandomly chosen int value between the origin (inclusive) and the bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if origin is greater than or equal to bound
nextLongback to summary
public long nextLong()

Overrides java.util.Random.nextLong.

Implements java.util.random.RandomGenerator.nextLong.

Doc from java.util.Random.nextLong.

Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. The general contract of nextLong is that one long value is pseudorandomly generated and returned.

Returns:long

the next pseudorandom, uniformly distributed long value from this random number generator's sequence

Annotations
@Override
nextLongback to summary
public long nextLong(long bound)

Overrides default java.util.random.RandomGenerator.nextLong.

Doc from java.util.random.RandomGenerator.nextLong.

Returns a pseudorandomly chosen long value between zero (inclusive) and the specified bound (exclusive).

Parameters
bound:long

the upper bound (exclusive) for the returned value. Must be positive.

Returns:long

a pseudorandomly chosen long value between zero (inclusive) and the bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if bound is not positive
nextLongback to summary
public long nextLong(long origin, long bound)

Overrides default java.util.random.RandomGenerator.nextLong.

Doc from java.util.random.RandomGenerator.nextLong.

Returns a pseudorandomly chosen long value between the specified origin (inclusive) and the specified bound (exclusive).

Parameters
origin:long

the least value that can be returned

bound:long

the upper bound (exclusive) for the returned value

Returns:long

a pseudorandomly chosen long value between the origin (inclusive) and the bound (exclusive)

Annotations
@Override
Exceptions
IllegalArgumentException:
if origin is greater than or equal to bound
nextSecondarySeedback to summary
pack-priv static final int nextSecondarySeed()

Returns the pseudo-randomly initialized or updated secondary seed.

nextSeedback to summary
pack-priv final long nextSeed()

Update the thread local seed value by adding to it the sum of GOLDEN_GAMMA (an odd value) and twice the thread id. This sum is always odd (to guarantee that the generator has maximum period) and is different for different threads. Because thread id values are allocated consecutively starting from 0, the high 32 bits of this sum will be the same as the high 32 bits of GOLDEN_GAMMA unless an extremely large number of threads have been created, and so the overall value added to the thread local seed value will have at least fourteen 01 and 10 transitions (see the documentation for the method mixGamma in class SplittableRandom), which should provide adequate statistical quality for applications likely to use ThreadLocalRandom.

readResolveback to summary
private Object readResolve()

Returns the current thread's ThreadLocalRandom.

Returns:Object

the current thread's ThreadLocalRandom

setInheritedAccessControlContextback to summary
pack-priv static final void setInheritedAccessControlContext(Thread thread, AccessControlContext acc)

Deprecated

as a consequence of AccessControlContext being deprecated. See corresponding docs for further information.

setSeedback to summary
public void setSeed(long seed)

Overrides java.util.Random.setSeed.

Throws UnsupportedOperationException. Setting seeds in this generator is not supported.

Parameters
seed:long

Doc from java.util.Random.setSeed.

the seed value

Exceptions
UnsupportedOperationException:
always
writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Hides java.util.Random.writeObject.

Saves the ThreadLocalRandom to a stream (that is, serializes it).

Parameters
s:ObjectOutputStream

the stream

Exceptions
IOException:
if an I/O error occurs
java.util.concurrent back to summary

private Class ThreadLocalRandom.Access

extends Object
Class Inheritance

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Accessback to summary
private Access()
java.util.concurrent back to summary

private final Class ThreadLocalRandom.ThreadLocalRandomProxy

extends Random
Class Inheritance
Annotations
@SuppressWarnings:serial

Field Summary

Modifier and TypeField and Description
pack-priv static final Random

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public double
nextDouble()

Overrides java.util.Random.nextDouble.

Overrides default java.util.random.RandomGenerator.nextDouble.

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
public int
nextInt()

Overrides java.util.Random.nextInt.

Overrides default java.util.random.RandomGenerator.nextInt.

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
public long
nextLong()

Overrides java.util.Random.nextLong.

Implements java.util.random.RandomGenerator.nextLong.

Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
Inherited from java.util.Random:
doublesdoublesdoublesdoublesfromintsintsintsintslongslongslongslongsnextnextBooleannextBytesnextFloatnextGaussiannextIntsetSeed

Field Detail

PROXYback to summary
pack-priv static final Random PROXY

Constructor Detail

ThreadLocalRandomProxyback to summary
private ThreadLocalRandomProxy()

Method Detail

nextDoubleback to summary
public double nextDouble()

Overrides java.util.Random.nextDouble.

Overrides default java.util.random.RandomGenerator.nextDouble.

Doc from java.util.Random.nextDouble.

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

The general contract of nextDouble is that one double value, chosen (approximately) uniformly from the range 0.0d (inclusive) to 1.0d (exclusive), is pseudorandomly generated and returned.

Returns:double

the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence

nextIntback to summary
public int nextInt()

Overrides java.util.Random.nextInt.

Overrides default java.util.random.RandomGenerator.nextInt.

Doc from java.util.Random.nextInt.

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. The general contract of nextInt is that one int value is pseudorandomly generated and returned. All 232 possible int values are produced with (approximately) equal probability.

Returns:int

the next pseudorandom, uniformly distributed int value from this random number generator's sequence

nextLongback to summary
public long nextLong()

Overrides java.util.Random.nextLong.

Implements java.util.random.RandomGenerator.nextLong.

Doc from java.util.Random.nextLong.

Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. The general contract of nextLong is that one long value is pseudorandomly generated and returned.

Returns:long

the next pseudorandom, uniformly distributed long value from this random number generator's sequence