PSCF v1.4.0
Random.h
1#ifndef UTIL_RANDOM_H
2#define UTIL_RANDOM_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/param/ParamComposite.h>
12#include <util/space/Vector.h>
13
14#include <cmath>
15
16#include <util/random/mersenne/mtrand.h>
17#define UTIL_ENGINE MTRand_int32
18
19namespace Util
20{
21
22 class Vector;
23
46 class Random : public ParamComposite
47 {
48
49 public:
50
51 typedef unsigned long SeedType;
52
56 Random();
57
61 virtual ~Random();
62
68 virtual void readParameters(std::istream &in);
69
75 virtual void loadParameters(Serializable::IArchive &ar);
76
82 virtual void save(Serializable::OArchive &ar);
83
89 void setSeed(SeedType seed);
90
97 double uniform();
98
105 double uniform(double range1, double range2);
106
114 long uniformInt(long range1, long range2);
115
123 void getPoint(double minR[], double maxR[], double r[]);
124
130 double gaussian(void);
131
137 void unitVector(Vector& v);
138
149 bool metropolis(double ratio);
150
161 long drawFrom(double probability[], long size);
162
166 template <class Archive>
167 void serialize(Archive& ar, const unsigned int version);
168
174 long seed();
175
176 private:
177
181 UTIL_ENGINE engine_;
182
186 SeedType seed_;
187
189 bool isInitialized_;
190
194 void setSeed();
195
196 };
197
198 // Inline methods
199
200 /*
201 * Get a uniform double precision number.
202 */
203 inline double Random::uniform()
204 {
205 // Divide 32 bit integer engine output by 2^32
206 return static_cast<double>(engine_()) * (1.0/ 4294967296.0);
207 }
208
209 /*
210 * Get a uniform double precision number in range1 <= x < range2.
211 */
212 inline double Random::uniform(double range1, double range2)
213 {
214 // Divide 32 bit integer engine output by 2^32
215 double frac = static_cast<double>(engine_()) * (1.0/ 4294967296.0);
216 return range1 + (range2 - range1)*frac;
217 }
218
219 /*
220 * Return a random long integer x uniform in range1 <= x < range2.
221 *
222 * Parameters range1 and range2 must be within the range of long integers.
223 */
224 inline long Random::uniformInt(long range1, long range2) {
225 assert(range2 > range1);
226 double x = uniform(0, range2 - range1);
227 return range1 + long(x);
228 }
229
230 /*
231 * Choose one of several outcomes with a specified set of probabilities.
232 *
233 * Precondition: Elements of probability array must add to 1.0
234 *
235 * \param probability[] array of probabilities, for indices 0,...,size-1
236 * \param size number of options
237 */
238 inline long Random::drawFrom(double probability[], long size) {
239 double roulette = uniform();
240 long n=0;
241 double cumProb = probability[0];
242 while ( cumProb < roulette && n < size ) {
243 ++n;
244 cumProb += probability[n];
245 }
246 return(n);
247 }
248
249 /*
250 * Get random point within a rectangular prism.
251 */
252 inline void Random::getPoint(double minR[], double maxR[], double r[]) {
253 r[0] = uniform(minR[0], maxR[0]);
254 r[1] = uniform(minR[1], maxR[1]);
255 r[2] = uniform(minR[2], maxR[2]);
256 }
257
258 /*
259 * Metropolis criteria for whether to accept a MC move.
260 */
261 inline bool Random::metropolis(double ratio)
262 {
263 if ( ratio > 1.0 ) {
264 return true;
265 } else {
266 double ran = uniform();
267 if ( ran < ratio ) {
268 return true;
269 } else {
270 return false;
271 }
272 }
273 }
274
275 /*
276 * Returns value of random seed (private member variable idum)
277 */
278 inline long Random::seed()
279 { return seed_; }
280
281 /*
282 * Serialize to/from an archive.
283 */
284 template <class Archive>
285 void Random::serialize(Archive& ar, const unsigned int version)
286 {
287 ar & engine_;
288 ar & seed_;
289 }
290
291}
292#undef UTIL_ENGINE
293#endif // ifndef UTIL_RANDOM_H
ParamComposite()
Constructor.
void setSeed(SeedType seed)
Sets of random seed, and initializes random number generator.
Definition Random.cpp:57
virtual void readParameters(std::istream &in)
Read seed from file, initialize RNG.
Definition Random.cpp:28
void getPoint(double minR[], double maxR[], double r[])
Generate a random point in a box.
Definition Random.h:252
long uniformInt(long range1, long range2)
Return random long int x uniform in range1 <= x < range2.
Definition Random.h:224
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from file.
Definition Random.cpp:37
virtual ~Random()
Destructor.
Definition Random.cpp:22
Random()
Constructor.
Definition Random.cpp:13
void unitVector(Vector &v)
Generate unit vector with uniform probability over the unit sphere.
Definition Random.cpp:122
bool metropolis(double ratio)
Metropolis algorithm for whether to accept a MC move.
Definition Random.h:261
double uniform()
Return a random floating point number x, uniformly distributed in the range 0 <= x < 1.
Definition Random.h:203
double gaussian(void)
Return a Gaussian random number with zero average and unit variance.
Definition Random.cpp:92
long seed()
Returns value of random seed (private member variable seed_).
Definition Random.h:278
long drawFrom(double probability[], long size)
Choose one of several outcomes with a specified set of probabilities.
Definition Random.h:238
void serialize(Archive &ar, const unsigned int version)
Serialize to/from an archive.
Definition Random.h:285
virtual void save(Serializable::OArchive &ar)
Save internal state to file.
Definition Random.cpp:46
BinaryFileIArchive IArchive
Type of input archive used by load method.
BinaryFileOArchive OArchive
Type of output archive used by save method.
A Vector is a Cartesian vector.
Definition Vector.h:76
Utility classes for scientific computation.