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.
void reseed (uint32_t s)
 Re-initializes the Random Number Generator.
void oldReseed (uint32_t s)
 Re-initializes the Random Number Generator.
double uniform (double m=1.0)
 uniform(m = 1.0) returns a random double in the range [0, m)
uint32_t random (uint32_t m)
 random() returns a random integer in the range [0, m)
bool flip (float bias=0.5)
 flip() tosses a biased coin such that flip(x/100.0) will returns true x% of the time
double normal (void)
 normal() zero mean gaussian deviate with standard deviation of 1
double normal (double stdev)
 normal(stdev) zero mean gaussian deviate with user defined standard deviation
double normal (double mean, double stdev)
 normal(mean, stdev) user defined mean gaussian deviate with user defined standard deviation
double negexp (double mean)
 Generates 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 (void) const
 rand_max() the maximum returned by rand()
template<typename TYPE>
int roulette_wheel (const std::vector< TYPE > &vec, TYPE total=0)
 roulette_wheel(vec, total = 0) does a roulette wheel selection on the input std::vector vec.
template<typename TYPE>
const TYPE & choice (const std::vector< TYPE > &vec) const
 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
 Write object.
void readFrom (std::istream &_is)
 Read object.
std::string className (void) const
 Return the class id.

Private Member Functions

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

Private Attributes

uint32_t * state
 Array for the state.
uint32_t * next
int left
bool cached
float cacheValue
const int N
const int M
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 103 of file eoRNG.h.


Constructor & Destructor Documentation

eoRng::eoRng uint32_t  s  )  [inline]
 

Constructor.

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

Definition at line 113 of file eoRNG.h.

References K, 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

Definition at line 135 of file eoRNG.h.

void eoRng::oldReseed uint32_t  s  )  [inline]
 

Re-initializes the Random Number Generator.

This is the traditional seeding procedure. This version is deprecated and only provided for compatibility with old code. In new projects you should use reseed.

See also:
reseed for details on usage of the seeding value.
Version:
old version (deprecated)

Definition at line 150 of file eoRNG.h.

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

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 225 of file eoRNG.h.

References uniform().

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

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

Randomly select element from vector.

Returns:
Uniformly chosen element from the vector.

Definition at line 247 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 262 of file eoRNG.h.

References random().

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

Write object.

It's called printOn since it prints the object on a stream.

Parameters:
_os A std::ostream.

Implements eoPrintable.

Definition at line 266 of file eoRNG.h.

References state.

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

Read object.

Parameters:
_is A std::istream.
Exceptions:
runtime_std::exception If a valid object can't be read.

Implements eoPersistent.

Definition at line 277 of file eoRNG.h.

References state.

std::string eoRng::className void   )  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 293 of file eoRNG.h.

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

Assignmant operator.

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


The documentation for this class was generated from the following file:
Generated on Thu Oct 19 05:06:55 2006 for EO by  doxygen 1.3.9.1