#include <utils/eoRNG.h>
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. | |
eoRng & | operator= (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. |
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
.
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.
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.
eoRng::eoRng | ( | uint32_t | s | ) | [inline] |
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.
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
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.
double eoRng::uniform | ( | double | m = 1.0 |
) | [inline] |
Random number from unifom distribution.
m | Define interval for random number to [0, m) |
Definition at line 169 of file eoRNG.h.
References rand(), and rand_max().
Referenced by eoEsChromInit< EOT >::create_self_adapt(), flip(), normal(), eoVariableInertiaWeightedVelocity< POT >::operator()(), eoUniformInit< T >::operator()(), eoStandardVelocity< POT >::operator()(), eoSigBinaryFlight< POT >::operator()(), eoUniformGenerator< T >::operator()(), eoSegmentCrossover< EOT >::operator()(), eoDetUniformMutation< EOT >::operator()(), eoUniformMutation< EOT >::operator()(), eoRandomRealWeightUp::operator()(), eoProportionalSelect< EOT >::operator()(), eoIntegerVelocity< POT >::operator()(), eoFixedInertiaWeightedVelocity< POT >::operator()(), eoExtendedVelocity< POT >::operator()(), eoConstrictedVelocity< POT >::operator()(), eoConstrictedVariableWeightVelocity< POT >::operator()(), random(), roulette_wheel(), and eoStochasticUniversalSelect< EOT >::setup().
uint32_t eoRng::random | ( | uint32_t | m | ) | [inline] |
Random integer number from unifom distribution.
m | Define interval for random number to [0, m) |
Definition at line 179 of file eoRNG.h.
References uniform().
Referenced by choice(), eoVelocityInitVariableLength< POT >::operator()(), eoVlAddMutation< EOT >::operator()(), eoSymInit< EoType >::operator()(), eoStHoistMutation< FType, Node >::operator()(), eoStPointMutation< FType, Node >::operator()(), eoStBranchMutation< FType, Node >::operator()(), eoStSubtreeXOver< FType, Node >::operator()(), eoDetUniformMutation< EOT >::operator()(), eoHoistMutation< FType, Node >::operator()(), eoCollapseSubtreeMutation< FType, Node >::operator()(), eoExpansionMutation< FType, Node >::operator()(), eoPointMutation< FType, Node >::operator()(), eoBranchMutation< FType, Node >::operator()(), eoSubtreeXOver< FType, Node >::operator()(), eoFlOrKMutation< EOT >::operator()(), and eoStochasticUniversalSelect< EOT >::setup().
bool eoRng::flip | ( | double | bias = 0.5 |
) | [inline] |
Biased coin toss.
This tosses a biased coin such that flip(x/100.0) will true x% of the time
bias | The coins' bias (the x above) |
Definition at line 196 of file eoRNG.h.
References uniform().
Referenced by eoInnerExchangeQuadOp< EOT >::operator()(), eoSymInit< EoType >::operator()(), eoDynSGATransform< EOT >::operator()(), eoSGATransform< EOT >::operator()(), eoUniformGenerator< T >::operator()(), eoUniformMutation< EOT >::operator()(), eoOneMaxInit< GenotypeT >::operator()(), eoNormalMutation< Dummy >::operator()(), eoNormalVecMutation< EOT >::operator()(), eoFlOrAllAtomQuadOp< EOT >::operator()(), and eoFlOrAllAtomBinOp< EOT >::operator()().
double eoRng::normal | ( | ) | [inline] |
Gaussian deviate.
Zero mean Gaussian deviate with standard deviation 1.
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] |
double eoRng::normal | ( | double | mean, | |
double | stdev | |||
) | [inline] |
double eoRng::negexp | ( | double | mean | ) | [inline] |
Random numbers using a negative exponential distribution.
mean | Mean value of distribution |
Definition at line 235 of file eoRNG.h.
References rand(), and rand_max().
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()().
const TYPE& eoRng::choice | ( | const std::vector< TYPE > & | vec | ) | [inline] |
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.
Definition at line 294 of file eoRNG.h.
References random().
void eoRng::printOn | ( | std::ostream & | _os | ) | const [inline, virtual] |
Print RNG.
_os | Stream to print RNG on |
Implements eoPrintable.
void eoRng::readFrom | ( | std::istream & | _is | ) | [inline, virtual] |
Read RNG.
_is | Stream to read RNG from |
Implements eoPersistent.
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.