-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutility.cpp
106 lines (88 loc) · 2.53 KB
/
utility.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
#include "utility.h"
#include <iostream>
#include <random>
namespace wildland_firesim {
namespace utility {
class RandomNumberGenerator {
public:
static RandomNumberGenerator *getInstance()
{
if(!s_instance){
s_instance = new RandomNumberGenerator;
std::atexit([]() { delete s_instance; });
}
return s_instance;
}
float random()
{
return m_distribution(m_engine);
}
int random(int top)
{
auto distribution = std::uniform_int_distribution<int>{0, top};
return distribution(m_engine);
}
float random(float min, float max)
{
auto distribution = std::uniform_real_distribution<float>{min, max};
return distribution(m_engine);
}
float normal_random(float mean, float sd)
{
auto distribution = std::normal_distribution<float>{mean, sd};
return distribution(m_engine);
}
float weibull_random(float shape, float form)
{
auto distribution = std::weibull_distribution<float>{shape, form};
return distribution(m_engine);
}
private:
int fixedSeed = 42; //fixed seed for sensitivity analysis
static RandomNumberGenerator *s_instance;
//RandomNumberGenerator() : m_engine{rd()}, m_distribution{0.f, 1.f} {}
RandomNumberGenerator() : m_engine{fixedSeed}, m_distribution{0.f, 1.f} {}
std::random_device rd;
std::mt19937 m_engine;
std::uniform_real_distribution<float> m_distribution;
std::normal_distribution<float> m_normalDistribution;
std::weibull_distribution<float> m_weibullDistribution;
};
RandomNumberGenerator *RandomNumberGenerator::s_instance;
void _assert(bool condition, const char *message)
{
if (!condition) {
std::cerr << message << std::endl;
std::terminate();
}
}
float random()
{
return RandomNumberGenerator::getInstance()->random();
}
int random(int top)
{
return RandomNumberGenerator::getInstance()->random(top);
}
float random(float min, float max)
{
return RandomNumberGenerator::getInstance()->random(min, max);
}
float normal_random(float mean, float sd)
{
return RandomNumberGenerator::getInstance()->normal_random(mean, sd);
}
float weibull_random(float scale, float form)
{
return RandomNumberGenerator::getInstance()->weibull_random(scale, form);
}
float asFloat(const std::string &str)
{
return static_cast<float>(std::atof(str.c_str()));
}
int asInteger(const std::string &str)
{
return (std::atoi(str.c_str()));
}
} // namespace utility
} // namespace wildland_firesim