Class JDKRandomBridge

java.lang.Object
java.util.Random
org.apache.commons.rng.simple.JDKRandomBridge
All Implemented Interfaces:
Serializable, java.util.random.RandomGenerator

public final class JDKRandomBridge extends Random
Subclass of Random that delegates to a RestorableUniformRandomProvider instance but will otherwise rely on the base class for generating all the random types.

Legacy applications coded against the JDK's API could use this subclass of Random in order to replace its linear congruential generator by any RandomSource.

Caveat: Use of this class is not recommended for new applications. In particular, there is no guarantee that the serialized form of this class will be compatible across (even minor) releases of the library.

Since:
1.0
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Serializable version identifier.
      See Also:
    • source

      private final RandomSource source
      Source.
    • delegate

      private transient RestorableUniformRandomProvider delegate
      Delegate.
    • isInitialized

      private final transient boolean isInitialized
      Workaround JDK's "Random" bug: https://bugs.openjdk.java.net/browse/JDK-8154225.
  • Constructor Details

    • JDKRandomBridge

      public JDKRandomBridge(RandomSource source, Object seed)
      Creates a new instance.
      Parameters:
      source - Source of randomness.
      seed - Seed. Can be null.
  • Method Details

    • setSeed

      public void setSeed(long seed)
      Overrides:
      setSeed in class Random
    • next

      protected int next(int n)
      Delegates the generation of 32 random bits to the RandomSource argument provided at construction. The returned value is such that if the source of randomness is RandomSource.JDK, all the generated values will be identical to those produced by the same sequence of calls on a Random instance initialized with the same seed.
      Overrides:
      next in class Random
      Parameters:
      n - Number of random bits which the requested value must contain.
      Returns:
      the value represented by the n high-order bits of a pseudo-random 32-bits integer.
    • writeObject

      private void writeObject(ObjectOutputStream output) throws IOException
      Parameters:
      output - Output stream.
      Throws:
      IOException - if an error occurs.
    • readObject

      private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException
      Parameters:
      input - Input stream.
      Throws:
      IOException - if an error occurs.
      ClassNotFoundException - if an error occurs.