STK++ 0.9.13
|
Generate unsigner 32 bits random number using the Merdsenne Twister Algorithm. More...
#include <MersenneTwister.h>
Public Types | |
enum | { N = 624 } |
enum | { SAVE = N + 1 } |
typedef unsigned long | uint32 |
unsigned integer type, at least 32 bits | |
Public Member Functions | |
MTRand (const uint32 &oneSeed) | |
Initialize with a simple uint32. | |
MTRand (uint32 *const bigSeed, uint32 const seedLength=N) | |
Initialize with a an array. | |
MTRand () | |
auto-initialize with /dev/urandom or time() and clock() | |
double | rand () |
real number in [0,1] | |
double | rand (const double &n) |
real number in [0,n] | |
double | randExc () |
real number in [0,1) | |
double | randExc (const double &n) |
real number in [0,n) | |
double | randDblExc () |
real number in (0,1) | |
double | randDblExc (const double &n) |
real number in (0,n) | |
uint32 | randInt () |
integer in [0,2^32-1] | |
double | operator() () |
same as rand(). | |
uint32 | randInt (const uint32 &n) |
integer in [0,n] for n < 2^32 | |
double | rand53 () |
Access to 53-bit random numbers (capacity of IEEE double precision). | |
double | randNorm (const double &mean, const double &variance) |
Access to nonuniform random number distributions. | |
void | seed (const uint32 oneSeed) |
Re-seeding functions with same behavior as initializers. | |
void | seed (uint32 *const bigSeed, const uint32 seedLength) |
Re-seeding functions with same behavior as initializers. | |
void | seed () |
Re-seeding functions with same behavior as initializers. | |
void | save (uint32 *saveArray) const |
Saving and loading generator state. | |
void | load (uint32 *const loadArray) |
Saving and loading generator state. | |
Protected Types | |
enum | { M = 397 } |
Protected Member Functions | |
void | initialize (const uint32 seed) |
Initialize generator state with seed See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier. | |
void | reload () |
Generate N new values in state Made disposeer and faster by Matthew Bellew (matth.nosp@m.ew.b.nosp@m.ellew.nosp@m.@hom.nosp@m.e.com) | |
uint32 | hiBit (const uint32 &u) const |
High bit. | |
uint32 | loBit (const uint32 &u) const |
low bit. | |
uint32 | loBits (const uint32 &u) const |
low bits. | |
uint32 | mixBits (const uint32 &u, const uint32 &v) const |
mixed bits. | |
uint32 | twist (const uint32 &m, const uint32 &s0, const uint32 &s1) const |
twisted values. | |
Static Protected Member Functions | |
static uint32 | hash (time_t t, clock_t c) |
Get a uint32 from t and c Better than uint32(x) in case x is floating point in [0,1] Based on code by Lawrence Kirby (fred@.nosp@m.gene.nosp@m.sis.d.nosp@m.emon.nosp@m..co.u.nosp@m.k) | |
Protected Attributes | |
uint32 | state [N] |
internal state | |
uint32 * | pNext |
Next value to get from state. | |
int | left |
number of values left before reload needed | |
Generate unsigner 32 bits random number using the Merdsenne Twister Algorithm.
The Mersenne Twister is an algorithm for generating random numbers. It was designed with consideration of the flaws in various other generators. The period, 2^19937-1, and the order of equidistribution, 623 dimensions, are far greater. The generator is also fast; it avoids multiplication and division, and it benefits from caches and pipelines. For more information see the inventors' web page at http://www.math.keio.ac.jp/~matumoto/emt.html
Reference M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
Do NOT use for CRYPTOGRAPHY without securely hashing several returned values together, otherwise the generator state can be learned after reading 624 consecutive values. Access to 32-bit random numbers
Not thread safe (unless auto-initialization is avoided and each thread has its own MTRand object)
Definition at line 87 of file MersenneTwister.h.
typedef unsigned long MTRand::uint32 |
unsigned integer type, at least 32 bits
Definition at line 92 of file MersenneTwister.h.
anonymous enum |
anonymous enum |
|
protected |
|
inline |
Initialize with a simple uint32.
Definition at line 107 of file MersenneTwister.h.
References seed().
Initialize with a an array.
Definition at line 111 of file MersenneTwister.h.
References seed().
|
inline |
auto-initialize with /dev/urandom or time() and clock()
Definition at line 115 of file MersenneTwister.h.
References seed().
|
inlinestaticprotected |
Get a uint32 from t and c Better than uint32(x) in case x is floating point in [0,1] Based on code by Lawrence Kirby (fred@.nosp@m.gene.nosp@m.sis.d.nosp@m.emon.nosp@m..co.u.nosp@m.k)
Definition at line 345 of file MersenneTwister.h.
Referenced by seed().
High bit.
Definition at line 322 of file MersenneTwister.h.
Referenced by mixBits().
|
inlineprotected |
Initialize generator state with seed See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier.
In previous versions, most significant bits (MSBs) of the seed affect only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto.
Definition at line 292 of file MersenneTwister.h.
|
inline |
Saving and loading generator state.
from such array
Definition at line 275 of file MersenneTwister.h.
low bit.
Definition at line 326 of file MersenneTwister.h.
Referenced by twist().
low bits.
Definition at line 330 of file MersenneTwister.h.
Referenced by mixBits().
mixed bits.
Definition at line 334 of file MersenneTwister.h.
References hiBit(), and loBits().
Referenced by twist().
|
inline |
same as rand().
Definition at line 148 of file MersenneTwister.h.
References rand().
Referenced by STK::RandBase::operator()().
|
inline |
real number in [0,1]
Definition at line 119 of file MersenneTwister.h.
References randInt().
Referenced by operator()(), STK::RandBase::rand(), and STK::RandBase::rand().
|
inline |
real number in [0,n]
Definition at line 121 of file MersenneTwister.h.
References rand().
Referenced by rand().
|
inline |
Access to 53-bit random numbers (capacity of IEEE double precision).
real number in [0,1)
Definition at line 174 of file MersenneTwister.h.
References randInt().
|
inline |
real number in (0,1)
Definition at line 127 of file MersenneTwister.h.
References randInt().
Referenced by STK::RandBase::randDblExc(), STK::RandBase::randDblExc(), and randNorm().
|
inline |
real number in (0,n)
Definition at line 129 of file MersenneTwister.h.
References randDblExc().
Referenced by randDblExc().
|
inline |
real number in [0,1)
Definition at line 123 of file MersenneTwister.h.
References randInt().
Referenced by STK::RandBase::randExc(), STK::RandBase::randExc(), and randNorm().
|
inline |
real number in [0,n)
Definition at line 125 of file MersenneTwister.h.
References randExc().
Referenced by randExc().
|
inline |
integer in [0,2^32-1]
Definition at line 132 of file MersenneTwister.h.
References left, pNext, and reload().
Referenced by rand(), rand53(), randDblExc(), STK::RandBase::randDiscreteUnif(), randExc(), STK::RandBase::randGauss(), and randInt().
integer in [0,n] for n < 2^32
Definition at line 151 of file MersenneTwister.h.
References randInt().
|
inline |
Access to nonuniform random number distributions.
Return a real number from a normal (Gaussian) distribution with given mean and variance by Box-Muller method.
Definition at line 184 of file MersenneTwister.h.
References randDblExc(), and randExc().
|
inlineprotected |
Generate N new values in state Made disposeer and faster by Matthew Bellew (matth.nosp@m.ew.b.nosp@m.ellew.nosp@m.@hom.nosp@m.e.com)
Definition at line 309 of file MersenneTwister.h.
|
inline |
|
inline |
Re-seeding functions with same behavior as initializers.
Seed the generator with an array from /dev/urandom if available Otherwise use a hash of time() and clock() values
Definition at line 240 of file MersenneTwister.h.
References hash(), N, and seed().
Referenced by initialize(), MTRand(), MTRand(), MTRand(), STK::RandBase::RandBase(), and seed().
|
inline |
Re-seeding functions with same behavior as initializers.
Seed the generator with a simple uint32
Definition at line 193 of file MersenneTwister.h.
References initialize(), and reload().
Re-seeding functions with same behavior as initializers.
Seed the generator with an array of uint32's There are 2^19937-1 possible initial states. This function allows all of those to be accessed by providing at least 19937 bits (with a default seed length of N = 624 uint32's). Any bits above the lower 32 in each element are discarded. Just call seed() if you want to get array from /dev/urandom
Definition at line 207 of file MersenneTwister.h.
References initialize(), N, reload(), and state.
|
inlineprotected |
twisted values.
Definition at line 338 of file MersenneTwister.h.
References loBit(), and mixBits().
Referenced by reload().
|
protected |
|
protected |
Next value to get from state.
Definition at line 101 of file MersenneTwister.h.
internal state
Definition at line 100 of file MersenneTwister.h.
Referenced by initialize(), load(), reload(), save(), and seed().