Programming Reference:RandomGenerator Class: Difference between revisions
No edit summary |
|||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
==Location== | ==Location== | ||
<tt>BCI2000/src/shared/utils | <tt>BCI2000/src/shared/utils</tt> | ||
==Synopsis== | ==Synopsis== | ||
| Line 27: | Line 27: | ||
===int operator()(int N)=== | ===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 <tt>RandomGenerator</tt> as a [http://www.sgi.com/tech/stl/functors.html functor], i.e. an object that may be used as if it were a function name. A typical application would be to specify a <tt>RandomGenerator</tt> object as a third argument to the STL's [http://www.dinkumware.com/manuals/?manual=compleat&page=algorith.html#random_shuffle <tt>std::random_shuffle()</tt>] function. | 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 <tt>RandomGenerator</tt> as a [http://www.sgi.com/tech/stl/functors.html functor], i.e. an object that may be used as if it were a function name. A typical application would be to specify a <tt>RandomGenerator</tt> object as a third argument to the STL's [http://www.dinkumware.com/manuals/?manual=compleat&page=algorith.html#random_shuffle <tt>std::random_shuffle()</tt>] 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== | ==Remarks== | ||
Latest revision as of 15:38, 29 June 2012
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