Package org.apache.commons.rng.simple
Class JDKRandomBridge
- java.lang.Object
-
- java.util.Random
-
- org.apache.commons.rng.simple.JDKRandomBridge
-
- All Implemented Interfaces:
java.io.Serializable
public final class JDKRandomBridge extends java.util.RandomSubclass ofRandomthatdelegatesto aRestorableUniformRandomProviderinstance 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
Randomin order to replace its linear congruential generator by anyRandomSource.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:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private RestorableUniformRandomProviderdelegateDelegate.private booleanisInitializedWorkaround JDK's "Random" bug: https://bugs.openjdk.java.net/browse/JDK-8154225.private static longserialVersionUIDSerializable version identifier.private RandomSourcesourceSource.
-
Constructor Summary
Constructors Constructor Description JDKRandomBridge(RandomSource source, java.lang.Object seed)Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intnext(int n)Delegates the generation of 32 random bits to theRandomSourceargument provided atconstruction.private voidreadObject(java.io.ObjectInputStream input)Deserialization method.voidsetSeed(long seed)private voidwriteObject(java.io.ObjectOutputStream output)Serialization method.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serializable version identifier.- See Also:
- Constant Field Values
-
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 Detail
-
JDKRandomBridge
public JDKRandomBridge(RandomSource source, java.lang.Object seed)
Creates a new instance.- Parameters:
source- Source of randomness.seed- Seed. Can benull.
-
-
Method Detail
-
setSeed
public void setSeed(long seed)
- Overrides:
setSeedin classjava.util.Random
-
next
protected int next(int n)
Delegates the generation of 32 random bits to theRandomSourceargument provided atconstruction. The returned value is such that if the source of randomness isRandomSource.JDK, all the generated values will be identical to those produced by the same sequence of calls on aRandominstance initialized with the same seed.- Overrides:
nextin classjava.util.Random- Parameters:
n- Number of random bits which the requested value must contain.- Returns:
- the value represented by the
nhigh-order bits of a pseudo-random 32-bits integer.
-
writeObject
private void writeObject(java.io.ObjectOutputStream output) throws java.io.IOExceptionSerialization method.- Parameters:
output- Output stream.- Throws:
java.io.IOException- if an error occurs.
-
readObject
private void readObject(java.io.ObjectInputStream input) throws java.io.IOException, java.lang.ClassNotFoundExceptionDeserialization method.- Parameters:
input- Input stream.- Throws:
java.io.IOException- if an error occurs.java.lang.ClassNotFoundException- if an error occurs.
-
-