-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom1.cpp
50 lines (40 loc) · 874 Bytes
/
Random1.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
//
// Random1.cpp
//
//
// Created by carlos on 26/10/2018.
//
//
#include "Random1.hpp"
#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 j=0; j < 12; j++)
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;
y = 2.0*rand()/static_cast<double>(RAND_MAX)-1;
sizeSquared = x*x + y*y;
}
while
( sizeSquared >= 1.0);
result = x*sqrt(-2*log(sizeSquared)/sizeSquared);
return result;
}