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
- Prepare your custom RNG. It should be a subclass of the
Java class java.util.Random,
- Select the experiment and open the Randomness section of its
- 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
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
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
particular call of a probability distribution function
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.
- Create and initialize an
instance of your custom RNG. For example, it may be a variable myRNG of class Random
or its subclass.
- When calling a probability
distribution function, provide myRNG as
the last parameter, for example:
uniform( myRNG ) or
triangular( 5, 10, 25, myRNG )