Library of utility functions for the Random package (usually of no interest for the user)
This package contains utility functions for the random number generators, that are usually of no interest for the user (they are, for example, used in package Blocks.Noise).
Extends from Modelica.Icons.UtilitiesPackage (Icon for utility packages).
Name | Description |
---|---|
initialStateWithXorshift64star | Return an initial state vector for a random number generator (based on xorshift64star algorithm) |
automaticGlobalSeed | Creates an automatic integer seed (typically from the current time and process id; this is an impure function) |
initializeImpureRandom | Initializes the internal state of the impure random number generator |
impureRandom | Impure random number generator (with hidden state vector) |
impureRandomInteger | Impure random number generator for integer values (with hidden state vector) |
Return an initial state vector for a random number generator (based on xorshift64star algorithm)
state = Utilities.initialStateWithXorshift6star(localSeed, globalSeed, nState);
The Xorshift64star random number generator is used to fill a state vector of length nState (nState ≥ 1) with random numbers and return this vector. Arguments localSeed and globalSeed are any Integer numbers (including zero or negative number) that characterize the initial state. If the same localSeed, globalSeed, nState is given, the same state vector is returned.
parameter Integer localSeed; parameter Integer globalSeed; Integer state[33]; initial equation state = Utilities.initialStateWithXorshift64star(localSeed, globalSeed, size(state,1));
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
localSeed | The local seed to be used for generating initial states |
globalSeed | The global seed to be combined with the local seed |
nState | The dimension of the state vector (>= 1) |
Name | Description |
---|---|
state[nState] | The generated initial states |
Creates an automatic integer seed (typically from the current time and process id; this is an impure function)
seed = Utilities.automaticGlobalSeed();
Returns an automatically computed seed (Integer). Typically, this seed is computed from:
If getTime and getPid functions are not available on the target where this Modelica function is called, other means to compute a seed may be used.
Note, this is an impure function that returns always a different value, when it is newly called. This function should be only called once during initialization.
parameter Boolean useAutomaticSeed = false; parameter Integer fixedSeed = 67867967; final parameter Integer seed = if useAutomaticSeed then Random.Utilities.automaticGlobalSeed() else fixedSeed;
This function is impure!
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
seed | Automatically generated seed |
Initializes the internal state of the impure random number generator
id = initializeImpureRandom(seed;
Generates a hidden initial state vector for the Xorshift1024star random number generator (= xorshift1024* algorithm), from Integer input argument seed. Argument seed can be given any value (including zero or negative number). The function returns the dummy Integer number id. This number needs to be passed as input to function impureRandom, in order that the sorting order is correct (so that impureRandom is always called after initializeImpureRandom). The function stores a reasonable initial state vector in a C-static memory by using the Xorshift64star random number generator to fill the internal state vector with 64 bit random numbers.
parameter Integer seed; Real r; function random = impureRandom (final id=id); protected Integer id = initializeImpureRandom(seed); equation // Use the random number generator when sample(0,0.001) then r = random(); end when;
Utilities.impureRandom, Random.Generators
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
seed | The input seed to initialize the impure random number generator |
Name | Description |
---|---|
id | Identification number to be passed as input to function impureRandom, in order that sorting is correct |
Impure random number generator (with hidden state vector)
r = impureRandom(id);
Returns a uniform random number in the range 0 < random ≤ 1 with the xorshift1024* algorithm. The dummy input Integer argument id must be the output argument of a call to function initializeImpureRandom, in order that the sorting order is correct (so that impureRandom is always called after initializeImpureRandom). For every call of impureRandom(id), a different random number is returned, so the function is impure.
parameter Integer seed; Real r; function random = impureRandom (final id=id); protected Integer id; equation // Initialize the random number generator when initial() then id = initializeImpureRandom(seed, time); end when; // Use the random number generator when sample(0,0.001) then r = random(); end when;
initializeImpureRandom, Random.Generators
This function is impure!
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
id | Identification number from initializeImpureRandom(..) function (is needed for correct sorting) |
Name | Description |
---|---|
y | A random number with a uniform distribution on the interval (0,1] |
Impure random number generator for integer values (with hidden state vector)
r = impureRandomInteger(id, imin=1, imax=Modelica.Constants.Integer_inf);
Returns an Integer random number in the range imin ≤ random ≤ imax with the xorshift1024* algorithm, (the random number in the range 0 ... 1 returned by the xorshift1024* algorithm is mapped to an Integer number in the range imin ... imax). The dummy input Integer argument id must be the output argument of a call to function initializeImpureRandom, in order that the sorting order is correct (so that impureRandomInteger is always called after initializeImpureRandom). For every call of impureRandomInteger(id), a different random number is returned, so the function is impure.
initializeImpureRandom, Random.Generators
This function is impure!
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
id | Identification number from initializeImpureRandom(..) function (is needed for correct sorting) |
imin | Minimum integer to generate |
imax | Maximum integer to generate (default = 2^28) |
Name | Description |
---|---|
y | A random number with a uniform distribution on the interval [imin,imax] |