67 |
|
#include <time.h> |
68 |
|
#include <math.h> |
69 |
|
#include <vector> |
70 |
< |
namespace oopse { |
70 |
> |
namespace OpenMD { |
71 |
|
|
72 |
|
class MTRand { |
73 |
|
// Data |
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) |
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 |
< |
double operator()() { return rand(); } // same as rand() |
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 ); |
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 ); |
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 |
+ |
#ifdef _MSC_VER |
139 |
+ |
#pragma warning( push ) // save current warning settings |
140 |
+ |
#pragma warning( disable : 4146 ) // warning C4146: unary minus operator applied to unsigned type, result still unsigned |
141 |
+ |
#endif |
142 |
|
uint32 twist( const uint32& m, const uint32& s0, const uint32& s1 ) const |
143 |
|
{ return m ^ (mixBits(s0,s1)>>1) ^ (-loBit(s1) & 0x9908b0dfUL); } |
144 |
+ |
#ifdef _MSC_VER |
145 |
+ |
#pragma warning( pop ) // return warning settings to what they were |
146 |
+ |
#endif |
147 |
+ |
|
148 |
|
static uint32 hash( time_t t, clock_t c ); |
149 |
|
}; |
150 |
|
|
164 |
|
seed(); |
165 |
|
} |
166 |
|
|
167 |
< |
inline double MTRand::rand() |
168 |
< |
{ return double(randInt()) * (1.0/4294967295.0); } |
167 |
> |
inline RealType MTRand::rand() |
168 |
> |
{ return RealType(randInt()) * (1.0/4294967295.0); } |
169 |
|
|
170 |
< |
inline double MTRand::rand( const double& n ) |
170 |
> |
inline RealType MTRand::rand( const RealType& n ) |
171 |
|
{ return rand() * n; } |
172 |
|
|
173 |
< |
inline double MTRand::randExc() |
174 |
< |
{ return double(randInt()) * (1.0/4294967296.0); } |
173 |
> |
inline RealType MTRand::randExc() |
174 |
> |
{ return RealType(randInt()) * (1.0/4294967296.0); } |
175 |
|
|
176 |
< |
inline double MTRand::randExc( const double& n ) |
176 |
> |
inline RealType MTRand::randExc( const RealType& n ) |
177 |
|
{ return randExc() * n; } |
178 |
|
|
179 |
< |
inline double MTRand::randDblExc() |
180 |
< |
{ return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); } |
179 |
> |
inline RealType MTRand::randDblExc() |
180 |
> |
{ return ( RealType(randInt()) + 0.5 ) * (1.0/4294967296.0); } |
181 |
|
|
182 |
< |
inline double MTRand::randDblExc( const double& n ) |
182 |
> |
inline RealType MTRand::randDblExc( const RealType& n ) |
183 |
|
{ return randDblExc() * n; } |
184 |
|
|
185 |
< |
inline double MTRand::rand53() |
185 |
> |
inline RealType MTRand::rand53() |
186 |
|
{ |
187 |
|
uint32 a = randInt() >> 5, b = randInt() >> 6; |
188 |
|
return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0); // by Isaku Wada |
189 |
|
} |
190 |
|
|
191 |
< |
inline double MTRand::randNorm( const double mean, const double variance ) |
191 |
> |
inline RealType MTRand::randNorm( const RealType mean, const RealType variance ) |
192 |
|
{ |
193 |
|
// Return a real number from a normal (Gaussian) distribution with given |
194 |
|
// mean and variance by Box-Muller method |
195 |
|
assert(variance > 0); |
196 |
< |
double r = sqrt( -2.0 * log( 1.0-randDblExc()) * variance); |
197 |
< |
double phi = 2.0 * 3.14159265358979323846264338328 * randExc(); |
196 |
> |
RealType r = sqrt( -2.0 * log( 1.0-randDblExc()) * variance); |
197 |
> |
RealType phi = 2.0 * 3.14159265358979323846264338328 * randExc(); |
198 |
|
return mean + r * cos(phi); |
199 |
|
} |
200 |
|
|
206 |
|
* number in the stride sequence. |
207 |
|
*/ |
208 |
|
inline MTRand::uint32 MTRand::randInt() { |
209 |
< |
|
209 |
> |
|
210 |
|
std::vector<uint32> ranNums(nstrides_); |
211 |
|
|
212 |
|
for (int i = 0; i < nstrides_; ++i) { |
329 |
|
register int i = N; |
330 |
|
register bool success = true; |
331 |
|
while( success && i-- ) |
332 |
< |
success = fread( s++, sizeof(uint32), 1, urandom ); |
332 |
> |
success = (fread( s++, sizeof(uint32), 1, urandom ) == 0); |
333 |
|
fclose(urandom); |
334 |
|
if( success ) { return bigSeed; } |
335 |
|
} |