Package org.apache.commons.rng.simple
Class ThreadLocalRandomSource
- java.lang.Object
-
- org.apache.commons.rng.simple.ThreadLocalRandomSource
-
public final class ThreadLocalRandomSource extends java.lang.ObjectThis class provides a thread-localUniformRandomProvider.The
UniformRandomProvideris created once-per-thread using the default construction methodRandomSource.create().Example:
import org.apache.commons.rng.simple.RandomSource; import org.apache.commons.rng.simple.ThreadLocalRandomSource; import org.apache.commons.rng.sampling.distribution.PoissonSampler; // Access a thread-safe random number generator UniformRandomProvider rng = ThreadLocalRandomSource.current(RandomSource.SPLIT_MIX_64); // One-time Poisson sample double mean = 12.3; int counts = PoissonSampler.of(rng, mean).sample();Note if the
RandomSourcerequires additional arguments then it is not supported. The same can be achieved using:import org.apache.commons.rng.simple.RandomSource; import org.apache.commons.rng.sampling.distribution.PoissonSampler; // Provide a thread-safe random number generator with data arguments private static ThreadLocal<UniformRandomProvider> rng = new ThreadLocal<UniformRandomProvider>() { @Override protected UniformRandomProvider initialValue() { return RandomSource.TWO_CMRES_SELECT.create(null, 3, 4); } }; // One-time Poisson sample using a thread-safe random number generator double mean = 12.3; int counts = PoissonSampler.of(rng.get(), mean).sample();- Since:
- 1.3
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classThreadLocalRandomSource.ThreadLocalRngExtend theThreadLocalto allow creation of the desiredRandomSource.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map<RandomSource,java.lang.ThreadLocal<UniformRandomProvider>>SOURCESA map containing theThreadLocalinstance for eachRandomSource.
-
Constructor Summary
Constructors Modifier Constructor Description privateThreadLocalRandomSource()No public construction.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static UniformRandomProvidercurrent(RandomSource source)Returns the current thread's copy of the givensource.
-
-
-
Field Detail
-
SOURCES
private static final java.util.Map<RandomSource,java.lang.ThreadLocal<UniformRandomProvider>> SOURCES
A map containing theThreadLocalinstance for eachRandomSource.This should only be modified to create new instances in a synchronized block.
-
-
Method Detail
-
current
public static UniformRandomProvider current(RandomSource source)
Returns the current thread's copy of the givensource. If there is no value for the current thread, it is first initialized to the value returned byRandomSource.create().Note if the
sourcerequires additional arguments then it is not supported.- Parameters:
source- the source- Returns:
- the current thread's value of the
source. - Throws:
java.lang.IllegalArgumentException- if the source is null or the source requires arguments
-
-