-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRandom1.cpp
More file actions
37 lines (29 loc) · 798 Bytes
/
Random1.cpp
File metadata and controls
37 lines (29 loc) · 798 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
#include "Random1.h"
#include <cstdlib>
#include <cmath>
// the basic math functions should be in namespace
// std but aren’t in VCPP6
#if !defined(_MSC_VER)
using namespace std;
#endif
double getOneGaussianBySummation() {
double result = 0;
for (unsigned long i = 0; i < 12; i++) {
result += rand() / static_cast<double>(RAND_MAX);
}
result -= 6.0;
return result;
}
double getOneGaussianByBoxMuller() {
double result;
double x;
double y;
double sizeSquared;
do {
x = 2.0 * rand() / static_cast<double>(RAND_MAX) - 1.0;
y = 2.0 * rand() / static_cast<double>(RAND_MAX) - 1.0;
sizeSquared = x*x + y*y;
} while (sizeSquared >= 1.0);
result = x * sqrt(-2 * log(sizeSquared) / sizeSquared);
return result;
}