Class RandomManager
-
- All Implemented Interfaces:
public class RandomManagerCentralized 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
-
-
Field Summary
Fields Modifier and Type Field Description public static volatile intmasterSeed
-
Constructor Summary
Constructors Constructor Description RandomManager()
-
Method Summary
Modifier and Type Method Description static intgetMasterSeed()Gets the current master seed. static voidsetMasterSeed(int seed)Sets the master seed that controls all random number generation. static MersenneTwistergetThreadRandom()Gets a thread-specific MersenneTwister instance. static RandomgetThreadRandomAsRandom()Gets a thread-specific Random instance for compatibility with java.util.Random. static MersenneTwistergetSolverRandom(String solverId)Gets a solver-specific MersenneTwister instance. static MersenneTwistergetParallelRandom(String solverId, int threadOffset)Creates a MersenneTwister instance for parallel processing with a thread offset. static doublenextDouble()Generates a random double value in [0.0, 1.0) using the thread-specific generator. static doublenextGaussian()Generates 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 MersenneTwisternewInstance()Gets a fresh MersenneTwister instance with the master seed. static intgenerateRandomSeed()Generates a random seed using the same formula as SolverOptions. -
-
Method Detail
-
getMasterSeed
static int getMasterSeed()
Gets the current master seed.
- Returns:
the current master seed
-
setMasterSeed
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
-
getThreadRandom
static 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
static Random 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
static MersenneTwister getSolverRandom(String solverId)
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
static 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.- Returns:
MersenneTwister instance with deterministic seed
-
nextDouble
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
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
static int nextInt(int bound)
Generates a random integer between 0 (inclusive) and the specified value (exclusive).
- Parameters:
bound- the upper bound (exclusive).- Returns:
random integer in range [0, bound)
-
reset
static void reset()
Resets all random number generators to use the current master seed. This can be called to ensure fresh random sequences.
-
newInstance
static 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
static int generateRandomSeed()
Generates a random seed using the same formula as SolverOptions.
- Returns:
random seed value
-
-
-
-