-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.cpp
133 lines (115 loc) · 2.82 KB
/
random.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "common.h"
const long MBIG = 1000000000;
const long MSEED = time(NULL);
const long MZ = 0;
const double FAC = (1.0 / MBIG);
double idum;
double RAN3() {
static int inext, inextp;
static double ma[56];
static int iff = 0;
long mj, mk;
int i, ii, k;
if (idum < 0 || iff == 0) {
iff = 1;
mj = MSEED - (long) (idum < 0 ? -idum : idum);
mj = mj % MBIG;
ma[55] = mj;
mk = 1;
for (i = 1; i <= 54; i++) {
ii = (21 * i) % 55;
ma[ii] = mk;
mk = mj - mk;
if (mk < MZ) {
mk += MBIG;
}
mj = (long) ma[ii];
}
for (k = 1; k <= 4; k++)
for (i = 1; i <= 55; i++) {
ma[i] -= ma[1 + (i + 30) % 55];
if (ma[i] < MZ) {
ma[i] += MBIG;
}
}
inext = 0;
inextp = 31;
idum = 1;
}
if (++inext == 56) inext = 1;
if (++inextp == 56) inextp = 1;
mj = (long) (ma[inext] - ma[inextp]);
if (mj < MZ) {
mj += MBIG;
}
ma[inext] = mj;
return mj*FAC;
}
int RANDINT(int low, int high) {
return (int) (RAN3()*((double) high - low)) + low;
}
double MIN(double a, double b) {
return (a < b ? a : b);
}
double ROUND(double d) {
return floor(d + 0.5);
}
/* The following two functions draw random numbers from a
* Gaussian distribution with a set mean and standard deviation.
* Uses the Box-Mueller algorithm.
*/
double RANDGAUSS() {
return RANDGAUSS(0.0, 1.0);
}
double RANDGAUSS(double mean, double stdev) {
static int turn = 0;
static double x1, x2, r;
if (turn == 1) {
turn = 0;
return mean + x2 * r*stdev;
} else {
r = 1.0;
while (r >= 1.0) {
x1 = (2.0 * RAN3()) - 1.0;
x2 = (2.0 * RAN3()) - 1.0;
r = x1 * x1 + x2*x2;
}
r = sqrt(-2.0 * log(r) / r);
turn = 1;
return mean + x1 * r*stdev;
}
}
double * RANDUNITVECTOR() {
double * vector = new double[3];
double rx, ry, r0, rz, r2 = 2.0;
while (r2 >= 1.0) {
rx = 1 - 2 * RAN3();
ry = 1 - 2 * RAN3();
r2 = rx * rx + ry*ry;
}
r0 = 2 * sqrt(1 - r2);
rx *= r0;
ry *= r0;
rz = 1 - 2 * r2;
vector[0] = rx;
vector[1] = ry;
vector[2] = rz;
return vector;
}
string TIMESTAMP() {
char the_date[BUF_SIZE];
the_date[0] = '\0';
time_t now = time(NULL);
if (now != -1) {
strftime(the_date, BUF_SIZE, "%Y%m%d_%H%M", localtime(&now));
}
return string(the_date);
}
void ASSERT(bool expression, string error_msg) {
if (!expression) {
cerr << "\nFATAL: " << error_msg << endl
<< "\nPREMATURELY TERMINATING PROGRAM...\n" << endl;
abort();
}
return;
}