PSCF v1.1
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.
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 uniformly distributed 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 double x = uniform(range1, range2);
226 return long(x);
227 }
228
229 /*
230 * Choose one of several outcomes with a specified set of probabilities.
231 *
232 * Precondition: Elements of probability array must add to 1.0
233 *
234 * \param probability[] array of probabilities, for indices 0,...,size-1
235 * \param size number of options
236 */
237 inline long Random::drawFrom(double probability[], long size) {
238 double roulette = uniform();
239 long n=0;
240 double cumProb = probability[0];
241 while ( cumProb < roulette && n < size ) {
242 ++n;
243 cumProb += probability[n];
244 }
245 return(n);
246 }
247
248 /*
249 * Get random point within a rectangular prism.
250 */
251 inline void Random::getPoint(double minR[], double maxR[], double r[]) {
252 r[0] = uniform(minR[0], maxR[0]);
253 r[1] = uniform(minR[1], maxR[1]);
254 r[2] = uniform(minR[2], maxR[2]);
255 }
256
257 /*
258 * Metropolis criteria for whether to accept a MC move.
259 */
260 inline bool Random::metropolis(double ratio)
261 {
262 if ( ratio > 1.0 ) {
263 return true;
264 } else {
265 double ran = uniform();
266 if ( ran < ratio ) {
267 return true;
268 } else {
269 return false;
270 }
271 }
272 }
273
274 /*
275 * Returns value of random seed (private member variable idum)
276 */
277 inline long Random::seed()
278 { return seed_; }
279
280 /*
281 * Serialize to/from an archive.
282 */
283 template <class Archive>
284 void Random::serialize(Archive& ar, const unsigned int version)
285 {
286 ar & engine_;
287 ar & seed_;
288 }
289
290}
291#undef UTIL_ENGINE
292#endif // ifndef UTIL_RANDOM_H
Saving archive for binary istream.
Saving / output archive for binary ostream.
An object that can read multiple parameters from file.
Random number generator.
Definition: Random.h:47
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:251
long uniformInt(long range1, long range2)
Return random long int x uniformly distributed 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:260
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:277
long drawFrom(double probability[], long size)
Choose one of several outcomes with a specified set of probabilities.
Definition: Random.h:237
void serialize(Archive &ar, const unsigned int version)
Serialize to/from an archive.
Definition: Random.h:284
virtual void save(Serializable::OArchive &ar)
Save internal state to file.
Definition: Random.cpp:46
A Vector is a Cartesian vector.
Definition: Vector.h:76
Utility classes for scientific computation.
Definition: accumulators.mod:1