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 ); |
156 |
|
seed(); |
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 ) |
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 ) |
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 ) |
174 |
> |
inline RealType MTRand::randDblExc( const RealType& n ) |
175 |
|
{ return randDblExc() * n; } |
176 |
|
|
177 |
< |
inline double MTRand::rand53() |
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 double MTRand::randNorm( const double mean, const double variance ) |
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 |
< |
double r = sqrt( -2.0 * log( 1.0-randDblExc()) * variance); |
189 |
< |
double phi = 2.0 * 3.14159265358979323846264338328 * randExc(); |
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 |
|
|
198 |
|
* number in the stride sequence. |
199 |
|
*/ |
200 |
|
inline MTRand::uint32 MTRand::randInt() { |
201 |
< |
|
201 |
> |
|
202 |
|
std::vector<uint32> ranNums(nstrides_); |
203 |
|
|
204 |
|
for (int i = 0; i < nstrides_; ++i) { |