-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.h
More file actions
299 lines (255 loc) · 10.3 KB
/
Random.h
File metadata and controls
299 lines (255 loc) · 10.3 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#ifndef RANDOM_H
#define RANDOM_H
#include "Shape.h"
#include "Algorithm.h"
#include <random>
#include <limits>
#include <vector>
#include <algorithm>
#include <functional>
#include <numbers>
class Random {
public:
template<typename T>
class WeightedContainer {
public:
void PushBack(T&& value, const double& weight)
{
values_.push_back(std::move(value));
weights_.push_back(weight);
distribution_ = std::discrete_distribution<size_t>(std::begin(weights_), std::end(weights_));
}
const T& RandomItem()
{
auto index = Random::Generate(distribution_);
return values_.at(index);
}
private:
std::vector<T> values_;
std::vector<double> weights_;
std::discrete_distribution<size_t> distribution_;
};
static void Seed(const std::mt19937::result_type& seed)
{
entropy_.seed(seed);
}
static double Bearing()
{
return Random::Number(0.0, std::numbers::pi * 2.0);
}
static bool Boolean()
{
std::bernoulli_distribution d;
return Generate(d);
}
template<typename NumericType>
static NumericType Sign(const NumericType& value)
{
return Boolean() ? value : -value;
}
static Point PointIn(const Line& line)
{
double proportion = Random::Proportion();
double deltaX = (line.b.x - line.a.x) * proportion;
double deltaY = (line.b.y - line.a.y) * proportion;
return { line.a.x + deltaX, line.a.y + deltaY };
}
static Point PointIn(const Rect& rect)
{
return { Random::Number(util::Range(rect.left, rect.right)), Random::Number(util::Range(rect.bottom, rect.top)) };
}
static Point PointIn(const Circle& circle)
{
// TODO check to see if while (!RandomPointInSquare.isInCircle()) performs better
double rotation = Random::Number(0.0, std::numbers::pi * 2.0);
double distance = std::max(Random::Number(0.0, circle.radius), Random::Number(0.0, circle.radius));
double x = circle.x + distance * std::cos(rotation);
double y = circle.y + distance * std::sin(rotation);
return { x, y };
}
template<typename NumericType>
requires std::is_integral_v<NumericType>
static NumericType Number(NumericType min, NumericType max)
{
std::uniform_int_distribution<NumericType> distribution(min, max);
return Generate(distribution);
}
template<typename NumericType>
requires std::is_floating_point_v<NumericType>
static NumericType Number(NumericType min, NumericType max)
{
std::uniform_real_distribution<NumericType> distribution(min, std::nextafter(max, std::numeric_limits<NumericType>::max()));
return Generate(distribution);
}
template<typename NumericType>
static NumericType Number(const util::Range<NumericType>& range)
{
return Number<NumericType>(range.Min(), range.Max());
}
static size_t WeightedIndex(std::initializer_list<double>&& weights)
{
std::discrete_distribution<size_t> d(std::move(weights));
return Generate(d);
}
static double Proportion()
{
return Number(0.0, 1.0);
}
static double Percent()
{
return Number(0.0, 100.0);
}
static bool PercentChance(double chance)
{
std::bernoulli_distribution d(std::clamp(chance / 100.0, 0.0, 1.0));
return Generate(d);
}
/**
* Instead of rounding n.0 to n.49... down and n.5 to n.9... up, this
* function uses the value after the decimal as the propotional chance the
* value will be rounded up. e.g. 0.1 has a 10% chance of being rounded up
* to 1, whereas 0.9 has a 90% chance of being rounded up.
*/
static uint64_t Round(const double& v)
{
return static_cast<uint64_t>(v) + (Random::PercentChance(std::fmod(v, 1.0) * 100) ? 1 : 0);
}
template<typename NumericType>
static NumericType Gaussian(NumericType mean = std::numeric_limits<NumericType>::min(), NumericType standardDeviation = NumericType{ 1.0 })
{
std::normal_distribution<NumericType> distribution(mean, std::abs(standardDeviation));
return Generate(distribution);
}
template<typename NumericType>
static NumericType Poisson(NumericType mean = std::numeric_limits<NumericType>::min())
{
std::poisson_distribution<NumericType> distribution(mean);
return Generate(distribution);
}
template<typename NumericType>
requires std::is_integral_v<NumericType>
static std::vector<NumericType> Numbers(typename std::vector<NumericType>::size_type count, NumericType min, NumericType max)
{
std::vector<NumericType> rands;
rands.reserve(count);
std::uniform_int_distribution<NumericType> distribution(min, max);
std::generate_n(std::back_inserter(rands), count, [&](){ return Generate(distribution); });
return rands;
}
template<typename NumericType>
requires std::is_floating_point_v<NumericType>
static std::vector<NumericType> Numbers(typename std::vector<NumericType>::size_type count, NumericType min, NumericType max)
{
std::vector<NumericType> rands;
rands.reserve(count);
std::uniform_real_distribution<NumericType> distribution(min, max);
std::generate_n(std::back_inserter(rands), count, [&](){ return Generate(distribution); });
return rands;
}
template<typename NumericType>
static std::vector<NumericType> Gaussians(typename std::vector<NumericType>::size_type count, NumericType mean = std::numeric_limits<NumericType>::min(), NumericType standardDeviation = NumericType{ 1.0 })
{
std::normal_distribution<NumericType> distribution(mean, standardDeviation);
std::vector<NumericType> rands;
rands.reserve(count);
std::generate_n(std::back_inserter(rands), count, [&](){ return Generate(distribution); });
return rands;
}
template<typename NumericType>
static std::vector<NumericType> DualPeakGaussians(typename std::vector<NumericType>::size_type count, NumericType meanPeakOne, NumericType standardDeviationPeakOne, NumericType meanPeakTwo, NumericType standardDeviationPeakTwo)
{
std::normal_distribution<NumericType> distributionOne(meanPeakOne, std::abs(standardDeviationPeakOne));
std::normal_distribution<NumericType> distributionTwo(meanPeakTwo, std::abs(standardDeviationPeakTwo));
std::vector<NumericType> rands;
rands.reserve(count);
std::generate_n(std::back_inserter(rands), count, [&](){ return Random::Boolean() ? Generate(distributionOne) : Generate(distributionTwo); });
return rands;
}
template<typename NumericType>
static std::vector<NumericType> Poissons(typename std::vector<NumericType>::size_type count, NumericType mean = std::numeric_limits<NumericType>::min())
{
std::poisson_distribution<NumericType> distribution(mean);
std::vector<NumericType> rands;
rands.reserve(count);
std::generate_n(std::back_inserter(rands), count, [&](){ return Generate(distribution); });
return rands;
}
template<typename NumericType>
static NumericType GaussianAdjustment(const NumericType& toAdjust, double proportion)
{
return toAdjust + Gaussian(0.0, std::max(0.001, std::abs(toAdjust) * (proportion / 3.0)));
}
template<typename Container>
static void Shuffle(Container& toShuffle)
{
std::shuffle(std::begin(toShuffle), std::end(toShuffle), entropy_);
}
/**
* Returns a Container with max(a.size, b.size) items. For each index it
* will contain a copy of either a[index] or b[index]. Once the max index of
* the shortest container has been reached, all remaining items are copied
* from the longer container.
*/
template<typename Container>
static Container Merge(const Container& a, const Container& b)
{
Container c;
c.reserve(std::max(a.size(), b.size()));
util::IterateBoth(a, b, [&c](const auto& a, const auto& b)
{
c.push_back(Boolean() ? a : b);
});
if (a.size() > b.size()) {
auto remainingItemsIter = a.cbegin();
std::advance(remainingItemsIter, a.size() - b.size());
std::copy(remainingItemsIter, std::cend(a), std::back_inserter(c));
} else if (b.size() > a.size()) {
auto remainingItemsIter = b.cbegin();
std::advance(remainingItemsIter, b.size() - a.size());
std::copy(remainingItemsIter, std::cend(b), std::back_inserter(c));
}
return c;
}
template<typename Container>
static typename Container::value_type& Item(Container& container)
{
assert(!container.empty());
auto iter = std::begin(container);
std::advance(iter, Random::Number(typename Container::size_type{ 0 }, container.size() - 1));
return *iter;
}
template<typename Container>
static const typename Container::value_type& Item(const Container& container)
{
assert(!container.empty());
auto iter = std::cbegin(container);
std::advance(iter, Random::Number(typename Container::size_type{ 0 }, container.size() - 1));
return *iter;
}
template<typename Container, typename Action>
requires std::is_invocable_v<Action, typename Container::value_type&>
static void ForNItems(Container& container, size_t itemCount, const Action& action)
{
assert(!container.empty());
for (size_t count = 0; count < itemCount; ++count) {
std::invoke(action, Item(container));
}
}
template<typename Container, typename Action>
requires std::is_invocable_v<Action, const typename Container::value_type&>
static void ForNItems(const Container& container, size_t itemCount, const Action& action)
{
assert(!container.empty());
for (size_t count = 0; count < itemCount; ++count) {
std::invoke(action, Item(container));
}
}
private:
inline static std::mt19937 entropy_ = std::mt19937();
template<typename DistributionType>
static typename DistributionType::result_type Generate(DistributionType& distribution)
{
return distribution(entropy_);
}
};
#endif // RANDOM_H