MTRand Class Reference

#include <MersenneTwister.h>

List of all members.

Public Types

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

Public Member Functions

 MTRand (const uint32 &oneSeed)
 MTRand (uint32 *const bigSeed)
 MTRand ()
double rand ()
double rand (const double &n)
double randExc ()
double randExc (const double &n)
uint32 randInt ()
uint32 randInt (const uint32 &n)
double operator() ()
void seed (uint32 oneSeed)
void seed (uint32 *const bigSeed)
void seed ()
void save (uint32 *saveArray) const
void load (uint32 *const loadArray)

Protected Types

enum  { M = 397 }
enum  { MAGIC = 0x9908b0dfU }

Protected Member Functions

void reload ()
uint32 hiBit (const uint32 &u) const
uint32 loBit (const uint32 &u) const
uint32 loBits (const uint32 &u) const
uint32 mixBits (const uint32 &u, const uint32 &v) const
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

uint32 state [N]
uint32pNext
int left

Friends

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


Detailed Description

Definition at line 53 of file MersenneTwister.h.


Member Typedef Documentation

typedef unsigned long MTRand::uint32

Definition at line 56 of file MersenneTwister.h.


Member Enumeration Documentation

anonymous enum

Enumerator:
N 

Definition at line 58 of file MersenneTwister.h.

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

anonymous enum

Enumerator:
SAVE 

Definition at line 59 of file MersenneTwister.h.

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

anonymous enum [protected]

Enumerator:
M 

Definition at line 62 of file MersenneTwister.h.

00062 { M = 397 };              // period parameter

anonymous enum [protected]

Enumerator:
MAGIC 

Definition at line 63 of file MersenneTwister.h.

00063 { MAGIC = 0x9908b0dfU };  // magic constant


Constructor & Destructor Documentation

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

Definition at line 112 of file MersenneTwister.h.

00113         { seed(oneSeed); }

MTRand::MTRand ( uint32 *const   bigSeed  )  [inline]

Definition at line 115 of file MersenneTwister.h.

00116         { seed(bigSeed); }

MTRand::MTRand (  )  [inline]

Definition at line 118 of file MersenneTwister.h.

00119         { seed(); }


Member Function Documentation

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

Definition at line 224 of file MersenneTwister.h.

00225 {
00226         // Get a uint32 from t and c
00227         // Better than uint32(x) in case x is floating point in [0,1]
00228         // Based on code by Lawrence Kirby (fred@genesis.demon.co.uk)
00229 
00230         static uint32 differ = 0;  // guarantee time-based seeds will change
00231 
00232         uint32 h1 = 0;
00233         unsigned char *p = (unsigned char *) &t;
00234         for( size_t i = 0; i < sizeof(t); ++i )
00235         {
00236                 h1 *= UCHAR_MAX + 2U;
00237                 h1 += p[i];
00238         }
00239         uint32 h2 = 0;
00240         p = (unsigned char *) &c;
00241         for( size_t j = 0; j < sizeof(c); ++j )
00242         {
00243                 h2 *= UCHAR_MAX + 2U;
00244                 h2 += p[j];
00245         }
00246         return ( h1 + differ++ ) ^ h2;
00247 }

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

Definition at line 101 of file MersenneTwister.h.

00101 { return u & 0x80000000U; }

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

Definition at line 260 of file MersenneTwister.h.

00261 {
00262         register uint32 *s = state;
00263         register uint32 *la = loadArray;
00264         register int i = N;
00265         for( ; i--; *s++ = *la++ ) {}
00266         left = *la;
00267         pNext = &state[N-left];
00268 }

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

Definition at line 102 of file MersenneTwister.h.

00102 { return u & 0x00000001U; }

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

Definition at line 103 of file MersenneTwister.h.

00103 { return u & 0x7fffffffU; }

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

Definition at line 104 of file MersenneTwister.h.

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

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

Definition at line 86 of file MersenneTwister.h.

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

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

Definition at line 124 of file MersenneTwister.h.

00125         { return rand() * n; }

double MTRand::rand (  )  [inline]

Definition at line 121 of file MersenneTwister.h.

00122     { return double(randInt()) * 2.3283064370807974e-10; }

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

Definition at line 130 of file MersenneTwister.h.

00131         { return randExc() * n; }

double MTRand::randExc (  )  [inline]

Definition at line 127 of file MersenneTwister.h.

00128         { return double(randInt()) * 2.3283064365386963e-10; }

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

Definition at line 147 of file MersenneTwister.h.

00148         { return int( randExc() * (n+1) ); }

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

Definition at line 133 of file MersenneTwister.h.

00134 {
00135         if( left == 0 ) reload();
00136         --left;
00137                 
00138         register uint32 s1;
00139         s1 = *pNext++;
00140         s1 ^= (s1 >> 11);
00141         s1 ^= (s1 <<  7) & 0x9d2c5680U;
00142         s1 ^= (s1 << 15) & 0xefc60000U;
00143         return ( s1 ^ (s1 >> 18) );
00144 }

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

Definition at line 208 of file MersenneTwister.h.

00209 {
00210         // Generate N new values in state
00211         // Made clearer and faster by Matthew Bellew (matthew.bellew@home.com)
00212         register uint32 *p = state;
00213         register int i;
00214         for( i = N - M; i--; )
00215                 *p++ = twist( p[M], p[0], p[1] );
00216         for( i = M; --i; )
00217                 *p++ = twist( p[M-N], p[0], p[1] );
00218         *p = twist( p[M-N], p[0], state[0] );
00219 
00220         left = N, pNext = state;
00221 }

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

Definition at line 250 of file MersenneTwister.h.

00251 {
00252         register uint32 *sa = saveArray;
00253         register const uint32 *s = state;
00254         register int i = N;
00255         for( ; i--; *sa++ = *s++ ) {}
00256         *sa = left;
00257 }

void MTRand::seed (  )  [inline]

Definition at line 180 of file MersenneTwister.h.

00181 {
00182     /* commented G.K.
00183         // Seed the generator with an array from /dev/urandom if available
00184         // Otherwise use a hash of time() and clock() values
00185         
00186         // First try getting an array from /dev/urandom
00187         FILE* urandom = fopen( "/dev/urandom", "rb" );
00188         if( urandom )
00189         {
00190                 register uint32 *s = state;
00191                 register int i = N;
00192                 register bool success = true;
00193                 for( ; success && i--;
00194                      success = fread( s++, sizeof(uint32), 1, urandom ) ) {}
00195                 fclose(urandom);
00196                 if( success )
00197                 {
00198                         reload();
00199                         return;
00200                 }
00201         }
00202         */
00203         // Was not successful, so use time() and clock() instead
00204         seed( hash( time((long*)NULL), clock() ) );
00205 }

void MTRand::seed ( uint32 *const   bigSeed  )  [inline]

Definition at line 166 of file MersenneTwister.h.

00167 {
00168         // Seed the generator with an array of 624 uint32's
00169         // There are 2^19937-1 possible initial states.  This function
00170         // allows any one of those to be chosen by providing 19937 bits.
00171         // Theoretically, the array can contain any values except all zeroes.
00172         // Just call seed() if you want to get array from /dev/urandom
00173         register uint32 *s = state, *b = bigSeed;
00174         register int i = N;
00175         for( ; i--; *s++ = *b++ ) {}
00176         reload();
00177 }

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

Definition at line 152 of file MersenneTwister.h.

00153 {
00154         // Seed the generator with a simple uint32
00155         register uint32 *s;
00156         register int i;
00157         for( i = N, s = state;
00158              i--;
00159                  *s    = oneSeed & 0xffff0000,
00160                  *s++ |= ( (oneSeed *= 69069U)++ & 0xffff0000 ) >> 16,
00161                  (oneSeed *= 69069U)++ ) {}  // hard to read, but fast
00162         reload();
00163 }

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

Definition at line 106 of file MersenneTwister.h.

00107                 { return m ^ (mixBits(s0,s1)>>1) ^ (loBit(s1) ? MAGIC : 0U); }


Friends And Related Function Documentation

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

Definition at line 271 of file MersenneTwister.h.

00272 {
00273         register const MTRand::uint32 *s = mtrand.state;
00274         register int i = mtrand.N;
00275         for( ; i--; os << *s++ << "\t" ) {}
00276         return os << mtrand.left;
00277 }

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

Definition at line 280 of file MersenneTwister.h.

00281 {
00282         register MTRand::uint32 *s = mtrand.state;
00283         register int i = mtrand.N;
00284         for( ; i--; is >> *s++ ) {}
00285         is >> mtrand.left;
00286         mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left];
00287         return is;
00288 }


Member Data Documentation

int MTRand::left [protected]

Definition at line 67 of file MersenneTwister.h.

uint32* MTRand::pNext [protected]

Definition at line 66 of file MersenneTwister.h.

uint32 MTRand::state[N] [protected]

Definition at line 65 of file MersenneTwister.h.


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

Generated on Thu Nov 28 14:47:24 2013 for red_wireless by  doxygen 1.5.7.1