AnyLogic
Expand
Font size

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:
    • To set random seed, choose the Random seed (unique experiments) option.
    • To set fixed seed, choose the Fixed seed (reproducible experiments) option and enter the seed value in the Seed value edit box.

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.

Obtaining the default random number generator programmatically

You can retrieve the random number generator used by all probability distributions by default by calling the global function java.util.Random getDefaultRandomGenerator(). The default random number generator is used if no particular generator is specified in the call to a probability distribution function.

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:

  • Substitute AnyLogic default RNG with your own RNG.
  • Have multiple RNGs and explicitly specify which RNG should be used when calling a probability distribution function.

To substitute the default RNG with your own RNG

  1. Create a Java class defining the logic of your custom RNG. Name it, e.g. XSRandom as in the example described below. It should be a subclass of the Java class java.util.Random, so on the first page of the New Java Class wizard specify java.util.Random in the Superclass field.
  2. In the opened Java editor, write the code of your Java class. Here we provide you with an example of Xorshift random number generator. You can study the code and similarly implement the algorithm you need.
    RNG class. Code sample
    import java.util.Random;
    
    /**
     * A subclass of java.util.Random class that implements the 
     * Xorshift random number generator
     */
    
    public class XSRandom extends Random {
     private long seed;
    
     public XSRandom(long seed) {
      this.seed = seed;
     }
    
     protected int next(int nbits) {
      long x = seed;
      x ^= (x << 21);
      x ^= (x >>> 35);
      x ^= (x << 4);
      seed = x;
      x &= ((1L << nbits) - 1);
      return (int) x;
     }
    }
  3. Select the experiment and open the Randomness section of its properties. 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 XSRandom().

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(java.util.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.

How can we improve this article?