Programming Reference:RandomGenerator Class

From BCI2000 Wiki
Jump to: navigation, search

Location

BCI2000/src/shared/utils

Synopsis

The RandomGenerator class encapsulates a pseudo random number generator. Each class instance maintains its own internal state; for a given random seed, this ensures identical number sequences across software elements that use pseudo random numbers.

When a nonzero RandomSeed parameter is present at initialization time, the RandomGenerator class uses it as its random seed. Otherwise, it initializes itself from the current system time.

The RandomGenerator is a descendant of the EnvironmentExtension class.

Properties

For each property, there is a getter method carrying the plain property name. Writable properties have an additional setter method prefixed with "Set".

int Seed (rw)

A 32-bit value representing the random number generator's internal state. On each iteration, the seed is replaced with the generator's next state.

int RandMax (r)

An integer constant representing the maximum number returned from the Random() method.

Events

Initialize

In the Initialize event handler, the Seed property is initialized depending on whether a RandomSeed parameter is present in the system, and on its value. When a nonzero RandomSeed parameter is present, it is used as a random seed. Otherwise, the seed is initialized from the current system time.

Methods

int Random()

Returns a pseudo random number in the range from 0 to, and including, RandMax.

int operator()(int N)

Returns a pseudo random integer in the range from 0 to N-1. This function is a template, with the int type as a template parameter. It is provided to allow using an object of type RandomGenerator as a functor, i.e. an object that may be used as if it were a function name. A typical application would be to specify a RandomGenerator object as a third argument to the STL's std::random_shuffle() function.

Parameters

RandomSeed (optional)

When a nonzero RandomSeed parameter is present at initialization time, the RandomGenerator class uses it as its random seed. Otherwise, it initializes itself from the current system time.

In recent versions of BCI2000, the Operator module provides a global RandomSeed parameter for all modules, unless the RandomSeed parameter is specified on the command line, or otherwise by a core module at startup.

Remarks

The number sequence is generated using a standard linear congruential algorithm, with its parameters chosen as suggested in P. L'Ecuyer: A table of linear congruential generators of different sizes and good lattice structure. Mathematics of Computation, 68(225), 1999.

See also

Programming Reference:ApplicationBase Class, Programming Reference:EnvironmentExtension Class