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 Content

Name Description
Modelica.Math.Random.Utilities.initialStateWithXorshift64star initialStateWithXorshift64star Return an initial state vector for a random number generator (based on xorshift64star algorithm)
Modelica.Math.Random.Utilities.automaticGlobalSeed automaticGlobalSeed Creates an automatic integer seed (typically from the current time and process id; this is an impure function)
Modelica.Math.Random.Utilities.automaticLocalSeed automaticLocalSeed Creates an automatic local seed from the instance name
Modelica.Math.Random.Utilities.initializeImpureRandom initializeImpureRandom Initializes the internal state of the impure random number generator
Modelica.Math.Random.Utilities.impureRandom impureRandom Impure random number generator (with hidden state vector)
Modelica.Math.Random.Utilities.impureRandomInteger impureRandomInteger Impure random number generator for integer values (with hidden state vector)

Modelica.Math.Random.Utilities.initialStateWithXorshift64star 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

NameDescription
localSeedThe local seed to be used for generating initial states
globalSeedThe global seed to be combined with the local seed
nStateThe dimension of the state vector (>= 1)

Outputs

NameDescription
state[nState]The generated initial states

Modelica.Math.Random.Utilities.automaticGlobalSeed 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;

See also

automaticLocalSeed.

Note

This function is impure!

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

Outputs

NameDescription
seedAutomatically generated seed

Modelica.Math.Random.Utilities.automaticLocalSeed Modelica.Math.Random.Utilities.automaticLocalSeed

Creates an automatic local seed from the instance name

Information

Syntax

seed = Utilities.automaticLocalSeed(path);

Description

Returns an automatically computed seed (Integer) from the hash value of the full path name of an instance (has to be inquired in the model or block where this function is called by the Modelica built-in operator getInstanceName()). Contrary to automaticGlobalSeed(), this is a pure function, that is, the same seed is returned, if an identical path is provided.

Example

parameter Boolean useAutomaticLocalSeed = true;
parameter Integer fixedLocalSeed        = 10;
final parameter Integer localSeed = if useAutomaticLocalSeed then
                                   automaticLocalSeed(getInstanceName())
                                 else
                                   fixedLocalSeed;

See also

automaticGlobalSeed, hashString and getInstanceName.

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

Inputs

NameDescription
pathFull path name of the instance (inquire with getInstanceName())

Outputs

NameDescription
seedAutomatically generated seed

Modelica.Math.Random.Utilities.initializeImpureRandom 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

NameDescription
seedThe input seed to initialize the impure random number generator

Outputs

NameDescription
idIdentification number to be passed as input to function impureRandom, in order that sorting is correct

Modelica.Math.Random.Utilities.impureRandom 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

NameDescription
idIdentification number from initializeImpureRandom(..) function (is needed for correct sorting)

Outputs

NameDescription
yA random number with a uniform distribution on the interval (0,1]

Modelica.Math.Random.Utilities.impureRandomInteger 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

NameDescription
idIdentification number from initializeImpureRandom(..) function (is needed for correct sorting)
iminMinimum integer to generate
imaxMaximum integer to generate (default = 2^28)

Outputs

NameDescription
yA random number with a uniform distribution on the interval [imin,imax]
Automatically generated Thu Oct 1 16:08:15 2020.