Package jline.util
Class RandomManager
java.lang.Object
jline.util.RandomManager
Centralized random number generator management for reproducible, thread-safe random number generation.
This class ensures that:
- All random number generation uses MersenneTwister
- Setting a master seed produces deterministic results across all threads
- Each thread gets its own MersenneTwister instance with a deterministic sub-seed
- Parallel execution is reproducible when using the same master seed
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intGenerates a random seed using the same formula as SolverOptions.static intGets the current master seed.static org.apache.commons.math3.random.MersenneTwistergetParallelRandom(String solverId, int threadOffset) Creates a MersenneTwister instance for parallel processing with a thread offset.static RandomgetParallelRandomAsRandom(String solverId, int threadOffset) Returns aRandomadapter wrapping the parallelMersenneTwisterfor the given solver/thread.static org.apache.commons.math3.random.MersenneTwistergetSolverRandom(String solverId) Gets a solver-specific MersenneTwister instance.static org.apache.commons.math3.random.MersenneTwisterGets a thread-specific MersenneTwister instance.static RandomGets a thread-specific Random instance for compatibility with java.util.Random.static org.apache.commons.math3.random.MersenneTwisterGets a fresh MersenneTwister instance with the master seed.static doubleGenerates a random double value in [0.0, 1.0) using the thread-specific generator.static doubleGenerates a random Gaussian (normal) distributed double value using the thread-specific generator.static intnextInt(int bound) Generates a random integer between 0 (inclusive) and the specified value (exclusive).static voidreset()Resets all random number generators to use the current master seed.static voidsetMasterSeed(int seed) Sets the master seed that controls all random number generation.
-
Constructor Details
-
RandomManager
public RandomManager()
-
-
Method Details
-
setMasterSeed
public static void setMasterSeed(int seed) Sets the master seed that controls all random number generation. This ensures reproducible results across all threads and solvers.- Parameters:
seed- the master seed value
-
getMasterSeed
public static int getMasterSeed()Gets the current master seed.- Returns:
- the current master seed
-
getThreadRandom
public static org.apache.commons.math3.random.MersenneTwister getThreadRandom()Gets a thread-specific MersenneTwister instance. Each thread gets its own generator with a deterministic seed derived from the master seed.- Returns:
- thread-specific MersenneTwister instance
-
getThreadRandomAsRandom
Gets a thread-specific Random instance for compatibility with java.util.Random. This wraps the MersenneTwister to provide java.util.Random compatibility.- Returns:
- thread-specific Random instance backed by MersenneTwister
-
getSolverRandom
Gets a solver-specific MersenneTwister instance. Each solver gets its own generator with a deterministic seed.- Parameters:
solverId- unique identifier for the solver- Returns:
- solver-specific MersenneTwister instance
-
getParallelRandom
public static org.apache.commons.math3.random.MersenneTwister getParallelRandom(String solverId, int threadOffset) Creates a MersenneTwister instance for parallel processing with a thread offset. This ensures different threads in parallel algorithms get different but reproducible sequences.- Parameters:
solverId- unique identifier for the solverthreadOffset- offset for this thread (e.g., thread index in parallel processing)- Returns:
- MersenneTwister instance with deterministic seed
-
getParallelRandomAsRandom
Returns aRandomadapter wrapping the parallelMersenneTwisterfor the given solver/thread.- Parameters:
solverId- logical solver identifierthreadOffset- offset to differentiate parallel workers- Returns:
- java.util.Random instance backed by MersenneTwister
-
nextDouble
public static double nextDouble()Generates a random double value in [0.0, 1.0) using the thread-specific generator. For backward compatibility, this uses the same ThreadLocal pattern as original ThreadLocalRandom.- Returns:
- random double value
-
nextGaussian
public static double nextGaussian()Generates a random Gaussian (normal) distributed double value using the thread-specific generator. For backward compatibility, this uses the same ThreadLocal pattern as original ThreadLocalRandom.- Returns:
- random Gaussian-distributed double value
-
nextInt
public static int nextInt(int bound) Generates a random integer between 0 (inclusive) and the specified value (exclusive).- Parameters:
bound- the upper bound (exclusive). Must be positive.- Returns:
- random integer in range [0, bound)
-
reset
public static void reset()Resets all random number generators to use the current master seed. This can be called to ensure fresh random sequences. -
newInstance
public static org.apache.commons.math3.random.MersenneTwister newInstance()Gets a fresh MersenneTwister instance with the master seed. Useful for creating new independent random streams.- Returns:
- new MersenneTwister instance with master seed
-
generateRandomSeed
public static int generateRandomSeed()Generates a random seed using the same formula as SolverOptions.- Returns:
- random seed value
-