-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.cpp
More file actions
executable file
·52 lines (42 loc) · 847 Bytes
/
Random.cpp
File metadata and controls
executable file
·52 lines (42 loc) · 847 Bytes
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
#include "math.h"
#include "sasa_RandomGenerator.h"
#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
#define MASK 123459876
Random::Random(int aSeed) : seed(aSeed) {};
long double Random::Uniform()
{
seed ^= MASK;
int k = seed/IQ;
seed = IA*(seed - k * IQ) - IR * k;
if (seed<0)
seed += IM;
long double ans = AM * seed;
seed ^= MASK;
return ans;
};
long double Random::Normal()
{
static bool iset = true;
static long double gset = 0;
long double v1, v2;
long double r = 2.0;
if(iset) {
do {
v1= 2.0*Uniform()-1.0;
v2= 2.0*Uniform()-1.0;
r = v1*v1 + v2*v2;
} while (r>=1);
long double fac = sqrt(-2.0*log(r)/r);
gset = v1 * fac;
iset = false;
return v2 * fac;
}
else {
iset = true;
return gset;
}
};