-
Notifications
You must be signed in to change notification settings - Fork 2
/
RandomNumbers.h
230 lines (192 loc) · 5.71 KB
/
RandomNumbers.h
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// Created by Ryan.Zurrin001 on 12/16/2021.
//
#ifndef PHYSICSFORMULA_RANDOMNUMBERS_H
#define PHYSICSFORMULA_RANDOMNUMBERS_H
#include <functional>
#include <iomanip>
#include <random>
#include <iostream>
#include <algorithm>
#include <random>
using namespace std;
//#include <map>
class RandomNumbers
{
int min;
int max;
double mean;
double std;
std::random_device rd{};
public:
RandomNumbers();
RandomNumbers(int min_Mean, int max_Std, string mode = "ud");
std::mt19937 mt_gen;//32 bit mersenne twister engine with 19,937 bits
std::uniform_int_distribution<int> ud;//uniform distribution
std::normal_distribution<double> nd;
static void generate_and_print_distribution(
std::function<int(void)> gen, int const iterations = 10000);
static auto generate_random_integer(int lb, int ub);
static auto generate_normDis_double(double mean_, double std_);
static auto generate_random_double(double lb, double ub);
double return_mean()const;
double return_std()const;
int return_min()const;
int return_max()const;
void set_min(int min_);
void set_max(int max_);
void set_mean(double mean_);
void set_std(double std_);
~RandomNumbers();
};
#endif //PHYSICSFORMULA_RANDOMNUMBERS_H
inline RandomNumbers::RandomNumbers()
{
min = 1;
max = 6;
mean = 5.0;
std = 2.0;
mt_gen = std::mt19937{ rd() };
ud = std::uniform_int_distribution<>{ min, max };
nd = std::normal_distribution<double>{ mean, std };
}
inline RandomNumbers::RandomNumbers(int min_Mean, int max_Std, string mode)
{
if (mode == "ud")
{
min = min_Mean;
max = max_Std;
mean = 5.0;
std = 2.0;
}
else
{
min = 1;
max = 6;
mean = min_Mean;
std = max_Std;
}
mt_gen = std::mt19937{ rd() };
ud = std::uniform_int_distribution<>{ min, max };
nd = std::normal_distribution<double>{ static_cast<double>(min), static_cast<double>(max) };
}
inline void RandomNumbers::generate_and_print_distribution(
std::function<int(void)> gen, int const iterations)
{
// map to store the numbers and their repetitions
auto data = std::map<int, int>{};
for (auto n = 0; n < iterations; ++n)
++data[gen()];
auto max = std::max_element(
std::begin(data), std::end(data),
[](auto kvp1, auto kvp2){
return kvp1.second < kvp2.second;});
// print the bars
for (auto i = max->second / 200; i > 0; --i)
{
for (auto kvp : data)
{
std::cout
<< std::fixed << std::setprecision(1) << std::setw(3)
<< (kvp.second / 200 >= i ? (char)219 : ' ');
}
std::cout << '\n';
}
//print the numbers
for (auto kvp : data)
{
std::cout
<< std::fixed << std::setprecision(1) << std::setw(3)
<< kvp.first;
}
std::cout << '\n';
}
inline auto RandomNumbers::generate_random_integer(int lb, int ub)
{
std::random_device rd{};
std::array<int, std::mt19937::state_size> seed_data{};
std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd));
std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
auto eng = std::mt19937{ seq };
const auto randInt = std::uniform_int_distribution<>{ lb,ub };
return randInt;
}
inline auto RandomNumbers::generate_random_double(double lb, double ub)
{
std::random_device rd{};
std::array<double, std::mt19937::state_size> seed_data{};
std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd));
std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
auto eng = std::mt19937{ seq };
const auto randDouble = std::uniform_real_distribution<>{ lb,ub };
return randDouble;
}
inline auto RandomNumbers::generate_normDis_double(double mean_, double std_)
{
std::random_device rd{};
std::array<double, std::mt19937::state_size> seed_data{};
std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd));
std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
auto eng = std::mt19937{ seq };
auto randNormDist = std::normal_distribution<>{ mean_,std_ };
return randNormDist(eng);
}
inline double RandomNumbers::return_mean() const
{
return mean;
}
inline double RandomNumbers::return_std() const
{
return std;
}
inline int RandomNumbers::return_min()const
{
return min;
}
inline int RandomNumbers::return_max()const
{
return max;
}
inline void RandomNumbers::set_min(int min_)
{
min = min_;
ud = std::uniform_int_distribution<>{ min, max };
}
inline void RandomNumbers::set_max(int max_)
{
max = max_;
ud = std::uniform_int_distribution<>{ min, max };
}
inline void RandomNumbers::set_mean(double mean_)
{
mean = mean_;
nd = std::normal_distribution<double>{ mean, std };
}
inline void RandomNumbers::set_std(double std_)
{
std = std_;
nd = std::normal_distribution<double>{ mean, std };
}
inline RandomNumbers::~RandomNumbers() = default;
/*
*
* RandomNumbers r{};
auto mtgen = r.mt_gen;
auto u_d = r.ud;
//auto n_d = r.nd;
r.set_mean(3.0);
auto n_d = r.nd;
r.generate_and_print_distribution([&mtgen, &n_d]()
{
return static_cast<int>(std::round(n_d(mtgen)));
});
for (size_t i = 0; i < 100; i++)
{
std::cout << r.generate_random_integer(5, 100) << " ";
}
std::cout << std::endl<<std::endl;
for (size_t i = 0; i < 100; i++)
{
std::cout << r.generate_random_double(1.0, 25.0) << " ";
}
*/