Package Modelica.​Math.​Random.​Utilities
Library of utility functions for the Random package (usually of no interest for the user)

Information

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).

Package Contents

NameDescription
automaticGlobalSeedCreates an automatic integer seed (typically from the current time and process id; this is an impure function)
impureRandomImpure random number generator (with hidden state vector)
impureRandomIntegerImpure random number generator for integer values (with hidden state vector)
initializeImpureRandomInitializes the internal state of the impure random number generator
initialStateWithXorshift64starReturn an initial state vector for a random number generator (based on xorshift64star algorithm)

Function Modelica.​Math.​Random.​Utilities.​initialStateWithXorshift64star
Return an initial state vector for a random number generator (based on xorshift64star algorithm)

Information

Syntax

state = Utilities.initialStateWithXorshift6star(localSeed, globalSeed, nState);

Description

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.

Example

  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).

Inputs

TypeNameDescription
IntegerlocalSeedThe local seed to be used for generating initial states
IntegerglobalSeedThe global seed to be combined with the local seed
IntegernStateThe dimension of the state vector (>= 1)

Outputs

TypeNameDescription
Integerstate[nState]The generated initial states

Function Modelica.​Math.​Random.​Utilities.​automaticGlobalSeed
Creates an automatic integer seed (typically from the current time and process id; this is an impure function)

Information

Syntax

seed = Utilities.automaticGlobalSeed();

Description

Returns an automatically computed seed (Integer). Typically, this seed is computed from:

  1. The current local time by computing the number of milli-seconds up to the current hour
  2. The process id (added to the first part by multiplying it with the prime number 6007).

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.

Example

     parameter Boolean useAutomaticSeed = false;
     parameter Integer fixedSeed = 67867967;
     final parameter Integer seed = if useAutomaticSeed then
                                   Random.Utilities.automaticGlobalSeed() else fixedSeed;

Note

This function is impure!

Extends from Modelica.​Icons.​Function (Icon for functions).

Outputs

TypeNameDescription
IntegerseedAutomatically generated seed

Function Modelica.​Math.​Random.​Utilities.​initializeImpureRandom
Initializes the internal state of the impure random number generator

Information

Syntax

id = initializeImpureRandom(seed;

Description

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.

Example

  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;

See also

Utilities.impureRandom, Random.Generators

Extends from Modelica.​Icons.​Function (Icon for functions).

Inputs

TypeNameDescription
IntegerseedThe input seed to initialize the impure random number generator

Outputs

TypeNameDescription
IntegeridIdentification number to be passed as input to function impureRandom, in order that sorting is correct

Function Modelica.​Math.​Random.​Utilities.​impureRandom
Impure random number generator (with hidden state vector)

Information

Syntax

r = impureRandom(id);

Description

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.

Example

  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;

See also

initializeImpureRandom, Random.Generators

Note

This function is impure!

Extends from Modelica.​Icons.​Function (Icon for functions).

Inputs

TypeNameDescription
IntegeridIdentification number from initializeImpureRandom(..) function (is needed for correct sorting)

Outputs

TypeNameDescription
RealyA random number with a uniform distribution on the interval (0,1]

Function Modelica.​Math.​Random.​Utilities.​impureRandomInteger
Impure random number generator for integer values (with hidden state vector)

Information

Syntax

r = impureRandomInteger(id, imin=1, imax=Modelica.Constants.Integer_inf);

Description

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.

See also

initializeImpureRandom, Random.Generators

Note

This function is impure!

Extends from Modelica.​Icons.​Function (Icon for functions).

Inputs

TypeNameDescription
IntegeridIdentification number from initializeImpureRandom(..) function (is needed for correct sorting)
IntegeriminMinimum integer to generate
IntegerimaxMaximum integer to generate (default = 2^28)

Outputs

TypeNameDescription
IntegeryA random number with a uniform distribution on the interval [imin,imax]