EvolvingObjects
eoRng Class Reference

Random Number Generator. More...

#include <utils/eoRNG.h>

Inheritance diagram for eoRng:
eoObject eoPersistent eoPrintable

List of all members.

Public Member Functions

 eoRng (uint32_t s)
 Constructor.
 ~eoRng ()
 Destructor.
void reseed (uint32_t s)
 Re-initializes the Random Number Generator.
double uniform (double m=1.0)
 Random number from unifom distribution.
double uniform (double min, double max)
 Random number from unifom distribution.
uint32_t random (uint32_t m)
 Random integer number from unifom distribution.
bool flip (double bias=0.5)
 Biased coin toss.
double normal ()
 Gaussian deviate.
double normal (double stdev)
 Gaussian deviate.
double normal (double mean, double stdev)
 Gaussian deviate.
double negexp (double mean)
 Random numbers using a negative exponential distribution.
uint32_t rand ()
 rand() returns a random number in the range [0, rand_max)
uint32_t rand_max () const
 rand_max() the maximum returned by rand()
template<typename TYPE >
int roulette_wheel (const std::vector< TYPE > &vec, TYPE total=0)
 Roulette wheel selection.
template<typename TYPE >
const TYPE & choice (const std::vector< TYPE > &vec)
 Randomly select element from vector.
template<typename TYPE >
TYPE & choice (std::vector< TYPE > &vec)
 Randomly select element from vector.
void printOn (std::ostream &_os) const
 Print RNG.
void readFrom (std::istream &_is)
 Read RNG.
std::string className () const
 Return the class id.

Private Member Functions

uint32_t restart ()
void initialize (uint32_t seed)
 eoRng (const eoRng &)
 Copy constructor.
eoRngoperator= (const eoRng &)
 Assignment operator.

Private Attributes

uint32_t * state
 Array for the state.
uint32_t * next
 Pointer to next available random number.
int left
 Number of random numbers currently left.
bool cached
 Is there a valid cached value for the normal distribution?
double cacheValue
 Cached value for normal distribution?

Static Private Attributes

static const int N
 Size of the state-vector.
static const int M
 Internal constant.
static const uint32_t K
 Magic constant.

Detailed Description

Random Number Generator.

eoRng is a persistent class that uses the ``Mersenne Twister'' random number generator MT19937 for generating random numbers. The various member functions implement useful functions for evolutionary algorithms. Included are: rand(), random(), flip() and normal().

EO provides a global random number generator rng that is seeded by the current UNIX time at program start. Moreover some global convenience functions are provided that use the global random number generator: random, normal.

Warning:
If you want to repeatedly generated the same sequence of pseudo-random numbers, you should always reseed the generator at the beginning of your code.

Documentation in original file

This is the ``Mersenne Twister'' random number generator MT19937, which generates pseudorandom integers uniformly distributed in 0..(2^32 - 1) starting from any odd seed in 0..(2^32 - 1). This version is a recode by Shawn Cokus (Cokus@math.washington.edu) on March 8, 1998 of a version by Takuji Nishimura (who had suggestions from Topher Cooper and Marc Rieffel in July-August 1997).

Effectiveness of the recoding (on Goedel2.math.washington.edu, a DEC Alpha running OSF/1) using GCC -O3 as a compiler: before recoding: 51.6 sec. to generate 300 million random numbers; after recoding: 24.0 sec. for the same (i.e., 46.5% of original time), so speed is now about 12.5 million random number generations per second on this machine.

According to the URL <http://www.math.keio.ac.jp/~matumoto/emt.html> (and paraphrasing a bit in places), the Mersenne Twister is ``designed with consideration of the flaws of various existing generators,'' has a period of 2^19937 - 1, gives a sequence that is 623-dimensionally equidistributed, and ``has passed many std::stringent tests, including the die-hard test of G. Marsaglia and the load test of P. Hellekalek and S. Wegenkittl.'' It is efficient in memory usage (typically using 2506 to 5012 bytes of static data, depending on data type sizes, and the code is quite short as well). It generates random numbers in batches of 624 at a time, so the caching and pipelining of modern systems is exploited. It is also divide- and mod-free.

The code as Shawn received it included the following notice: Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. When you use this, send an e-mail to <matumoto@math.keio.ac.jp> with an appropriate reference to your work. It would be nice to Cc: <Cokus@math.washington.edu> and <eodev-main@lists.sourceforge.net> when you write.

Portability

Note for people porting EO to other platforms: please make sure that the type uint32_t in the file eoRNG.h is exactly 32 bits long. It may in principle be longer, but not shorter. If it is longer, file compatibility between EO on different platforms may be broken.

Definition at line 115 of file eoRNG.h.


Constructor & Destructor Documentation

eoRng::eoRng ( uint32_t  s) [inline]

Constructor.

Parameters:
sRandom seed; if you want another seed, use reseed.
See also:
reseed for details on usage of the seeding value.

Definition at line 125 of file eoRNG.h.

References N, and state.

eoRng::eoRng ( const eoRng ) [private]

Copy constructor.

Private copy ctor and assignment operator to make sure that nobody accidentally copies the random number generator. If you want similar RNG's, make two RNG's and initialize them with the same seed.

As it cannot be called, we do not provide an implementation.


Member Function Documentation

void eoRng::reseed ( uint32_t  s) [inline]

Re-initializes the Random Number Generator.

WARNING: Jeroen Eggermont <jeggermo@liacs.nl> noticed that initialize does not differentiate between odd and even numbers, therefore the argument to reseed is now doubled before being passed on.

Manually divide the seed by 2 if you want to re-run old runs

Version:
MS. 5 Oct. 2001
Examples:
t-eoGenOp.cpp, t-eoRingTopology.cpp, t-eoRoulette.cpp, t-eoSharing.cpp, and t-eoStateAndParser.cpp.

Definition at line 148 of file eoRNG.h.

Referenced by do_make_pop().

double eoRng::uniform ( double  min,
double  max 
) [inline]

Random number from unifom distribution.

Parameters:
minDefine minimum for interval in the range [min, max)
maxDefine maximum for interval in the range [min, max)
Returns:
random number in the range [min, max)

Definition at line 186 of file eoRNG.h.

References uniform().

uint32_t eoRng::random ( uint32_t  m) [inline]

Random integer number from unifom distribution.

Parameters:
mDefine interval for random number to [0, m)
Returns:
random integer in the range [0, m)
Examples:
t-eoExternalEO.cpp, t-eoSSGA.cpp, and t-eoStateAndParser.cpp.

Definition at line 196 of file eoRNG.h.

References uniform().

Referenced by choice(), eoOrderXover< Chrom >::operator()(), eoRandomSelect< EOT >::operator()(), eoTwoOptMutation< EOT >::operator()(), eoOneBitFlip< Chrom >::operator()(), eoShiftMutation< EOT >::operator()(), eoSwapMutation< Chrom >::operator()(), eoSubtreeXOver< FType, Node >::operator()(), eoVlAddMutation< EOT >::operator()(), eoStSubtreeXOver< FType, Node >::operator()(), eoDetBitFlip< Chrom >::operator()(), eoFlOrKMutation< EOT >::operator()(), eoFlOrKAtomQuadOp< EOT >::operator()(), eoUniformGeneChooser< EOT >::operator()(), eoFlOrKAtomBinOp< EOT >::operator()(), eoBranchMutation< FType, Node >::operator()(), eoEPReduce< EOT >::operator()(), eoDetSingleBitFlip< Chrom >::operator()(), eoVelocityInitVariableLength< POT >::operator()(), eoInitVariableLength< EOT >::operator()(), eoStBranchMutation< FType, Node >::operator()(), eoPointMutation< FType, Node >::operator()(), eoFlOr1ptQuadOp< EOT >::operator()(), eoFlOr1ptBinOp< EOT >::operator()(), eoDetUniformMutation< EOT >::operator()(), eoBitInversion< Chrom >::operator()(), eoExpansionMutation< FType, Node >::operator()(), eoStPointMutation< FType, Node >::operator()(), eoStHoistMutation< FType, Node >::operator()(), eoCollapseSubtreeMutation< FType, Node >::operator()(), eo1PtBitXover< Chrom >::operator()(), eoHoistMutation< FType, Node >::operator()(), eoBitGxOver< Chrom >::operator()(), and eoStochasticUniversalSelect< EOT >::setup().

double eoRng::normal ( ) [inline]

Gaussian deviate.

Zero mean Gaussian deviate with standard deviation 1. Note: Use the Marsaglia polar method.

Returns:
Random Gaussian deviate
Examples:
t-eoRNG.cpp.

Definition at line 517 of file eoRNG.h.

References cached, cacheValue, and uniform().

Referenced by eoGaussRealWeightUp::operator()(), eoNormalVecMutation< EOT >::operator()(), eoEsMutate< EOT >::operator()(), eoNormalGenerator< T >::operator()(), eoNormalInit< T >::operator()(), and eoNormalMutation< EOT >::operator()().

double eoRng::normal ( double  stdev) [inline]

Gaussian deviate.

Gaussian deviate with zero mean and specified standard deviation.

Parameters:
stdevStandard deviation for Gaussian distribution
Returns:
Random Gaussian deviate

Definition at line 234 of file eoRNG.h.

References normal().

Referenced by normal().

double eoRng::normal ( double  mean,
double  stdev 
) [inline]

Gaussian deviate.

Gaussian deviate with specified mean and standard deviation.

Parameters:
meanMean for Gaussian distribution
stdevStandard deviation for Gaussian distribution
Returns:
Random Gaussian deviate

Definition at line 245 of file eoRNG.h.

References normal().

Referenced by normal().

double eoRng::negexp ( double  mean) [inline]

Random numbers using a negative exponential distribution.

Parameters:
meanMean value of distribution
Returns:
Random number from a negative exponential distribution

Definition at line 253 of file eoRNG.h.

References rand(), and rand_max().

Referenced by eoNegExpGenerator< T >::operator()(), and eoNegExpInit< T >::operator()().

template<typename TYPE >
int eoRng::roulette_wheel ( const std::vector< TYPE > &  vec,
TYPE  total = 0 
) [inline]

Roulette wheel selection.

roulette_wheel(vec, total = 0) does a roulette wheel selection on the input std::vector vec. If the total is not supplied, it is calculated. It returns an integer denoting the selected argument.

Definition at line 275 of file eoRNG.h.

References uniform().

Referenced by eoProportionalOp< EOT >::apply(), eoCombinedInit< EOT >::operator()(), eoPropCombinedMonOp< EOT >::operator()(), eoPropCombinedBinOp< EOT >::operator()(), and eoPropCombinedQuadOp< EOT >::operator()().

template<typename TYPE >
const TYPE& eoRng::choice ( const std::vector< TYPE > &  vec) [inline]

Randomly select element from vector.

Returns:
Uniformly chosen element from the vector.

Definition at line 297 of file eoRNG.h.

References random().

template<typename TYPE >
TYPE& eoRng::choice ( std::vector< TYPE > &  vec) [inline]

Randomly select element from vector.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Provide a version returning a non-const element reference.

Returns:
Uniformly chosen element from the vector.
Warning:
Changing the return value does alter the vector.

Definition at line 312 of file eoRNG.h.

References random().

void eoRng::printOn ( std::ostream &  _os) const [inline, virtual]

Print RNG.

Parameters:
_osStream to print RNG on

Implements eoPrintable.

Definition at line 319 of file eoRNG.h.

References cached, cacheValue, left, N, next, and state.

void eoRng::readFrom ( std::istream &  _is) [inline, virtual]

Read RNG.

Parameters:
_isStream to read RNG from

Implements eoPersistent.

Definition at line 333 of file eoRNG.h.

References cached, cacheValue, left, N, next, and state.

std::string eoRng::className ( ) const [inline, virtual]

Return the class id.

This should be redefined in each class. Only "leaf" classes can be non-virtual.

Maarten: removed the default implementation as this proved to be too error-prone: I found several classes that had a typo in className (like classname), which would print eoObject instead of their own. Having it pure will force the implementor to provide a name.

Implements eoObject.

Definition at line 349 of file eoRNG.h.

eoRng& eoRng::operator= ( const eoRng ) [private]

Assignment operator.

See also:
Copy constructor eoRng(const eoRng&).

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Friends