Soar Kernel  9.3.2 08-06-12
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Types | Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Friends
MTRand Class Reference

#include <soar_rand.h>

Public Types

enum  
enum  

Public Member Functions

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

Protected Types

enum  

Protected Member Functions

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

Static Protected Member Functions

static uint32_t hash (time_t t, clock_t c)

Protected Attributes

int left
uint32_tpNext
uint32_t state [N]

Friends

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

Detailed Description

Definition at line 91 of file soar_rand.h.

Member Enumeration Documentation

anonymous enum
Enumerator:
N 

Definition at line 94 of file soar_rand.h.

{ N = 624 }; // length of state vector
anonymous enum
Enumerator:
SAVE 

Definition at line 95 of file soar_rand.h.

{ SAVE = N + 1 }; // length of array for save()
anonymous enum
protected
Enumerator:
M 

Definition at line 98 of file soar_rand.h.

{ M = 397 }; // period parameter

Constructor & Destructor Documentation

MTRand::MTRand ( const uint32_t oneSeed)
inline

Definition at line 165 of file soar_rand.h.

References seed().

{ seed(oneSeed); }
MTRand::MTRand ( uint32_t *const  bigSeed,
uint32_t const  seedLength = N 
)
inline

Definition at line 168 of file soar_rand.h.

References seed().

{ seed(bigSeed,seedLength); }
MTRand::MTRand ( )
inline

Definition at line 171 of file soar_rand.h.

References seed().

{ seed(); }

Member Function Documentation

uint32_t MTRand::hash ( time_t  t,
clock_t  c 
)
inlinestaticprotected

Definition at line 346 of file soar_rand.h.

References uint32_t().

Referenced by seed().

{
// 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@genesis.demon.co.uk)
static uint32_t differ = 0; // guarantee time-based seeds will change
uint32_t h1 = 0;
unsigned char *p = (unsigned char *) &t;
for( size_t i = 0; i < sizeof(t); ++i )
{
h1 *= UCHAR_MAX + 2U;
h1 += p[i];
}
uint32_t h2 = 0;
p = (unsigned char *) &c;
for( size_t j = 0; j < sizeof(c); ++j )
{
h2 *= UCHAR_MAX + 2U;
h2 += p[j];
}
return ( h1 + differ++ ) ^ h2;
}
uint32_t MTRand::hiBit ( const uint32_t u) const
inlineprotected

Definition at line 146 of file soar_rand.h.

Referenced by mixBits().

{ return u & 0x80000000U; }
void MTRand::initialize ( const uint32_t  oneSeed)
inlineprotected

Definition at line 312 of file soar_rand.h.

References N, state, and uint32_t().

Referenced by seed().

{
// 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.
register uint32_t* s = state;
register uint32_t* r = state;
register int i = 1;
*s++ = seed & 0xffffffffU;
for( ; i < N; ++i )
{
*s++ = ( 1812433253U * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffU;
r++;
}
}
void MTRand::load ( uint32_t *const  loadArray)
inline

Definition at line 382 of file soar_rand.h.

References left, N, pNext, state, and uint32_t().

{
register uint32_t* s = state;
register uint32_t* la = loadArray;
register int i = N;
for( ; i--; *s++ = *la++ ) {}
left = *la;
}
uint32_t MTRand::loBit ( const uint32_t u) const
inlineprotected

Definition at line 147 of file soar_rand.h.

Referenced by twist().

{ return u & 0x00000001U; }
uint32_t MTRand::loBits ( const uint32_t u) const
inlineprotected

Definition at line 148 of file soar_rand.h.

Referenced by mixBits().

{ return u & 0x7fffffffU; }
uint32_t MTRand::mixBits ( const uint32_t u,
const uint32_t v 
) const
inlineprotected

Definition at line 149 of file soar_rand.h.

References hiBit(), and loBits().

Referenced by twist().

{ return hiBit(u) | loBits(v); }
double MTRand::operator() ( )
inline

Definition at line 124 of file soar_rand.h.

References rand().

{ return rand(); } // same as rand()
double MTRand::rand ( )
inline

Definition at line 174 of file soar_rand.h.

References randInt().

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

{ return double(randInt()) * (1.0/4294967295.0); }
double MTRand::rand ( const double &  n)
inline

Definition at line 177 of file soar_rand.h.

References rand().

{ return rand() * n; }
double MTRand::rand53 ( )
inline

Definition at line 192 of file soar_rand.h.

References randInt(), and uint32_t().

{
uint32_t a = randInt() >> 5, b = randInt() >> 6;
return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0); // by Isaku Wada
}
double MTRand::randDblExc ( )
inline

Definition at line 186 of file soar_rand.h.

References randInt().

Referenced by randDblExc(), and randNorm().

{ return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); }
double MTRand::randDblExc ( const double &  n)
inline

Definition at line 189 of file soar_rand.h.

References randDblExc().

{ return randDblExc() * n; }
double MTRand::randExc ( )
inline

Definition at line 180 of file soar_rand.h.

References randInt().

Referenced by randExc(), and randNorm().

{ return double(randInt()) * (1.0/4294967296.0); }
double MTRand::randExc ( const double &  n)
inline

Definition at line 183 of file soar_rand.h.

References randExc().

{ return randExc() * n; }
uint32_t MTRand::randInt ( )
inline

Definition at line 207 of file soar_rand.h.

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

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

{
// Pull a 32-bit integer from the generator state
// Every other access function simply transforms the numbers extracted here
if( left == 0 ) reload();
--left;
register uint32_t s1;
s1 = *pNext++;
s1 ^= (s1 >> 11);
s1 ^= (s1 << 7) & 0x9d2c5680U;
s1 ^= (s1 << 15) & 0xefc60000U;
return ( s1 ^ (s1 >> 18) );
}
uint32_t MTRand::randInt ( const uint32_t n)
inline

Definition at line 223 of file soar_rand.h.

References randInt(), and uint32_t().

{
// Find which bits are used in n
// Optimized by Magnus Jonsson (magnus@smartelectronix.com)
uint32_t used = n;
used |= used >> 1;
used |= used >> 2;
used |= used >> 4;
used |= used >> 8;
used |= used >> 16;
// Draw numbers until one is found in [0,n]
do
i = randInt() & used; // toss unused bits to shorten search
while( i > n );
return i;
}
double MTRand::randNorm ( const double &  mean = 0.0,
const double &  stddeviation = 0.0 
)
inline

Definition at line 198 of file soar_rand.h.

References randDblExc(), and randExc().

{
// Return a real number from a normal (Gaussian) distribution with given
// mean and variance by Box-Muller method
double r = sqrt( -2.0 * log( 1.0-randDblExc()) ) * stddeviation;
double phi = 2.0 * 3.14159265358979323846264338328 * randExc();
return mean + r * cos(phi);
}
void MTRand::reload ( )
inlineprotected

Definition at line 330 of file soar_rand.h.

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

Referenced by randInt(), and seed().

{
// Generate N new values in state
// Made clearer and faster by Matthew Bellew (matthew.bellew@home.com)
register uint32_t *p = state;
register int i;
for( i = N - M; i--; ++p )
*p = twist( p[M], p[0], p[1] );
for( i = M; --i; ++p )
*p = twist( p[M-N], p[0], p[1] );
*p = twist( p[M-N], p[0], state[0] );
left = N, pNext = state;
}
void MTRand::save ( uint32_t saveArray) const
inline

Definition at line 372 of file soar_rand.h.

References left, N, state, and uint32_t().

{
register uint32_t* sa = saveArray;
register const uint32_t* s = state;
register int i = N;
for( ; i--; *sa++ = *s++ ) {}
*sa = left;
}
void MTRand::seed ( const uint32_t  oneSeed)
inline

Definition at line 243 of file soar_rand.h.

References initialize(), and reload().

Referenced by SoarSeedRNG().

{
// Seed the generator with a simple uint32
initialize(oneSeed);
reload();
}
void MTRand::seed ( uint32_t *const  bigSeed,
const uint32_t  seedLength = N 
)
inline

Definition at line 251 of file soar_rand.h.

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

{
// 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
initialize(19650218U);
register int i = 1;
register uint32_t j = 0;
register int k = ( N > seedLength ? N : seedLength );
for( ; k; --k )
{
state[i] =
state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525U );
state[i] += ( bigSeed[j] & 0xffffffffU ) + j;
state[i] &= 0xffffffffU;
++i; ++j;
if( i >= N ) { state[0] = state[N-1]; i = 1; }
if( j >= seedLength ) j = 0;
}
for( k = N - 1; k; --k )
{
state[i] =
state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941U );
state[i] -= i;
state[i] &= 0xffffffffU;
++i;
if( i >= N ) { state[0] = state[N-1]; i = 1; }
}
state[0] = 0x80000000U; // MSB is 1, assuring non-zero initial array
reload();
}
void MTRand::seed ( )
inline

Definition at line 287 of file soar_rand.h.

References hash(), N, and uint32_t().

Referenced by MTRand().

{
// Seed the generator with an array from /dev/urandom if available
// Otherwise use a hash of time() and clock() values
// First try getting an array from /dev/urandom
FILE* urandom = fopen( "/dev/urandom", "rb" );
if( urandom )
{
uint32_t bigSeed[N];
register uint32_t* s = bigSeed;
register int i = N;
register bool success = true;
while( success && i-- )
//success = fread( s++, sizeof(uint32_t), 1, urandom );
success = (fread( s++, sizeof(uint32_t), 1, urandom ) == 0); // RPM 1/06 modified to eliminate Visual Studio warning C4800
fclose(urandom);
if( success ) { seed( bigSeed, N ); return; }
}
// Was not successful, so use time() and clock() instead
seed( hash( time(NULL), clock() ) );
}
uint32_t MTRand::twist ( const uint32_t m,
const uint32_t s0,
const uint32_t s1 
) const
inlineprotected

Definition at line 155 of file soar_rand.h.

References loBit(), and mixBits().

Referenced by reload().

{ return m ^ (mixBits(s0,s1)>>1) ^ (-loBit(s1) & 0x9908b0dfU); } // RPM 1/06 this line causes Visual Studio warning C4146, but is actually safe

Friends And Related Function Documentation

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

Definition at line 393 of file soar_rand.h.

{
register const uint32_t* s = mtrand.state;
register int i = mtrand.N;
for( ; i--; os << *s++ << "\t" ) {}
return os << mtrand.left;
}
std::istream& operator>> ( std::istream &  is,
MTRand mtrand 
)
friend

Definition at line 402 of file soar_rand.h.

{
register uint32_t* s = mtrand.state;
register int i = mtrand.N;
for( ; i--; is >> *s++ ) {}
is >> mtrand.left;
mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left];
return is;
}

Field Documentation

int MTRand::left
protected

Definition at line 102 of file soar_rand.h.

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

uint32_t* MTRand::pNext
protected

Definition at line 101 of file soar_rand.h.

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

uint32_t MTRand::state[N]
protected

Definition at line 100 of file soar_rand.h.

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


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