Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

MTRand Class Reference

#include <MersenneTwister.h>

List of all members.

Public Types

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

Public Methods

 MTRand (const uint32 &oneSeed)
 MTRand (uint32 *const bigSeed)
 MTRand ()
double rand ()
double rand (const double &n)
double randExc ()
double randExc (const double &n)
double randDblExc ()
double randDblExc (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 Methods

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 Methods

uint32 hash (time_t t, clock_t c)

Protected Attributes

uint32 state [N]
uint32pNext
int left

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 57 of file MersenneTwister.h.

Referenced by hash(), hiBit(), load(), loBit(), loBits(), mixBits(), operator<<(), operator>>(), randInt(), reload(), save(), seed(), and twist().


Member Enumeration Documentation

anonymous enum
 

Enumeration values:
N 

Definition at line 59 of file MersenneTwister.h.

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

anonymous enum
 

Enumeration values:
SAVE 

Definition at line 60 of file MersenneTwister.h.

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

anonymous enum [protected]
 

Enumeration values:
M 

Definition at line 63 of file MersenneTwister.h.

00063 { M = 397 };              // period parameter

anonymous enum [protected]
 

Enumeration values:
MAGIC 

Definition at line 64 of file MersenneTwister.h.

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


Constructor & Destructor Documentation

MTRand::MTRand const uint32   oneSeed [inline]
 

Definition at line 115 of file MersenneTwister.h.

References seed().

00116         { seed(oneSeed); }

MTRand::MTRand uint32 *const    bigSeed [inline]
 

Definition at line 118 of file MersenneTwister.h.

References seed().

00119         { seed(bigSeed); }

MTRand::MTRand   [inline]
 

Definition at line 121 of file MersenneTwister.h.

References seed().

00122         { seed(); }


Member Function Documentation

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

Definition at line 251 of file MersenneTwister.h.

References uint32.

Referenced by seed().

00252 {
00253         // Get a uint32 from t and c
00254         // Better than uint32(x) in case x is floating point in [0,1]
00255         // Based on code by Lawrence Kirby (fred@genesis.demon.co.uk)
00256 
00257         static uint32 differ = 0;  // guarantee time-based seeds will change
00258 
00259         uint32 h1 = 0;
00260         unsigned char *p = (unsigned char *) &t;
00261         for( size_t i = 0; i < sizeof(t); ++i )
00262         {
00263                 h1 *= UCHAR_MAX + 2U;
00264                 h1 += p[i];
00265         }
00266         uint32 h2 = 0;
00267         p = (unsigned char *) &c;
00268         for( size_t j = 0; j < sizeof(c); ++j )
00269         {
00270                 h2 *= UCHAR_MAX + 2U;
00271                 h2 += p[j];
00272         }
00273         return ( h1 + differ++ ) ^ h2;
00274 }

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

Definition at line 104 of file MersenneTwister.h.

References uint32.

Referenced by mixBits().

00104 { return u & 0x80000000U; }

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

Definition at line 287 of file MersenneTwister.h.

References left, N, pNext, state, and uint32.

00288 {
00289         register uint32 *s = state;
00290         register uint32 *la = loadArray;
00291         register int i = N;
00292         for( ; i--; *s++ = *la++ ) {}
00293         left = *la;
00294         pNext = &state[N-left];
00295 }

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

Definition at line 105 of file MersenneTwister.h.

References uint32.

Referenced by twist().

00105 { return u & 0x00000001U; }

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

Definition at line 106 of file MersenneTwister.h.

References uint32.

Referenced by mixBits().

00106 { return u & 0x7fffffffU; }

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

Definition at line 107 of file MersenneTwister.h.

References hiBit(), loBits(), and uint32.

Referenced by twist().

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

double MTRand::operator()   [inline]
 

Definition at line 89 of file MersenneTwister.h.

References rand().

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

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

Definition at line 127 of file MersenneTwister.h.

References rand().

00128         { return rand() * n; }

double MTRand::rand   [inline]
 

Definition at line 124 of file MersenneTwister.h.

References randInt().

Referenced by operator()(), rand(), and matvec::ranf().

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

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

Definition at line 139 of file MersenneTwister.h.

References randDblExc().

00140         { return randDblExc() * n; }

double MTRand::randDblExc   [inline]
 

Definition at line 136 of file MersenneTwister.h.

References randInt().

Referenced by randDblExc().

00137         { return double( 1.0 + randInt() ) * 2.3283064359965952e-10; }

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

Definition at line 133 of file MersenneTwister.h.

References randExc().

00134         { return randExc() * n; }

double MTRand::randExc   [inline]
 

Definition at line 130 of file MersenneTwister.h.

References randInt().

Referenced by randExc().

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

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

Definition at line 156 of file MersenneTwister.h.

References randInt(), and uint32.

00157 {
00158         // Find which bits are used in n
00159         uint32 used = ~0;
00160         for( uint32 m = n; m; used <<= 1, m >>= 1 ) {}
00161         used = ~used;
00162         
00163         // Draw numbers until one is found in [0,n]
00164         uint32 i;
00165         do
00166                 i = randInt() & used;  // toss unused bits to shorten search
00167         while( i > n );
00168         return i;
00169 }

MTRand::uint32 MTRand::randInt   [inline]
 

Definition at line 142 of file MersenneTwister.h.

References left, pNext, reload(), and uint32.

Referenced by rand(), randDblExc(), randExc(), and randInt().

00143 {
00144         if( left == 0 ) reload();
00145         --left;
00146                 
00147         register uint32 s1;
00148         s1 = *pNext++;
00149         s1 ^= (s1 >> 11);
00150         s1 ^= (s1 <<  7) & 0x9d2c5680U;
00151         s1 ^= (s1 << 15) & 0xefc60000U;
00152         return ( s1 ^ (s1 >> 18) );
00153 }

void MTRand::reload   [inline, protected]
 

Definition at line 235 of file MersenneTwister.h.

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

Referenced by randInt(), and seed().

00236 {
00237         // Generate N new values in state
00238         // Made clearer and faster by Matthew Bellew (matthew.bellew@home.com)
00239         register uint32 *p = state;
00240         register int i;
00241         for( i = N - M; i--; ++p )
00242                 *p = twist( p[M], p[0], p[1] );
00243         for( i = M; --i; ++p )
00244                 *p = twist( p[M-N], p[0], p[1] );
00245         *p = twist( p[M-N], p[0], state[0] );
00246 
00247         left = N, pNext = state;
00248 }

void MTRand::save uint32   saveArray const [inline]
 

Definition at line 277 of file MersenneTwister.h.

References left, N, state, and uint32.

00278 {
00279         register uint32 *sa = saveArray;
00280         register const uint32 *s = state;
00281         register int i = N;
00282         for( ; i--; *sa++ = *s++ ) {}
00283         *sa = left;
00284 }

void MTRand::seed   [inline]
 

Definition at line 202 of file MersenneTwister.h.

References hash(), N, reload(), state, and uint32.

Referenced by MTRand().

00203 {
00204         // Seed the generator with an array from /dev/urandom if available
00205         // Otherwise use a hash of time() and clock() values
00206         
00207         // First try getting an array from /dev/urandom
00208         FILE* urandom = fopen( "/dev/urandom", "rb" );
00209         if( urandom )
00210         {
00211                 register uint32 *s = state;
00212                 register int i = N;
00213                 register bool success = true;
00214                 while( success && i-- )
00215                 {
00216                         success = fread( s, sizeof(uint32), 1, urandom );
00217                         *s++ &= 0xffffffff;  // filter in case uint32 > 32 bits
00218                 }
00219                 fclose(urandom);
00220                 if( success )
00221                 {
00222                         // There is a 1 in 2^19937 chance that a working urandom gave
00223                         // 19937 consecutive zeroes and will make the generator fail
00224                         // Ignore that case and continue with initialization
00225                         reload();
00226                         return;
00227                 }
00228         }
00229         
00230         // Was not successful, so use time() and clock() instead
00231         seed( hash( time(NULL), clock() ) );
00232 }

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

Definition at line 186 of file MersenneTwister.h.

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

00187 {
00188         // Seed the generator with an array of 624 uint32's
00189         // There are 2^19937-1 possible initial states.  This function allows
00190         // any one of those to be chosen by providing 19937 bits.  The lower
00191         // 31 bits of the first element, bigSeed[0], are discarded.  Any bits
00192         // above the lower 32 in each element are also discarded.  Theoretically,
00193         // the rest of the array can contain any values except all zeroes.
00194         // Just call seed() if you want to get array from /dev/urandom
00195         register uint32 *s = state, *b = bigSeed;
00196         register int i = N;
00197         for( ; i--; *s++ = *b++ & 0xffffffff ) {}
00198         reload();
00199 }

void MTRand::seed uint32    oneSeed [inline]
 

Definition at line 172 of file MersenneTwister.h.

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

00173 {
00174         // Seed the generator with a simple uint32
00175         register uint32 *s;
00176         register int i;
00177         for( i = N, s = state;
00178              i--;
00179                  *s    = oneSeed & 0xffff0000,
00180                  *s++ |= ( (oneSeed *= 69069U)++ & 0xffff0000 ) >> 16,
00181                  (oneSeed *= 69069U)++ ) {}  // hard to read, but fast
00182         reload();
00183 }

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

Definition at line 109 of file MersenneTwister.h.

References loBit(), MAGIC, mixBits(), and uint32.

Referenced by reload().

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


Friends And Related Function Documentation

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

Definition at line 298 of file MersenneTwister.h.

00299 {
00300         register const MTRand::uint32 *s = mtrand.state;
00301         register int i = mtrand.N;
00302         for( ; i--; os << *s++ << "\t" ) {}
00303         return os << mtrand.left;
00304 }

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

Definition at line 307 of file MersenneTwister.h.

00308 {
00309         register MTRand::uint32 *s = mtrand.state;
00310         register int i = mtrand.N;
00311         for( ; i--; is >> *s++ ) {}
00312         is >> mtrand.left;
00313         mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left];
00314         return is;
00315 }


Member Data Documentation

int MTRand::left [protected]
 

Definition at line 68 of file MersenneTwister.h.

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

uint32* MTRand::pNext [protected]
 

Definition at line 67 of file MersenneTwister.h.

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

uint32 MTRand::state[N] [protected]
 

Definition at line 66 of file MersenneTwister.h.

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


The documentation for this class was generated from the following file:
Generated on Thu Jun 16 17:14:12 2005 for Matvec by doxygen1.2.16