Class RandomFactory

    • Field Detail

      • RANDOM_FACTORY_PROP_KEY

        public static final String RANDOM_FACTORY_PROP_KEY
      • RANDOM_CLASS_PROP_KEY

        public static final String RANDOM_CLASS_PROP_KEY
      • randomClass

        public static Class<? extends Random> randomClass
    • Constructor Detail

      • RandomFactory

        public RandomFactory()
        This constructor is usually not called directly, use static factory method newInstance() 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 of RandomFactory. The default implementation is to return a new instance of RandomFactory, but the class created can be customized with the system property "jasima.core.random.RandomFactory".
        Returns:
        A new RandomFactory instance.
      • 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 given name and the seed of the RandomFactory.
        Parameters:
        name - A unique name of the Random instance (indirectly setting its seed).
        Returns:
        The new Random instance.
      • 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 given name and the seed of the RandomFactory.
        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 Random instance.
      • createRandom

        protected Random createRandom​(long seed)
        Create a new Random instance with the given seed. The concrete class instantiated is determined by a system property "jasima.core.random.RandomFactory.randomClass" (default: MersenneTwister).
        Parameters:
        seed - The seed for the new Random instance.
        Returns:
        A new Random instance initialized with the given seed.
      • 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 stream DblSequence.getName() is used. If it is null, defaultName is used instead.
        Parameters:
        stream - The stream to initialize.
        defaultName - Default name if stream provides 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 as initRndGen(DblSequence, String), just without a default name.
        Parameters:
        stream - The DblSequence to configure.
        Returns:
        The stream with random number generator initialized.
      • 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.