-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRandomGenerator.cpp
executable file
·113 lines (86 loc) · 3.09 KB
/
RandomGenerator.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
// ****************************************************************************
// NOTICE
//
// This work was produced for the U.S. Government under Contract 693KA8-22-C-00001
// and is subject to Federal Aviation Administration Acquisition Management System
// Clause 3.5-13, Rights In Data-General, Alt. III and Alt. IV (Oct. 1996).
//
// The contents of this document reflect the views of the author and The MITRE
// Corporation and do not necessarily reflect the views of the Federal Aviation
// Administration (FAA) or the Department of Transportation (DOT). Neither the FAA
// nor the DOT makes any warranty or guarantee, expressed or implied, concerning
// the content or accuracy of these views.
//
// For further information, please contact The MITRE Corporation, Contracts Management
// Office, 7515 Colshire Drive, McLean, VA 22102-7539, (703) 983-6000.
//
// 2022 The MITRE Corporation. All Rights Reserved.
// ****************************************************************************
#include "math/RandomGenerator.h"
#include <math.h>
#include <time.h>
#include <assert.h>
#include "utility/constants.h"
const double RandomGenerator::m_IA = 16807;
const double RandomGenerator::m_IM = 2147483647;
const double RandomGenerator::m_AM = 1.0 / m_IM;
const double RandomGenerator::m_IQ = 127773.0;
const double RandomGenerator::m_IR = 2836.0;
log4cplus::Logger RandomGenerator::m_logger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("RandomGenerator"));
RandomGenerator::RandomGenerator() {
double seed = time(NULL);
long k = seed / m_IM;
m_seed = seed - (k * m_IM);
}
RandomGenerator::RandomGenerator(const double seed) {
assert(seed != 0.0);
long k = seed / m_IM;
m_seed = seed - (k * m_IM);
}
RandomGenerator::~RandomGenerator() {}
const double RandomGenerator::UniformSample() {
long k = m_seed / m_IQ;
m_seed = m_IA * (m_seed - k * m_IQ) - m_IR * k;
if (m_seed < 0.0) {
m_seed += m_IM;
}
double sample = m_AM * m_seed;
LOG4CPLUS_TRACE(m_logger, m_seed << "," << sample);
return sample;
}
const double RandomGenerator::GaussianSample() {
double u1 = UniformSample();
double u2 = UniformSample();
double eln = -2.0 * log(u1);
double ang = 2.0 * M_PI * u2;
double v1 = sqrt(eln) * cos(ang);
LOG4CPLUS_TRACE(m_logger, m_seed);
return v1;
}
const double RandomGenerator::TruncatedGaussianSample(const double max_standard_deviation) {
double val = max_standard_deviation + 1.0;
while (val > (max_standard_deviation) || val < (-max_standard_deviation)) {
val = GaussianSample();
}
LOG4CPLUS_TRACE(m_logger, m_seed);
return val;
}
const double RandomGenerator::RayleighSample() {
double u1 = UniformSample();
double v1 = (sqrt(-2.0 * log(u1)) - 1.253) / sqrt(0.429);
return v1;
}
const double RandomGenerator::LaplaceSample() {
double uni = UniformSample();
double err = -log(uni);
uni = UniformSample();
if (uni < 0.5) {
err = -err;
}
return err;
}
void RandomGenerator::SetSeed(const double seed) {
assert(seed != 0.0);
m_seed = seed;
}
const double RandomGenerator::GetSeed(void) { return m_seed; }