MTRand Class Reference

#include <MersenneTwister.h>


Detailed Description

Definition at line 74 of file MersenneTwister.h.


Public Types

enum  { N = 624 }
enum  { SAVE = N + 1 }
typedef unsigned long uint32

Public Member Functions

void load (uint32 *const loadArray)
 MTRand ()
 MTRand (uint32 *const bigSeed, uint32 const seedLength=N)
 MTRand (const uint32 &oneSeed)
double operator() ()
double rand (const double &n)
double rand ()
double rand53 ()
double randDblExc (const double &n)
double randDblExc ()
double randExc (const double &n)
double randExc ()
uint32 randInt (const uint32 &n)
uint32 randInt ()
double randNorm (const double &mean=0.0, const double &variance=0.0)
void save (uint32 *saveArray) const
void seed ()
void seed (uint32 *const bigSeed, const uint32 seedLength=N)
void seed (const uint32 oneSeed)

Protected Types

enum  { M = 397 }

Protected Member Functions

uint32 hiBit (const uint32 &u) const
void initialize (const uint32 oneSeed)
uint32 loBit (const uint32 &u) const
uint32 loBits (const uint32 &u) const
uint32 mixBits (const uint32 &u, const uint32 &v) const
void reload ()
uint32 twist (const uint32 &m, const uint32 &s0, const uint32 &s1) const

Static Protected Member Functions

static uint32 hash (time_t t, clock_t c)

Protected Attributes

int left
uint32pNext
uint32 state [N]

Friends

std::ostream & operator<< (std::ostream &os, const MTRand &mtrand)
std::istream & operator>> (std::istream &is, MTRand &mtrand)

Member Typedef Documentation

typedef unsigned long MTRand::uint32

Definition at line 77 of file MersenneTwister.h.


Member Enumeration Documentation

anonymous enum

Enumerator:
N 

Definition at line 79 of file MersenneTwister.h.

00079 { N = 624 };       // length of state vector

anonymous enum

Enumerator:
SAVE 

Definition at line 80 of file MersenneTwister.h.

00080 { SAVE = N + 1 };  // length of array for save()

anonymous enum [protected]

Enumerator:
M 

Definition at line 83 of file MersenneTwister.h.

00083 { M = 397 };  // period parameter


Constructor & Destructor Documentation

MTRand::MTRand ( const uint32 oneSeed  )  [inline]

Definition at line 142 of file MersenneTwister.h.

References seed().

00143     { seed(oneSeed); }

MTRand::MTRand ( uint32 *const   bigSeed,
uint32 const  seedLength = N 
) [inline]

Definition at line 145 of file MersenneTwister.h.

References seed().

00146     { seed(bigSeed,seedLength); }

MTRand::MTRand (  )  [inline]

Definition at line 148 of file MersenneTwister.h.

References seed().

00149     { seed(); }


Member Function Documentation

MTRand::uint32 MTRand::hash ( time_t  t,
clock_t  c 
) [inline, static, protected]

Definition at line 323 of file MersenneTwister.h.

Referenced by seed().

00324 {
00325     // Get a uint32 from t and c
00326     // Better than uint32(x) in case x is floating point in [0,1]
00327     // Based on code by Lawrence Kirby (fred@genesis.demon.co.uk)
00328 
00329     static uint32 differ = 0;  // guarantee time-based seeds will change
00330 
00331     uint32 h1 = 0;
00332     unsigned char *p = (unsigned char *) &t;
00333     for( size_t i = 0; i < sizeof(t); ++i )
00334     {
00335         h1 *= UCHAR_MAX + 2U;
00336         h1 += p[i];
00337     }
00338     uint32 h2 = 0;
00339     p = (unsigned char *) &c;
00340     for( size_t j = 0; j < sizeof(c); ++j )
00341     {
00342         h2 *= UCHAR_MAX + 2U;
00343         h2 += p[j];
00344     }
00345     return ( h1 + differ++ ) ^ h2;
00346 }

uint32 MTRand::hiBit ( const uint32 u  )  const [inline, protected]

Definition at line 131 of file MersenneTwister.h.

Referenced by mixBits().

00131 { return u & 0x80000000UL; }

void MTRand::initialize ( const uint32  oneSeed  )  [inline, protected]

Definition at line 289 of file MersenneTwister.h.

References N, and state.

Referenced by seed().

00290 {
00291     // Initialize generator state with seed
00292     // See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier.
00293     // In previous versions, most significant bits (MSBs) of the seed affect
00294     // only MSBs of the state array.  Modified 9 Jan 2002 by Makoto Matsumoto.
00295     register uint32 *s = state;
00296     register uint32 *r = state;
00297     register int i = 1;
00298     *s++ = seed & 0xffffffffUL;
00299     for( ; i < N; ++i )
00300     {
00301         *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
00302         r++;
00303     }
00304 }

void MTRand::load ( uint32 *const   loadArray  )  [inline]

Definition at line 359 of file MersenneTwister.h.

References left, N, pNext, and state.

00360 {
00361     register uint32 *s = state;
00362     register uint32 *la = loadArray;
00363     register int i = N;
00364     for( ; i--; *s++ = *la++ ) {}
00365     left = *la;
00366     pNext = &state[N-left];
00367 }

uint32 MTRand::loBit ( const uint32 u  )  const [inline, protected]

Definition at line 132 of file MersenneTwister.h.

Referenced by twist().

00132 { return u & 0x00000001UL; }

uint32 MTRand::loBits ( const uint32 u  )  const [inline, protected]

Definition at line 133 of file MersenneTwister.h.

Referenced by mixBits().

00133 { return u & 0x7fffffffUL; }

uint32 MTRand::mixBits ( const uint32 u,
const uint32 v 
) const [inline, protected]

Definition at line 134 of file MersenneTwister.h.

References hiBit(), and loBits().

Referenced by twist().

00135         { return hiBit(u) | loBits(v); }

double MTRand::operator() (  )  [inline]

Definition at line 109 of file MersenneTwister.h.

References rand().

00109 { return rand(); }  // same as rand()

double MTRand::rand ( const double &  n  )  [inline]

Definition at line 154 of file MersenneTwister.h.

References rand().

00155     { return rand() * n; }

double MTRand::rand (  )  [inline]

Definition at line 151 of file MersenneTwister.h.

References randInt().

Referenced by operator()(), and rand().

00152     { return double(randInt()) * (1.0/4294967295.0); }

double MTRand::rand53 (  )  [inline]

Definition at line 169 of file MersenneTwister.h.

References randInt().

00170 {
00171     uint32 a = randInt() >> 5, b = randInt() >> 6;
00172     return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);  // by Isaku Wada
00173 }

double MTRand::randDblExc ( const double &  n  )  [inline]

Definition at line 166 of file MersenneTwister.h.

References randDblExc().

00167     { return randDblExc() * n; }

double MTRand::randDblExc (  )  [inline]

Definition at line 163 of file MersenneTwister.h.

References randInt().

Referenced by randDblExc(), and randNorm().

00164     { return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); }

double MTRand::randExc ( const double &  n  )  [inline]

Definition at line 160 of file MersenneTwister.h.

References randExc().

00161     { return randExc() * n; }

double MTRand::randExc (  )  [inline]

Definition at line 157 of file MersenneTwister.h.

References randInt().

Referenced by RandHelper::rand(), randExc(), and randNorm().

00158     { return double(randInt()) * (1.0/4294967296.0); }

MTRand::uint32 MTRand::randInt ( const uint32 n  )  [inline]

Definition at line 200 of file MersenneTwister.h.

References randInt().

00201 {
00202     // Find which bits are used in n
00203     // Optimized by Magnus Jonsson (magnus@smartelectronix.com)
00204     uint32 used = n;
00205     used |= used >> 1;
00206     used |= used >> 2;
00207     used |= used >> 4;
00208     used |= used >> 8;
00209     used |= used >> 16;
00210     
00211     // Draw numbers until one is found in [0,n]
00212     uint32 i;
00213     do
00214         i = randInt() & used;  // toss unused bits to shorten search
00215     while( i > n );
00216     return i;
00217 }

MTRand::uint32 MTRand::randInt (  )  [inline]

Definition at line 184 of file MersenneTwister.h.

References left, pNext, and reload().

Referenced by RandHelper::rand(), rand(), rand53(), randDblExc(), randExc(), and randInt().

00185 {
00186     // Pull a 32-bit integer from the generator state
00187     // Every other access function simply transforms the numbers extracted here
00188     
00189     if( left == 0 ) reload();
00190     --left;
00191         
00192     register uint32 s1;
00193     s1 = *pNext++;
00194     s1 ^= (s1 >> 11);
00195     s1 ^= (s1 <<  7) & 0x9d2c5680UL;
00196     s1 ^= (s1 << 15) & 0xefc60000UL;
00197     return ( s1 ^ (s1 >> 18) );
00198 }

double MTRand::randNorm ( const double &  mean = 0.0,
const double &  variance = 0.0 
) [inline]

Definition at line 175 of file MersenneTwister.h.

References randDblExc(), and randExc().

Referenced by RandHelper::randNorm().

00176 {
00177     // Return a real number from a normal (Gaussian) distribution with given
00178     // mean and variance by Box-Muller method
00179     double r = sqrt( -2.0 * log( 1.0-randDblExc()) ) * variance;
00180     double phi = 2.0 * 3.14159265358979323846264338328 * randExc();
00181     return mean + r * cos(phi);
00182 }

void MTRand::reload (  )  [inline, protected]

Definition at line 307 of file MersenneTwister.h.

References left, M, N, pNext, state, and twist().

Referenced by randInt(), and seed().

00308 {
00309     // Generate N new values in state
00310     // Made clearer and faster by Matthew Bellew (matthew.bellew@home.com)
00311     register uint32 *p = state;
00312     register int i;
00313     for( i = N - M; i--; ++p )
00314         *p = twist( p[M], p[0], p[1] );
00315     for( i = M; --i; ++p )
00316         *p = twist( p[M-N], p[0], p[1] );
00317     *p = twist( p[M-N], p[0], state[0] );
00318 
00319     left = N, pNext = state;
00320 }

void MTRand::save ( uint32 saveArray  )  const [inline]

Definition at line 349 of file MersenneTwister.h.

References left, N, and state.

00350 {
00351     register uint32 *sa = saveArray;
00352     register const uint32 *s = state;
00353     register int i = N;
00354     for( ; i--; *sa++ = *s++ ) {}
00355     *sa = left;
00356 }

void MTRand::seed (  )  [inline]

Definition at line 265 of file MersenneTwister.h.

References hash(), and N.

Referenced by MTRand().

00266 {
00267     // Seed the generator with an array from /dev/urandom if available
00268     // Otherwise use a hash of time() and clock() values
00269     
00270     // First try getting an array from /dev/urandom
00271     FILE* urandom = fopen( "/dev/urandom", "rb" );
00272     if( urandom )
00273     {
00274         uint32 bigSeed[N];
00275         register uint32 *s = bigSeed;
00276         register int i = N;
00277         register bool success = true;
00278         while( success && i-- )
00279             success = fread( s++, sizeof(uint32), 1, urandom )!=0; // !!! dk 16.02.2007
00280         fclose(urandom);
00281         if( success ) { seed( bigSeed, N );  return; }
00282     }
00283     
00284     // Was not successful, so use time() and clock() instead
00285     seed( hash( time(NULL), clock() ) );
00286 }

void MTRand::seed ( uint32 *const   bigSeed,
const uint32  seedLength = N 
) [inline]

Definition at line 228 of file MersenneTwister.h.

References initialize(), N, reload(), and state.

00229 {
00230     // Seed the generator with an array of uint32's
00231     // There are 2^19937-1 possible initial states.  This function allows
00232     // all of those to be accessed by providing at least 19937 bits (with a
00233     // default seed length of N = 624 uint32's).  Any bits above the lower 32
00234     // in each element are discarded.
00235     // Just call seed() if you want to get array from /dev/urandom
00236     initialize(19650218UL);
00237     register int i = 1;
00238     register uint32 j = 0;
00239     register uint32 k = N;
00240     if( seedLength > k ) k = seedLength;
00241     for( ; k; --k )
00242     {
00243         state[i] =
00244             state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL );
00245         state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
00246         state[i] &= 0xffffffffUL;
00247         ++i;  ++j;
00248         if( i >= N ) { state[0] = state[N-1];  i = 1; }
00249         if( j >= seedLength ) j = 0;
00250     }
00251     for( k = N - 1; k; --k )
00252     {
00253         state[i] =
00254             state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL );
00255         state[i] -= i;
00256         state[i] &= 0xffffffffUL;
00257         ++i;
00258         if( i >= N ) { state[0] = state[N-1];  i = 1; }
00259     }
00260     state[0] = 0x80000000UL;  // MSB is 1, assuring non-zero initial array
00261     reload();
00262 }

void MTRand::seed ( const uint32  oneSeed  )  [inline]

Definition at line 220 of file MersenneTwister.h.

References initialize(), and reload().

Referenced by RandHelper::initRandGlobal().

00221 {
00222     // Seed the generator with a simple uint32
00223     initialize(oneSeed);
00224     reload();
00225 }

uint32 MTRand::twist ( const uint32 m,
const uint32 s0,
const uint32 s1 
) const [inline, protected]

Definition at line 136 of file MersenneTwister.h.

References loBit(), and mixBits().

Referenced by reload().

00137         { return m ^ (mixBits(s0,s1)>>1) ^ (-loBit(s1) & 0x9908b0dfUL); }


Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const MTRand mtrand 
) [friend]

Definition at line 370 of file MersenneTwister.h.

00371 {
00372     register const MTRand::uint32 *s = mtrand.state;
00373     register int i = mtrand.N;
00374     for( ; i--; os << *s++ << "\t" ) {}
00375     return os << mtrand.left;
00376 }

std::istream& operator>> ( std::istream &  is,
MTRand mtrand 
) [friend]

Definition at line 379 of file MersenneTwister.h.

00380 {
00381     register MTRand::uint32 *s = mtrand.state;
00382     register int i = mtrand.N;
00383     for( ; i--; is >> *s++ ) {}
00384     is >> mtrand.left;
00385     mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left];
00386     return is;
00387 }


Field Documentation

int MTRand::left [protected]

Definition at line 87 of file MersenneTwister.h.

Referenced by load(), operator<<(), operator>>(), randInt(), reload(), and save().

uint32* MTRand::pNext [protected]

Definition at line 86 of file MersenneTwister.h.

Referenced by load(), operator>>(), randInt(), and reload().

uint32 MTRand::state[N] [protected]

Definition at line 85 of file MersenneTwister.h.

Referenced by initialize(), load(), operator<<(), operator>>(), reload(), save(), and seed().


The documentation for this class was generated from the following file:

Generated on Wed May 5 00:06:50 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6