MTRand Class Reference

#include <mtrand.h>

Inheritance diagram for MTRand:

CCObject SimpleCCObject List of all members.

Public Member Functions

 MTRand (const UINT32 &oneSeed)
 MTRand ()
UINT32 rand ()
UINT32 operator() ()
void seed (const UINT32 oneSeed)
UINT32 GetNextRandomNumberScaled (UINT32 MaxValue, UINT32 MinValue)
 as above, note that you MUST have already seeded the sequence
UINT32 GetNextRandomNumberScaled (UINT32 MaxValue, UINT32 MinValue, UINT32 Median)
 Overridden version which allows you to have a non-linear range. By specifying the median all random values > MTRAND / 2 will be scaled from Median - MaxValue and all random values <= MTRAND / 2 will be scaled from MinValue - Median.

Protected Types

enum  { N = 624 }
enum  { M = 397 }

Protected Member Functions

void initialize (const UINT32 oneSeed)
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

Protected Attributes

UINT32 state [N]
UINT32pNext
INT32 left

Static Protected Attributes

static UINT32 lastSeed = 1

Detailed Description

Definition at line 87 of file mtrand.h.


Member Enumeration Documentation

anonymous enum [protected]
 

Enumerator:
N 

Definition at line 91 of file mtrand.h.

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

anonymous enum [protected]
 

Enumerator:
M 

Definition at line 92 of file mtrand.h.

00092 { M = 397 };  // period parameter


Constructor & Destructor Documentation

MTRand::MTRand const UINT32 oneSeed  )  [inline]
 

Definition at line 133 of file mtrand.h.

00134     { seed(lastSeed = oneSeed); }

MTRand::MTRand  )  [inline]
 

Definition at line 136 of file mtrand.h.

00137     { seed(lastSeed += 2); }


Member Function Documentation

UINT32 MTRand::GetNextRandomNumberScaled UINT32  MaxValue,
UINT32  MinValue,
UINT32  Median
 

Overridden version which allows you to have a non-linear range. By specifying the median all random values > MTRAND / 2 will be scaled from Median - MaxValue and all random values <= MTRAND / 2 will be scaled from MinValue - Median.

Author:
Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com>
Date:
04/11/2005
Parameters:
MaxValue - the top of our random range [INPUTS] MinValue - the bottom of our random range Median - duh
Returns:
the next number in an already seeded random number sequence, scaled to between the two specified values
See also:

Definition at line 51 of file mtrand.cpp.

00052 {
00053     UINT32 Random = rand();
00054 
00055     UINT32 Retval = 0;
00056     if (Random < MTRAND_MED)
00057     {
00058         double ScaleFactor = ((double)(Median - MinValue)) / (double)MTRAND_MED;
00059         double Res = ScaleFactor * Random;
00060         Retval = (UINT32)Res + MinValue;
00061     }
00062     if (Random >= MTRAND_MED)
00063     {
00064         double ScaleFactor = ((double)(MaxValue - Median)) / (double)MTRAND_MED;
00065         double Res = ScaleFactor * (Random - MTRAND_MED);
00066         Retval = (UINT32)Res + Median;
00067     }
00068     return Retval;
00069 }

UINT32 MTRand::GetNextRandomNumberScaled UINT32  MaxValue,
UINT32  MinValue
 

as above, note that you MUST have already seeded the sequence

Author:
Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com>
Date:
04/11/2005
Parameters:
- [INPUTS]
Returns:
the next number in an already seeded random number sequence, scaled to between the two specified values
See also:

Definition at line 24 of file mtrand.cpp.

00025 {
00026     UINT32 Random =  this->operator ()();
00027     double ScaleFactor = ((double)(MaxValue - MinValue)) / ((double)MTRAND_MAX);
00028     double Res = (double)Random * ScaleFactor;
00029     return (UINT32)Res + MinValue;
00030 }

UINT32 MTRand::hiBit const UINT32 u  )  const [inline, protected]
 

Definition at line 123 of file mtrand.h.

00123 { return u & 0x80000000UL; }

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

Definition at line 165 of file mtrand.h.

00166 {
00167     // Initialize generator state with seed
00168     // See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier.
00169     // In previous versions, most significant bits (MSBs) of the seed affect
00170     // only MSBs of the state array.  Modified 9 Jan 2002 by Makoto Matsumoto.
00171 //  register UINT32 *s = state;
00172 //  register UINT32 *r = state;
00173 //  register INT32 i = 1;
00174 //  *s++ = seed & 0xffffffffUL;
00175 //  for( ; i < N; ++i )
00176 //  {
00177 //      *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
00178 //      r++;
00179 //  }
00180 
00181     // This is the original seeding code to ensure that the number sequences are the same
00182     register UINT32 x = (seed | 1U) & 0xFFFFFFFFU, *s = state;
00183     register INT32    j;
00184 
00185     for(left=0, *s++=x, j=N; --j;
00186         *s++ = (x*=69069U) & 0xFFFFFFFFU);
00187 }

UINT32 MTRand::loBit const UINT32 u  )  const [inline, protected]
 

Definition at line 124 of file mtrand.h.

00124 { return u & 0x00000001UL; }

UINT32 MTRand::loBits const UINT32 u  )  const [inline, protected]
 

Definition at line 125 of file mtrand.h.

00125 { return u & 0x7fffffffUL; }

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

Definition at line 126 of file mtrand.h.

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

UINT32 MTRand::operator()  )  [inline]
 

Definition at line 110 of file mtrand.h.

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

UINT32 MTRand::rand  )  [inline]
 

Definition at line 140 of file mtrand.h.

00141 {
00142     // Pull a 32-bit integer from the generator state
00143     // Every other access function simply transforms the numbers extracted here
00144     
00145     if( left == 0 ) reload();
00146     --left;
00147         
00148     register UINT32 s1;
00149     s1 = *pNext++;
00150     s1 ^= (s1 >> 11);
00151     s1 ^= (s1 <<  7) & 0x9d2c5680UL;
00152     s1 ^= (s1 << 15) & 0xefc60000UL;
00153     return ( s1 ^ (s1 >> 18) );
00154 }

void MTRand::reload  )  [inline, protected]
 

Definition at line 190 of file mtrand.h.

00191 {
00192     // Generate N new values in state
00193     // Made clearer and faster by Matthew Bellew (matthew.bellew@home.com)
00194     register UINT32 *p = state;
00195     register INT32 i;
00196     for( i = N - M; i--; ++p )
00197         *p = twist( p[M], p[0], p[1] );
00198     for( i = M; --i; ++p )
00199         *p = twist( p[M-N], p[0], p[1] );
00200     *p = twist( p[M-N], p[0], state[0] );
00201 
00202     left = N, pNext = state;
00203 }

void MTRand::seed const UINT32  oneSeed  )  [inline]
 

Definition at line 157 of file mtrand.h.

00158 {
00159     // Seed the generator with a simple UINT32
00160     initialize(oneSeed);
00161     reload();
00162 }

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

Definition at line 128 of file mtrand.h.

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


Member Data Documentation

UINT32 MTRand::lastSeed = 1 [static, protected]
 

Definition at line 98 of file mtrand.h.

INT32 MTRand::left [protected]
 

Definition at line 96 of file mtrand.h.

UINT32* MTRand::pNext [protected]
 

Definition at line 95 of file mtrand.h.

UINT32 MTRand::state[N] [protected]
 

Definition at line 94 of file mtrand.h.


The documentation for this class was generated from the following files:
Generated on Sat Nov 10 03:56:21 2007 for Camelot by  doxygen 1.4.4