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.