eoRng Class Reference

Random number generator adapted from (see comments below). 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.
void oldReseed (uint32_t s)
 Re-initializes the Random Number Generator.
double uniform (double m=1.0)
 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 adapted from (see comments below).

The random number generator is modified into a class by Maarten Keijzer (mak@dhi.dk). Also added the Box-Muller transformation to generate normal deviates.

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Contact: eodev-main@lists.sourceforge.net Old contact information: todos@geneura.ugr.es, http://geneura.ugr.es 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 111 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 121 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

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

double eoRng::uniform ( double  m = 1.0  )  [inline]

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

bool eoRng::flip ( double  bias = 0.5  )  [inline]

double eoRng::normal (  )  [inline]

Gaussian deviate.

Zero mean Gaussian deviate with standard deviation 1.

Returns:
Random Gaussian deviate

Definition at line 496 of file eoRNG.h.

References cached, cacheValue, and uniform().

Referenced by normal(), eoNormalMutation< Dummy >::operator()(), eoNormalVecMutation< EOT >::operator()(), eoGaussRealWeightUp::operator()(), and eoEsMutate< EOT >::operator()().

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

Gaussian deviate.

Gaussian deviate with zero mean and specified standard deviation.

Parameters:
stdev Standard deviation for Gaussian distribution
Returns:
Random Gaussian deviate

Definition at line 216 of file eoRNG.h.

References normal().

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

Gaussian deviate.

Gaussian deviate with specified mean and standard deviation.

Parameters:
mean Mean for Gaussian distribution
stdev Standard deviation for Gaussian distribution
Returns:
Random Gaussian deviate

Definition at line 227 of file eoRNG.h.

References normal().

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

Random numbers using a negative exponential distribution.

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

Definition at line 235 of file eoRNG.h.

References rand(), and rand_max().

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

References uniform().

Referenced by eoCombinedInit< 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 279 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 294 of file eoRNG.h.

References random().

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

Print RNG.

Parameters:
_os Stream to print RNG on

Implements eoPrintable.

Definition at line 301 of file eoRNG.h.

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

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

Read RNG.

Parameters:
_is Stream to read RNG from

Implements eoPersistent.

Definition at line 315 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 331 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:

Generated on Thu Jan 1 23:19:50 2009 for EvolvingObjects by  doxygen 1.5.5