#include <mtrand.h>
Inheritance diagram for MTRand:
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] |
UINT32 * | pNext |
INT32 | left |
Static Protected Attributes | |
static UINT32 | lastSeed = 1 |
Definition at line 87 of file mtrand.h.
|
Definition at line 91 of file mtrand.h. 00091 { N = 624 }; // length of state vector
|
|
Definition at line 92 of file mtrand.h. 00092 { M = 397 }; // period parameter
|
|
Definition at line 133 of file mtrand.h.
|
|
Definition at line 136 of file mtrand.h.
|
|
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.
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 }
|
|
as above, note that you MUST have already seeded the sequence
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 }
|
|
Definition at line 123 of file mtrand.h.
|
|
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 }
|
|
Definition at line 124 of file mtrand.h.
|
|
Definition at line 125 of file mtrand.h.
|
|
Definition at line 126 of file mtrand.h.
|
|
Definition at line 110 of file mtrand.h. 00110 { return rand(); } // same as rand()
|
|
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 }
|
|
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 }
|
|
Definition at line 157 of file mtrand.h. 00158 { 00159 // Seed the generator with a simple UINT32 00160 initialize(oneSeed); 00161 reload(); 00162 }
|
|
Definition at line 128 of file mtrand.h.
|
|
|
|
|
|
|
|
|