66 |
|
#include <stdio.h> |
67 |
|
#include <time.h> |
68 |
|
#include <math.h> |
69 |
+ |
#include <vector> |
70 |
+ |
namespace oopse { |
71 |
|
|
72 |
< |
class MTRand { |
73 |
< |
// Data |
74 |
< |
public: |
75 |
< |
typedef unsigned long uint32; // unsigned integer type, at least 32 bits |
72 |
> |
class MTRand { |
73 |
> |
// Data |
74 |
> |
public: |
75 |
> |
typedef unsigned long uint32; // unsigned integer type, at least 32 bits |
76 |
|
|
77 |
< |
enum { N = 624 }; // length of state vector |
78 |
< |
enum { SAVE = N + 1 }; // length of array for save() |
77 |
> |
enum { N = 624 }; // length of state vector |
78 |
> |
enum { SAVE = N + 1 }; // length of array for save() |
79 |
|
|
80 |
< |
private: |
81 |
< |
enum { M = 397 }; // period parameter |
80 |
> |
private: |
81 |
> |
enum { M = 397 }; // period parameter |
82 |
|
|
83 |
< |
uint32 state[N]; // internal state |
84 |
< |
uint32 *pNext; // next value to get from state |
85 |
< |
int left; // number of values left before reload needed |
86 |
< |
int nstrides_; |
87 |
< |
int stride_; |
83 |
> |
uint32 state[N]; // internal state |
84 |
> |
uint32 *pNext; // next value to get from state |
85 |
> |
int left; // number of values left before reload needed |
86 |
> |
int nstrides_; |
87 |
> |
int stride_; |
88 |
|
|
89 |
< |
//Methods |
90 |
< |
public: |
91 |
< |
MTRand( const uint32& oneSeed, int nstrides = 1, int stride = 0); // initialize with a simple uint32 |
92 |
< |
MTRand( uint32 *const bigSeed, uint32 const seedLength = N, int nstrides = 1, int stride = 0); // or an array |
93 |
< |
MTRand(int nstrides = 1, int stride = 0); // auto-initialize with /dev/urandom or time() and clock() |
89 |
> |
//Methods |
90 |
> |
public: |
91 |
> |
MTRand( const uint32& oneSeed, int nstrides, int stride); // initialize with a simple uint32 |
92 |
> |
MTRand( uint32 *const bigSeed, uint32 const seedLength, int nstrides, int stride); // or an array |
93 |
> |
MTRand(int nstrides, int stride); // auto-initialize with /dev/urandom or time() and clock() |
94 |
|
|
95 |
< |
// Do NOT use for CRYPTOGRAPHY without securely hashing several returned |
96 |
< |
// values together, otherwise the generator state can be learned after |
97 |
< |
// reading 624 consecutive values. |
95 |
> |
// Do NOT use for CRYPTOGRAPHY without securely hashing several returned |
96 |
> |
// values together, otherwise the generator state can be learned after |
97 |
> |
// reading 624 consecutive values. |
98 |
|
|
99 |
< |
// Access to 32-bit random numbers |
100 |
< |
double rand(); // real number in [0,1] |
101 |
< |
double rand( const double& n ); // real number in [0,n] |
102 |
< |
double randExc(); // real number in [0,1) |
103 |
< |
double randExc( const double& n ); // real number in [0,n) |
104 |
< |
double randDblExc(); // real number in (0,1) |
105 |
< |
double randDblExc( const double& n ); // real number in (0,n) |
106 |
< |
uint32 randInt(); // integer in [0,2^32-1] (modified for striding) |
107 |
< |
uint32 rawRandInt(); // original randInt |
108 |
< |
uint32 randInt( const uint32& n ); // integer in [0,n] for n < 2^32 |
109 |
< |
double operator()() { return rand(); } // same as rand() |
99 |
> |
// Access to 32-bit random numbers |
100 |
> |
RealType rand(); // real number in [0,1] |
101 |
> |
RealType rand( const RealType& n ); // real number in [0,n] |
102 |
> |
RealType randExc(); // real number in [0,1) |
103 |
> |
RealType randExc( const RealType& n ); // real number in [0,n) |
104 |
> |
RealType randDblExc(); // real number in (0,1) |
105 |
> |
RealType randDblExc( const RealType& n ); // real number in (0,n) |
106 |
> |
uint32 randInt(); // integer in [0,2^32-1] (modified for striding) |
107 |
> |
uint32 rawRandInt(); // original randInt |
108 |
> |
uint32 randInt( const uint32& n ); // integer in [0,n] for n < 2^32 |
109 |
> |
RealType operator()() { return rand(); } // same as rand() |
110 |
|
|
111 |
< |
// Access to 53-bit random numbers (capacity of IEEE double precision) |
112 |
< |
double rand53(); // real number in [0,1) |
111 |
> |
// Access to 53-bit random numbers (capacity of IEEE RealType precision) |
112 |
> |
RealType rand53(); // real number in [0,1) |
113 |
|
|
114 |
< |
// Access to nonuniform random number distributions |
115 |
< |
double randNorm( const double& mean = 0.0, const double& variance = 0.0 ); |
114 |
> |
// Access to nonuniform random number distributions |
115 |
> |
RealType randNorm( const RealType mean = 0.0, const RealType variance = 0.0 ); |
116 |
|
|
117 |
< |
// Re-seeding functions with same behavior as initializers |
118 |
< |
void seed( const uint32 oneSeed ); |
119 |
< |
void seed( uint32 *const bigSeed, const uint32 seedLength = N ); |
120 |
< |
void seed(); |
119 |
< |
|
120 |
< |
// Saving and loading generator state |
121 |
< |
void save( uint32* saveArray ) const; // to array of size SAVE |
122 |
< |
void load( uint32 *const loadArray ); // from such array |
123 |
< |
friend std::ostream& operator<<( std::ostream& os, const MTRand& mtrand ); |
124 |
< |
friend std::istream& operator>>( std::istream& is, MTRand& mtrand ); |
117 |
> |
// Re-seeding functions with same behavior as initializers |
118 |
> |
void seed( const uint32 oneSeed ); |
119 |
> |
void seed( uint32 *const bigSeed, const uint32 seedLength = N ); |
120 |
> |
void seed(); |
121 |
|
|
122 |
< |
protected: |
123 |
< |
void initialize( const uint32 oneSeed ); |
124 |
< |
void reload(); |
125 |
< |
uint32 hiBit( const uint32& u ) const { return u & 0x80000000UL; } |
126 |
< |
uint32 loBit( const uint32& u ) const { return u & 0x00000001UL; } |
127 |
< |
uint32 loBits( const uint32& u ) const { return u & 0x7fffffffUL; } |
128 |
< |
uint32 mixBits( const uint32& u, const uint32& v ) const |
133 |
< |
{ return hiBit(u) | loBits(v); } |
134 |
< |
uint32 twist( const uint32& m, const uint32& s0, const uint32& s1 ) const |
135 |
< |
{ return m ^ (mixBits(s0,s1)>>1) ^ (-loBit(s1) & 0x9908b0dfUL); } |
136 |
< |
static uint32 hash( time_t t, clock_t c ); |
137 |
< |
}; |
122 |
> |
std::vector<uint32>generateSeeds(); |
123 |
> |
|
124 |
> |
// Saving and loading generator state |
125 |
> |
void save( uint32* saveArray ) const; // to array of size SAVE |
126 |
> |
void load( uint32 *const loadArray ); // from such array |
127 |
> |
friend std::ostream& operator<<( std::ostream& os, const MTRand& mtrand ); |
128 |
> |
friend std::istream& operator>>( std::istream& is, MTRand& mtrand ); |
129 |
|
|
130 |
+ |
protected: |
131 |
+ |
void initialize( const uint32 oneSeed ); |
132 |
+ |
void reload(); |
133 |
+ |
uint32 hiBit( const uint32& u ) const { return u & 0x80000000UL; } |
134 |
+ |
uint32 loBit( const uint32& u ) const { return u & 0x00000001UL; } |
135 |
+ |
uint32 loBits( const uint32& u ) const { return u & 0x7fffffffUL; } |
136 |
+ |
uint32 mixBits( const uint32& u, const uint32& v ) const |
137 |
+ |
{ return hiBit(u) | loBits(v); } |
138 |
+ |
uint32 twist( const uint32& m, const uint32& s0, const uint32& s1 ) const |
139 |
+ |
{ return m ^ (mixBits(s0,s1)>>1) ^ (-loBit(s1) & 0x9908b0dfUL); } |
140 |
+ |
static uint32 hash( time_t t, clock_t c ); |
141 |
+ |
}; |
142 |
|
|
143 |
< |
inline MTRand::MTRand( const uint32& oneSeed, int nstrides, int stride) : nstrides_(nstrides), stride_(stride) { |
143 |
> |
|
144 |
> |
inline MTRand::MTRand( const uint32& oneSeed, int nstrides, int stride) : nstrides_(nstrides), stride_(stride) { |
145 |
|
assert(stride_ < nstrides_ && stride_ >= 0); |
146 |
|
seed(oneSeed); |
147 |
< |
} |
147 |
> |
} |
148 |
|
|
149 |
< |
inline MTRand::MTRand( uint32 *const bigSeed, const uint32 seedLength, int nstrides, int stride) : nstrides_(nstrides), stride_(stride) { |
149 |
> |
inline MTRand::MTRand( uint32 *const bigSeed, const uint32 seedLength, int nstrides, int stride) : nstrides_(nstrides), stride_(stride) { |
150 |
|
assert(stride_ < nstrides_ && stride_ >= 0); |
151 |
|
seed(bigSeed,seedLength); |
152 |
< |
} |
152 |
> |
} |
153 |
|
|
154 |
< |
inline MTRand::MTRand(int nstrides, int stride) : nstrides_(nstrides), stride_(stride){ |
154 |
> |
inline MTRand::MTRand(int nstrides, int stride) : nstrides_(nstrides), stride_(stride){ |
155 |
|
assert(stride_ < nstrides_ && stride_ >= 0); |
156 |
|
seed(); |
157 |
< |
} |
157 |
> |
} |
158 |
|
|
159 |
< |
inline double MTRand::rand() |
160 |
< |
{ return double(randInt()) * (1.0/4294967295.0); } |
159 |
> |
inline RealType MTRand::rand() |
160 |
> |
{ return RealType(randInt()) * (1.0/4294967295.0); } |
161 |
|
|
162 |
< |
inline double MTRand::rand( const double& n ) |
163 |
< |
{ return rand() * n; } |
162 |
> |
inline RealType MTRand::rand( const RealType& n ) |
163 |
> |
{ return rand() * n; } |
164 |
|
|
165 |
< |
inline double MTRand::randExc() |
166 |
< |
{ return double(randInt()) * (1.0/4294967296.0); } |
165 |
> |
inline RealType MTRand::randExc() |
166 |
> |
{ return RealType(randInt()) * (1.0/4294967296.0); } |
167 |
|
|
168 |
< |
inline double MTRand::randExc( const double& n ) |
169 |
< |
{ return randExc() * n; } |
168 |
> |
inline RealType MTRand::randExc( const RealType& n ) |
169 |
> |
{ return randExc() * n; } |
170 |
|
|
171 |
< |
inline double MTRand::randDblExc() |
172 |
< |
{ return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); } |
171 |
> |
inline RealType MTRand::randDblExc() |
172 |
> |
{ return ( RealType(randInt()) + 0.5 ) * (1.0/4294967296.0); } |
173 |
|
|
174 |
< |
inline double MTRand::randDblExc( const double& n ) |
175 |
< |
{ return randDblExc() * n; } |
174 |
> |
inline RealType MTRand::randDblExc( const RealType& n ) |
175 |
> |
{ return randDblExc() * n; } |
176 |
|
|
177 |
< |
inline double MTRand::rand53() |
178 |
< |
{ |
179 |
< |
uint32 a = randInt() >> 5, b = randInt() >> 6; |
180 |
< |
return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0); // by Isaku Wada |
181 |
< |
} |
177 |
> |
inline RealType MTRand::rand53() |
178 |
> |
{ |
179 |
> |
uint32 a = randInt() >> 5, b = randInt() >> 6; |
180 |
> |
return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0); // by Isaku Wada |
181 |
> |
} |
182 |
> |
|
183 |
> |
inline RealType MTRand::randNorm( const RealType mean, const RealType variance ) |
184 |
> |
{ |
185 |
> |
// Return a real number from a normal (Gaussian) distribution with given |
186 |
> |
// mean and variance by Box-Muller method |
187 |
> |
assert(variance > 0); |
188 |
> |
RealType r = sqrt( -2.0 * log( 1.0-randDblExc()) * variance); |
189 |
> |
RealType phi = 2.0 * 3.14159265358979323846264338328 * randExc(); |
190 |
> |
return mean + r * cos(phi); |
191 |
> |
} |
192 |
|
|
193 |
< |
inline double MTRand::randNorm( const double& mean, const double& variance ) |
194 |
< |
{ |
195 |
< |
// Return a real number from a normal (Gaussian) distribution with given |
196 |
< |
// mean and variance by Box-Muller method |
197 |
< |
double r = sqrt( -2.0 * log( 1.0-randDblExc()) ) * variance; |
198 |
< |
double phi = 2.0 * 3.14159265358979323846264338328 * randExc(); |
199 |
< |
return mean + r * cos(phi); |
200 |
< |
} |
187 |
< |
|
188 |
< |
/** |
189 |
< |
* This function is modified from the original to allow for random |
190 |
< |
* streams on parallel jobs. It now takes numbers from by striding |
191 |
< |
* through the random stream and picking up only one of the random |
192 |
< |
* numbers per nstrides_. The number it picks is the stride_'th |
193 |
< |
* number in the stride sequence. |
194 |
< |
*/ |
195 |
< |
inline MTRand::uint32 MTRand::randInt() { |
193 |
> |
/** |
194 |
> |
* This function is modified from the original to allow for random |
195 |
> |
* streams on parallel jobs. It now takes numbers from by striding |
196 |
> |
* through the random stream and picking up only one of the random |
197 |
> |
* numbers per nstrides_. The number it picks is the stride_'th |
198 |
> |
* number in the stride sequence. |
199 |
> |
*/ |
200 |
> |
inline MTRand::uint32 MTRand::randInt() { |
201 |
|
|
202 |
< |
uint32 ranNums[nstrides_]; |
202 |
> |
std::vector<uint32> ranNums(nstrides_); |
203 |
|
|
204 |
< |
for (int i = 0; i < nstrides_; ++i) { |
205 |
< |
ranNums[i] = rawRandInt(); |
206 |
< |
} |
204 |
> |
for (int i = 0; i < nstrides_; ++i) { |
205 |
> |
ranNums[i] = rawRandInt(); |
206 |
> |
} |
207 |
|
|
208 |
< |
return ranNums[stride_]; |
209 |
< |
} |
208 |
> |
return ranNums[stride_]; |
209 |
> |
} |
210 |
|
|
211 |
< |
/** |
212 |
< |
* This is the original randInt function which implements the mersenne |
213 |
< |
* twister. |
214 |
< |
*/ |
215 |
< |
inline MTRand::uint32 MTRand::rawRandInt() |
216 |
< |
{ |
217 |
< |
// Pull a 32-bit integer from the generator state |
218 |
< |
// Every other access function simply transforms the numbers extracted here |
211 |
> |
/** |
212 |
> |
* This is the original randInt function which implements the mersenne |
213 |
> |
* twister. |
214 |
> |
*/ |
215 |
> |
inline MTRand::uint32 MTRand::rawRandInt() |
216 |
> |
{ |
217 |
> |
// Pull a 32-bit integer from the generator state |
218 |
> |
// Every other access function simply transforms the numbers extracted here |
219 |
|
|
220 |
< |
if( left == 0 ) reload(); |
221 |
< |
--left; |
220 |
> |
if( left == 0 ) reload(); |
221 |
> |
--left; |
222 |
|
|
223 |
< |
register uint32 s1; |
224 |
< |
s1 = *pNext++; |
225 |
< |
s1 ^= (s1 >> 11); |
226 |
< |
s1 ^= (s1 << 7) & 0x9d2c5680UL; |
227 |
< |
s1 ^= (s1 << 15) & 0xefc60000UL; |
228 |
< |
return ( s1 ^ (s1 >> 18) ); |
229 |
< |
} |
223 |
> |
register uint32 s1; |
224 |
> |
s1 = *pNext++; |
225 |
> |
s1 ^= (s1 >> 11); |
226 |
> |
s1 ^= (s1 << 7) & 0x9d2c5680UL; |
227 |
> |
s1 ^= (s1 << 15) & 0xefc60000UL; |
228 |
> |
return ( s1 ^ (s1 >> 18) ); |
229 |
> |
} |
230 |
|
|
231 |
< |
inline MTRand::uint32 MTRand::randInt( const uint32& n ) |
232 |
< |
{ |
233 |
< |
// Find which bits are used in n |
234 |
< |
// Optimized by Magnus Jonsson (magnus@smartelectronix.com) |
235 |
< |
uint32 used = n; |
236 |
< |
used |= used >> 1; |
237 |
< |
used |= used >> 2; |
238 |
< |
used |= used >> 4; |
239 |
< |
used |= used >> 8; |
240 |
< |
used |= used >> 16; |
241 |
< |
|
242 |
< |
// Draw numbers until one is found in [0,n] |
243 |
< |
uint32 i; |
244 |
< |
do |
245 |
< |
i = randInt() & used; // toss unused bits to shorten search |
246 |
< |
while( i > n ); |
247 |
< |
return i; |
248 |
< |
} |
244 |
< |
|
245 |
< |
|
246 |
< |
inline void MTRand::seed( const uint32 oneSeed ) |
247 |
< |
{ |
248 |
< |
// Seed the generator with a simple uint32 |
249 |
< |
initialize(oneSeed); |
250 |
< |
reload(); |
251 |
< |
} |
252 |
< |
|
253 |
< |
|
254 |
< |
inline void MTRand::seed( uint32 *const bigSeed, const uint32 seedLength ) |
255 |
< |
{ |
256 |
< |
// Seed the generator with an array of uint32's |
257 |
< |
// There are 2^19937-1 possible initial states. This function allows |
258 |
< |
// all of those to be accessed by providing at least 19937 bits (with a |
259 |
< |
// default seed length of N = 624 uint32's). Any bits above the lower 32 |
260 |
< |
// in each element are discarded. |
261 |
< |
// Just call seed() if you want to get array from /dev/urandom |
262 |
< |
initialize(19650218UL); |
263 |
< |
register int i = 1; |
264 |
< |
register uint32 j = 0; |
265 |
< |
register int k = ( N > seedLength ? N : seedLength ); |
266 |
< |
for( ; k; --k ) |
267 |
< |
{ |
268 |
< |
state[i] = |
269 |
< |
state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL ); |
270 |
< |
state[i] += ( bigSeed[j] & 0xffffffffUL ) + j; |
271 |
< |
state[i] &= 0xffffffffUL; |
272 |
< |
++i; ++j; |
273 |
< |
if( i >= N ) { state[0] = state[N-1]; i = 1; } |
274 |
< |
if( j >= seedLength ) j = 0; |
275 |
< |
} |
276 |
< |
for( k = N - 1; k; --k ) |
277 |
< |
{ |
278 |
< |
state[i] = |
279 |
< |
state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL ); |
280 |
< |
state[i] -= i; |
281 |
< |
state[i] &= 0xffffffffUL; |
282 |
< |
++i; |
283 |
< |
if( i >= N ) { state[0] = state[N-1]; i = 1; } |
284 |
< |
} |
285 |
< |
state[0] = 0x80000000UL; // MSB is 1, assuring non-zero initial array |
286 |
< |
reload(); |
287 |
< |
} |
288 |
< |
|
289 |
< |
|
290 |
< |
inline void MTRand::seed() |
291 |
< |
{ |
292 |
< |
vector<uint32> seeds; |
293 |
< |
|
294 |
< |
seeds = generateSeeds(); |
231 |
> |
inline MTRand::uint32 MTRand::randInt( const uint32& n ) |
232 |
> |
{ |
233 |
> |
// Find which bits are used in n |
234 |
> |
// Optimized by Magnus Jonsson (magnus@smartelectronix.com) |
235 |
> |
uint32 used = n; |
236 |
> |
used |= used >> 1; |
237 |
> |
used |= used >> 2; |
238 |
> |
used |= used >> 4; |
239 |
> |
used |= used >> 8; |
240 |
> |
used |= used >> 16; |
241 |
> |
|
242 |
> |
// Draw numbers until one is found in [0,n] |
243 |
> |
uint32 i; |
244 |
> |
do |
245 |
> |
i = randInt() & used; // toss unused bits to shorten search |
246 |
> |
while( i > n ); |
247 |
> |
return i; |
248 |
> |
} |
249 |
|
|
250 |
< |
if (seeds.size() == 1) { |
251 |
< |
seed( seeds[0] ); |
252 |
< |
} else { |
253 |
< |
seed( &seeds[0], seeds.size() ); |
250 |
> |
|
251 |
> |
inline void MTRand::seed( const uint32 oneSeed ) |
252 |
> |
{ |
253 |
> |
// Seed the generator with a simple uint32 |
254 |
> |
initialize(oneSeed); |
255 |
> |
reload(); |
256 |
|
} |
301 |
– |
} |
257 |
|
|
258 |
|
|
259 |
< |
inline vector<uint32> MTRand::generateSeeds() { |
260 |
< |
// Seed the generator with an array from /dev/urandom if available |
261 |
< |
// Otherwise use a hash of time() and clock() values |
259 |
> |
inline void MTRand::seed( uint32 *const bigSeed, const uint32 seedLength ) |
260 |
> |
{ |
261 |
> |
// Seed the generator with an array of uint32's |
262 |
> |
// There are 2^19937-1 possible initial states. This function allows |
263 |
> |
// all of those to be accessed by providing at least 19937 bits (with a |
264 |
> |
// default seed length of N = 624 uint32's). Any bits above the lower 32 |
265 |
> |
// in each element are discarded. |
266 |
> |
// Just call seed() if you want to get array from /dev/urandom |
267 |
> |
initialize(19650218UL); |
268 |
> |
register int i = 1; |
269 |
> |
register uint32 j = 0; |
270 |
> |
register int k = ( N > seedLength ? N : seedLength ); |
271 |
> |
for( ; k; --k ) |
272 |
> |
{ |
273 |
> |
state[i] = |
274 |
> |
state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL ); |
275 |
> |
state[i] += ( bigSeed[j] & 0xffffffffUL ) + j; |
276 |
> |
state[i] &= 0xffffffffUL; |
277 |
> |
++i; ++j; |
278 |
> |
if( i >= N ) { state[0] = state[N-1]; i = 1; } |
279 |
> |
if( j >= seedLength ) j = 0; |
280 |
> |
} |
281 |
> |
for( k = N - 1; k; --k ) |
282 |
> |
{ |
283 |
> |
state[i] = |
284 |
> |
state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL ); |
285 |
> |
state[i] -= i; |
286 |
> |
state[i] &= 0xffffffffUL; |
287 |
> |
++i; |
288 |
> |
if( i >= N ) { state[0] = state[N-1]; i = 1; } |
289 |
> |
} |
290 |
> |
state[0] = 0x80000000UL; // MSB is 1, assuring non-zero initial array |
291 |
> |
reload(); |
292 |
> |
} |
293 |
|
|
308 |
– |
vector<uint32> bigSeed; |
294 |
|
|
295 |
< |
// First try getting an array from /dev/urandom |
296 |
< |
FILE* urandom = fopen( "/dev/urandom", "rb" ); |
297 |
< |
if( urandom ) |
298 |
< |
{ |
299 |
< |
bigSeed.resize(N); |
300 |
< |
register uint32 *s = &bigSeed[0]; |
301 |
< |
register int i = N; |
302 |
< |
register bool success = true; |
303 |
< |
while( success && i-- ) |
304 |
< |
success = fread( s++, sizeof(uint32), 1, urandom ); |
320 |
< |
fclose(urandom); |
321 |
< |
if( success ) { return bigSeed; } |
295 |
> |
inline void MTRand::seed() |
296 |
> |
{ |
297 |
> |
std::vector<uint32> seeds; |
298 |
> |
|
299 |
> |
seeds = generateSeeds(); |
300 |
> |
|
301 |
> |
if (seeds.size() == 1) { |
302 |
> |
seed( seeds[0] ); |
303 |
> |
} else { |
304 |
> |
seed( &seeds[0], seeds.size() ); |
305 |
|
} |
306 |
+ |
} |
307 |
+ |
|
308 |
+ |
|
309 |
+ |
inline std::vector<MTRand::uint32> MTRand::generateSeeds() { |
310 |
+ |
// Seed the generator with an array from /dev/urandom if available |
311 |
+ |
// Otherwise use a hash of time() and clock() values |
312 |
+ |
|
313 |
+ |
std::vector<uint32> bigSeed; |
314 |
+ |
|
315 |
+ |
// First try getting an array from /dev/urandom |
316 |
+ |
FILE* urandom = fopen( "/dev/urandom", "rb" ); |
317 |
+ |
if( urandom ) |
318 |
+ |
{ |
319 |
+ |
bigSeed.resize(N); |
320 |
+ |
register uint32 *s = &bigSeed[0]; |
321 |
+ |
register int i = N; |
322 |
+ |
register bool success = true; |
323 |
+ |
while( success && i-- ) |
324 |
+ |
success = fread( s++, sizeof(uint32), 1, urandom ); |
325 |
+ |
fclose(urandom); |
326 |
+ |
if( success ) { return bigSeed; } |
327 |
+ |
} |
328 |
|
|
329 |
< |
// Was not successful, so use time() and clock() instead |
329 |
> |
// Was not successful, so use time() and clock() instead |
330 |
|
|
331 |
< |
bigSeed.push_back(hash( time(NULL), clock())); |
332 |
< |
return bigSeed; |
333 |
< |
} |
331 |
> |
bigSeed.push_back(hash( time(NULL), clock())); |
332 |
> |
return bigSeed; |
333 |
> |
} |
334 |
|
|
335 |
|
|
336 |
< |
inline void MTRand::initialize( const uint32 seed ) |
337 |
< |
{ |
338 |
< |
// Initialize generator state with seed |
339 |
< |
// See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier. |
340 |
< |
// In previous versions, most significant bits (MSBs) of the seed affect |
341 |
< |
// only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto. |
342 |
< |
register uint32 *s = state; |
343 |
< |
register uint32 *r = state; |
344 |
< |
register int i = 1; |
345 |
< |
*s++ = seed & 0xffffffffUL; |
346 |
< |
for( ; i < N; ++i ) |
347 |
< |
{ |
348 |
< |
*s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL; |
349 |
< |
r++; |
350 |
< |
} |
351 |
< |
} |
336 |
> |
inline void MTRand::initialize( const uint32 seed ) |
337 |
> |
{ |
338 |
> |
// Initialize generator state with seed |
339 |
> |
// See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier. |
340 |
> |
// In previous versions, most significant bits (MSBs) of the seed affect |
341 |
> |
// only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto. |
342 |
> |
register uint32 *s = state; |
343 |
> |
register uint32 *r = state; |
344 |
> |
register int i = 1; |
345 |
> |
*s++ = seed & 0xffffffffUL; |
346 |
> |
for( ; i < N; ++i ) |
347 |
> |
{ |
348 |
> |
*s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL; |
349 |
> |
r++; |
350 |
> |
} |
351 |
> |
} |
352 |
|
|
353 |
|
|
354 |
< |
inline void MTRand::reload() |
355 |
< |
{ |
356 |
< |
// Generate N new values in state |
357 |
< |
// Made clearer and faster by Matthew Bellew (matthew.bellew@home.com) |
358 |
< |
register uint32 *p = state; |
359 |
< |
register int i; |
360 |
< |
for( i = N - M; i--; ++p ) |
361 |
< |
*p = twist( p[M], p[0], p[1] ); |
362 |
< |
for( i = M; --i; ++p ) |
363 |
< |
*p = twist( p[M-N], p[0], p[1] ); |
364 |
< |
*p = twist( p[M-N], p[0], state[0] ); |
354 |
> |
inline void MTRand::reload() |
355 |
> |
{ |
356 |
> |
// Generate N new values in state |
357 |
> |
// Made clearer and faster by Matthew Bellew (matthew.bellew@home.com) |
358 |
> |
register uint32 *p = state; |
359 |
> |
register int i; |
360 |
> |
for( i = N - M; i--; ++p ) |
361 |
> |
*p = twist( p[M], p[0], p[1] ); |
362 |
> |
for( i = M; --i; ++p ) |
363 |
> |
*p = twist( p[M-N], p[0], p[1] ); |
364 |
> |
*p = twist( p[M-N], p[0], state[0] ); |
365 |
|
|
366 |
< |
left = N, pNext = state; |
367 |
< |
} |
366 |
> |
left = N, pNext = state; |
367 |
> |
} |
368 |
|
|
369 |
|
|
370 |
< |
inline MTRand::uint32 MTRand::hash( time_t t, clock_t c ) |
371 |
< |
{ |
372 |
< |
// Get a uint32 from t and c |
373 |
< |
// Better than uint32(x) in case x is floating point in [0,1] |
374 |
< |
// Based on code by Lawrence Kirby (fred@genesis.demon.co.uk) |
370 |
> |
inline MTRand::uint32 MTRand::hash( time_t t, clock_t c ) |
371 |
> |
{ |
372 |
> |
// Get a uint32 from t and c |
373 |
> |
// Better than uint32(x) in case x is floating point in [0,1] |
374 |
> |
// Based on code by Lawrence Kirby (fred@genesis.demon.co.uk) |
375 |
|
|
376 |
< |
static uint32 differ = 0; // guarantee time-based seeds will change |
376 |
> |
static uint32 differ = 0; // guarantee time-based seeds will change |
377 |
|
|
378 |
< |
uint32 h1 = 0; |
379 |
< |
unsigned char *p = (unsigned char *) &t; |
380 |
< |
for( size_t i = 0; i < sizeof(t); ++i ) |
381 |
< |
{ |
382 |
< |
h1 *= UCHAR_MAX + 2U; |
383 |
< |
h1 += p[i]; |
384 |
< |
} |
385 |
< |
uint32 h2 = 0; |
386 |
< |
p = (unsigned char *) &c; |
387 |
< |
for( size_t j = 0; j < sizeof(c); ++j ) |
388 |
< |
{ |
389 |
< |
h2 *= UCHAR_MAX + 2U; |
390 |
< |
h2 += p[j]; |
391 |
< |
} |
392 |
< |
return ( h1 + differ++ ) ^ h2; |
393 |
< |
} |
378 |
> |
uint32 h1 = 0; |
379 |
> |
unsigned char *p = (unsigned char *) &t; |
380 |
> |
for( size_t i = 0; i < sizeof(t); ++i ) |
381 |
> |
{ |
382 |
> |
h1 *= UCHAR_MAX + 2U; |
383 |
> |
h1 += p[i]; |
384 |
> |
} |
385 |
> |
uint32 h2 = 0; |
386 |
> |
p = (unsigned char *) &c; |
387 |
> |
for( size_t j = 0; j < sizeof(c); ++j ) |
388 |
> |
{ |
389 |
> |
h2 *= UCHAR_MAX + 2U; |
390 |
> |
h2 += p[j]; |
391 |
> |
} |
392 |
> |
return ( h1 + differ++ ) ^ h2; |
393 |
> |
} |
394 |
|
|
395 |
|
|
396 |
< |
inline void MTRand::save( uint32* saveArray ) const |
397 |
< |
{ |
398 |
< |
register uint32 *sa = saveArray; |
399 |
< |
register const uint32 *s = state; |
400 |
< |
register int i = N; |
401 |
< |
for( ; i--; *sa++ = *s++ ) {} |
402 |
< |
*sa = left; |
403 |
< |
} |
396 |
> |
inline void MTRand::save( uint32* saveArray ) const |
397 |
> |
{ |
398 |
> |
register uint32 *sa = saveArray; |
399 |
> |
register const uint32 *s = state; |
400 |
> |
register int i = N; |
401 |
> |
for( ; i--; *sa++ = *s++ ) {} |
402 |
> |
*sa = left; |
403 |
> |
} |
404 |
|
|
405 |
|
|
406 |
< |
inline void MTRand::load( uint32 *const loadArray ) |
407 |
< |
{ |
408 |
< |
register uint32 *s = state; |
409 |
< |
register uint32 *la = loadArray; |
410 |
< |
register int i = N; |
411 |
< |
for( ; i--; *s++ = *la++ ) {} |
412 |
< |
left = *la; |
413 |
< |
pNext = &state[N-left]; |
414 |
< |
} |
406 |
> |
inline void MTRand::load( uint32 *const loadArray ) |
407 |
> |
{ |
408 |
> |
register uint32 *s = state; |
409 |
> |
register uint32 *la = loadArray; |
410 |
> |
register int i = N; |
411 |
> |
for( ; i--; *s++ = *la++ ) {} |
412 |
> |
left = *la; |
413 |
> |
pNext = &state[N-left]; |
414 |
> |
} |
415 |
|
|
416 |
|
|
417 |
< |
inline std::ostream& operator<<( std::ostream& os, const MTRand& mtrand ) |
418 |
< |
{ |
419 |
< |
register const MTRand::uint32 *s = mtrand.state; |
420 |
< |
register int i = mtrand.N; |
421 |
< |
for( ; i--; os << *s++ << "\t" ) {} |
422 |
< |
return os << mtrand.left; |
423 |
< |
} |
417 |
> |
inline std::ostream& operator<<( std::ostream& os, const MTRand& mtrand ) |
418 |
> |
{ |
419 |
> |
register const MTRand::uint32 *s = mtrand.state; |
420 |
> |
register int i = mtrand.N; |
421 |
> |
for( ; i--; os << *s++ << "\t" ) {} |
422 |
> |
return os << mtrand.left; |
423 |
> |
} |
424 |
|
|
425 |
|
|
426 |
< |
inline std::istream& operator>>( std::istream& is, MTRand& mtrand ) |
427 |
< |
{ |
428 |
< |
register MTRand::uint32 *s = mtrand.state; |
429 |
< |
register int i = mtrand.N; |
430 |
< |
for( ; i--; is >> *s++ ) {} |
431 |
< |
is >> mtrand.left; |
432 |
< |
mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left]; |
433 |
< |
return is; |
434 |
< |
} |
426 |
> |
inline std::istream& operator>>( std::istream& is, MTRand& mtrand ) |
427 |
> |
{ |
428 |
> |
register MTRand::uint32 *s = mtrand.state; |
429 |
> |
register int i = mtrand.N; |
430 |
> |
for( ; i--; is >> *s++ ) {} |
431 |
> |
is >> mtrand.left; |
432 |
> |
mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left]; |
433 |
> |
return is; |
434 |
> |
} |
435 |
|
|
436 |
+ |
} |
437 |
|
#endif // MERSENNETWISTER_H |
438 |
|
|
439 |
|
// Change log: |