Random Number Generator

Stochastic models require a random seed value for the pseudorandom number generator. In this case model runs cannot be reproduced since the model random number generator is initialized with different values for each model run. Specifying the fixed seed value, you initialize the model random number generator with the same value for each model run, thus the model runs are reproducible.

To set up random number generator seed
  1. In the Projects view, select the experiment you are currently working with.
  2. Go to the Randomness section of the Properties view.
  3. In the Random number generation group of buttons:

If the model does not receive any external input (either data or user actions), the behavior of the model in two simulations with the same initial seeds is identical. The random number generator is initialized once when the model is created and is not reinitialized between model replications.

In some rare cases the model may output non-reproducible results even with the option Fixed seed (reproducible experiments) being selected. These situations are described here.

Custom random number generator

By default, all probability distribution functions in AnyLogic, the Process Modeling Library blocks, the random transitions and events, the random layouts and networks and the AnyLogic simulation engine itself – in other words, all randomness in AnyLogic, is based on the default random number generator. The default random number generator is an instance of the Java class Random, which is a Linear Congruental Generator (LCG).

If for any reason you are not satisfied with the quality of Random, you can:

To substitute the default RNG with your own RNG

  1. Prepare your custom RNG. It should be a subclass of the Java class java.util.Random, e.g. MyRandom.
  2. Select the experiment and open the Randomness section of its properties.
  3. Select the radio button Custom generator (subclass of Random) and in the field on the right write the expression returning an instance of your RNG, for example:
    new MyRandom() or
    new MyRandom( 1234 )

Setting a custom random number generator as default RNG

The initialization of the default RNG (provided by AnyLogic or by you) occurs during the initialization of the experiment and then before each simulation run.

In addition you can substitute the default RNG at any time by calling:

setDefaultRandomGenerator( Random r )

However you should be aware that before each simulation run the generator will be set up again according to the settings on the General page of the experiment properties.

To use a custom RNG in a particular call of a probability distribution function

  1. Create and initialize an instance of your custom RNG. For example, it may be a variable myRNG of class Random or its subclass.
  2. When calling a probability distribution function, provide myRNG as the last parameter, for example:
    uniform( myRNG ) or
    triangular( 5, 10, 25, myRNG )
If a probability distribution function has several forms with different parameters, some of them may not have a variant with a custom RNG, but the one with the most complete parameter set always has it.