Package jline.util

Class RandomManager

java.lang.Object
jline.util.RandomManager

public class RandomManager extends Object
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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    Generates a random seed using the same formula as SolverOptions.
    static int
    Gets the current master seed.
    static org.apache.commons.math3.random.MersenneTwister
    getParallelRandom(String solverId, int threadOffset)
    Creates a MersenneTwister instance for parallel processing with a thread offset.
    static Random
    getParallelRandomAsRandom(String solverId, int threadOffset)
    Returns a Random adapter wrapping the parallel MersenneTwister for the given solver/thread.
    static org.apache.commons.math3.random.MersenneTwister
    Gets a solver-specific MersenneTwister instance.
    static org.apache.commons.math3.random.MersenneTwister
    Gets a thread-specific MersenneTwister instance.
    static Random
    Gets a thread-specific Random instance for compatibility with java.util.Random.
    static org.apache.commons.math3.random.MersenneTwister
    Gets a fresh MersenneTwister instance with the master seed.
    static double
    Generates a random double value in [0.0, 1.0) using the thread-specific generator.
    static double
    Generates a random Gaussian (normal) distributed double value using the thread-specific generator.
    static int
    nextInt(int bound)
    Generates a random integer between 0 (inclusive) and the specified value (exclusive).
    static void
    Resets all random number generators to use the current master seed.
    static void
    setMasterSeed(int seed)
    Sets the master seed that controls all random number generation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public 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

      public static org.apache.commons.math3.random.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

      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 solver
      threadOffset - offset for this thread (e.g., thread index in parallel processing)
      Returns:
      MersenneTwister instance with deterministic seed
    • getParallelRandomAsRandom

      public static Random getParallelRandomAsRandom(String solverId, int threadOffset)
      Returns a Random adapter wrapping the parallel MersenneTwister for the given solver/thread.
      Parameters:
      solverId - logical solver identifier
      threadOffset - 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