Package jasima.core.random
Class RandomFactory
- java.lang.Object
-
- jasima.core.random.RandomFactory
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
RandomFactoryOld
public class RandomFactory extends Object implements Serializable
This class provides functionality to create (independent) random number streams. These streams are dependent on a base seed (setSeed(long)) and a stream name (provided as a parameter tocreateInstance(String)).Behaviour of this class can be modified in two ways using system properties.
- A property
RANDOM_FACTORY_PROP_KEYcan be used to change the class returned by the static methodnewInstance(). - If just a different implementation of
Randomis desired (default isMersenneTwister), use the system propertyRANDOM_CLASS_PROP_KEY.
- Author:
- Torsten Hildebrandt
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static StringRANDOM_CLASS_PROP_KEYstatic StringRANDOM_FACTORY_PROP_KEYstatic Class<? extends Random>randomClassstatic Class<? extends RandomFactory>randomFactoryClass
-
Constructor Summary
Constructors Constructor Description RandomFactory()This constructor is usually not called directly, use static factory methodnewInstance()instead.RandomFactory(Simulation sim, long initialSeed)Convenience constructor when creating instances directly.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RandomcreateInstance(String name)Create a new random instance.RandomcreateInstance(String name, Consumer<String> warningReceiver)Create a new random instance.protected RandomcreateRandom(long seed)Create a newRandominstance with the given seed.protected longgetSeed(String name, Consumer<String> warningReceiver)Compute a (hopefully unique) seed which only depends on 'name' and this RandomFactory's seed.SimulationgetSim()DblSequenceinitRndGen(DblSequence stream)Initializes the random number generator of a DblStream if it is not already set using the stream's name.<T extends DblSequence>
TinitRndGen(T stream, String defaultName)Initializes the random number generator of a DblStream if it is not already set.static RandomFactorynewInstance()Factory method to create a new instance ofRandomFactory.static voidreloadSysProps()Used during tests to change implementation classes while JVM is running.voidsetSeed(long seed)Sets the seed that is used to initialize all random number streams.voidsetSim(Simulation sim)Sets the simulation this random factory is currently used with.
-
-
-
Field Detail
-
RANDOM_FACTORY_PROP_KEY
public static final String RANDOM_FACTORY_PROP_KEY
-
randomFactoryClass
public static Class<? extends RandomFactory> randomFactoryClass
-
RANDOM_CLASS_PROP_KEY
public static final String RANDOM_CLASS_PROP_KEY
-
-
Constructor Detail
-
RandomFactory
public RandomFactory()
This constructor is usually not called directly, use static factory methodnewInstance()instead.
-
RandomFactory
public RandomFactory(Simulation sim, long initialSeed)
Convenience constructor when creating instances directly.
-
-
Method Detail
-
newInstance
public static RandomFactory newInstance()
Factory method to create a new instance ofRandomFactory. The default implementation is to return a new instance ofRandomFactory, but the class created can be customized with the system property "jasima.core.random.RandomFactory".- Returns:
- A new
RandomFactoryinstance.
-
createInstance
public Random createInstance(String name)
Create a new random instance. The seed of this new instance (and hence the stream of pseudo-random numbers) is determined by the givennameand the seed of theRandomFactory.- Parameters:
name- A unique name of the Random instance (indirectly setting its seed).- Returns:
- The new
Randominstance.
-
createInstance
public Random createInstance(String name, @Nullable Consumer<String> warningReceiver)
Create a new random instance. The seed of this new instance (and hence the stream of pseudo-random numbers) is determined by the givennameand the seed of theRandomFactory.- Parameters:
name- A unique name of the Random instance (indirectly setting its seed).warningReceiver- used to issue a warning message in case of a hash collision- Returns:
- The new
Randominstance.
-
createRandom
protected Random createRandom(long seed)
Create a newRandominstance with the given seed. The concrete class instantiated is determined by a system property "jasima.core.random.RandomFactory.randomClass" (default:MersenneTwister).
-
getSeed
protected long getSeed(String name, @Nullable Consumer<String> warningReceiver)
Compute a (hopefully unique) seed which only depends on 'name' and this RandomFactory's seed.- Parameters:
warningReceiver- used to issue a warning message in case of a hash collision
-
setSeed
public void setSeed(long seed)
Sets the seed that is used to initialize all random number streams.- Parameters:
seed- The seed to use.
-
initRndGen
public <T extends DblSequence> T initRndGen(T stream, String defaultName)
Initializes the random number generator of a DblStream if it is not already set. As name of the streamDblSequence.getName()is used. If it isnull,defaultNameis used instead.- Parameters:
stream- The stream to initialize.defaultName- Default name ifstreamprovides no useful information.- Returns:
- The same as
stream.
-
initRndGen
public DblSequence initRndGen(DblSequence stream)
Initializes the random number generator of a DblStream if it is not already set using the stream's name. This method is the same asinitRndGen(DblSequence, String), just without a default name.- Parameters:
stream- TheDblSequenceto configure.- Returns:
- The stream with random number generator initialized.
-
getSim
public Simulation getSim()
-
setSim
public void setSim(Simulation sim)
Sets the simulation this random factory is currently used with.
-
reloadSysProps
public static void reloadSysProps()
Used during tests to change implementation classes while JVM is running.
-
-