-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom.h
48 lines (46 loc) · 908 Bytes
/
random.h
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
class Random {
static bool flag;
static double r1, r2;
public:
static void init () {
long seed = 0;
time(&seed);
srand48(seed);
flag = false;
}
static double ran() {
return drand48();
}
static double ran1(double xl, double xu) {
return ran() * (xu-xl) + xl;
}
static double rand_normal() {
if (flag) {
flag = false;
return r2;
}
double rbase = sqrt(-2.0 * log(ran()));
double rt = 2 * M_PI * ran();
r1 = rbase * cos (rt);
r2 = rbase * sin (rt);
flag = true;
return r1;
}
static double randiam () {
double dmin = 0.5, dmax = 1;
double x, y, c2 = dmax * dmin / (dmax - dmin);
do {
x = ran1(dmin, dmax);
y = ran1(0.0, c2 / (dmin * dmin));
} while (y > c2 / (x * x));
return x;
}
/*
static double ran3 (double z) {
return (Diam_max - Diam_min)*z*z / (Box_z * Box_z) + Diam_min;
}
*/
};
bool Random::flag;
double Random::r1;
double Random::r2;