PSCF v1.1
Random.cpp
1#include "Random.h"
2
3#include <cmath>
4#include <cstdio>
5#include <sys/time.h>
6
7namespace Util
8{
9
10 /*
11 * Constructor.
12 */
14 : engine_(),
15 seed_(0),
16 isInitialized_(false)
17 { setClassName("Random"); }
18
19 /*
20 * Destructor.
21 */
23 {}
24
25 /*
26 * Read random seed and initialize.
27 */
28 void Random::readParameters(std::istream &in)
29 {
30 read<SeedType>(in, "seed", seed_);
31 setSeed();
32 }
33
34 /*
35 * Load random seed and internal state.
36 */
38 {
39 loadParameter<SeedType>(ar, "seed", seed_);
40 ar >> engine_;
41 }
42
43 /*
44 * Save internal state.
45 */
47 {
48 ar << seed_;
49 ar << engine_;
50 }
51
52 /*
53 * Sets of random seed, and initializes random number generator.
54 *
55 * \param seed value for random seed (private member variable seed)
56 */
57 void Random::setSeed(Random::SeedType seed)
58 {
59 seed_ = seed;
60 setSeed();
61 }
62
63 /*
64 * If seed_ == 0, use a seed taken from the clock. Otherwise, use
65 * seed_ as an integer seed for the random number generator.
66 */
67 void Random::setSeed()
68 {
69 // If seed == 0, replace with random seed from clock.
70 if (seed_ == 0) {
71 SeedType temp;
72 //temp = static_cast<SeedType>(std::time(0));
73 timeval time;
74 gettimeofday(&time, NULL);
75 temp = time.tv_sec + 1123*time.tv_usec;
76 #ifdef UTIL_MPI
77 if (MPI::Is_initialized()) {
78 SeedType rank = MPI::COMM_WORLD.Get_rank();
79 temp += rank*(31 + time.tv_usec);
80 }
81 #endif
82 engine_.seed(temp);
83 } else {
84 engine_.seed(seed_);
85 }
86 isInitialized_ = true;
87 }
88
89 /*
90 * Return Gaussian distributed random number.
91 */
92 double Random::gaussian(void)
93 {
94 static bool iset = false;
95 static double zGauss1;
96 double zGauss2;
97
98 if (iset == false) {
99 double v1;
100 double v2;
101 double rsq=2.0;
102
103 while( rsq >= 1.0) {
104 v1 = 2.0*uniform(-1.0, 1.0);
105 v2 = 2.0*uniform(-1.0, 1.0);
106 rsq = v1*v1 + v2*v2;
107 }
108 double fac = sqrt(-2.0*log(rsq)/rsq);
109 zGauss1 = v1*fac;
110 zGauss2 = v2*fac;
111 iset = true;
112 } else {
113 zGauss2 = zGauss1;
114 iset = false;
115 }
116 return zGauss2;
117 }
118
119 /*
120 * unit vector with uniform probability over the unit sphere
121 */
123 {
124 double ran1=0, ran2=0, ranh;
125 double ransq;
126
127 ransq=2.0;
128 while (ransq >= 1.0) {
129 ran1 = 1.0-2.0*uniform(0.0, 1.0);
130 ran2 = 1.0-2.0*uniform(0.0, 1.0);
131 ransq = ran1*ran1 + ran2*ran2;
132 }
133 ranh= 2.0*sqrt(1.0-ransq);
134 v[0] = ran1*ranh;
135 v[1] = ran2*ranh;
136 v[2] = 1.0 - 2.0*ransq;
137 }
138
139
140}
Saving archive for binary istream.
Saving / output archive for binary ostream.
void setClassName(const char *className)
Set class name string.
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
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
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
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