Chebyshev
Unit testing for scientific software
Loading...
Searching...
No Matches
random.h
Go to the documentation of this file.
1
5
6#ifndef CHEBYSHEV_RANDOM_H
7#define CHEBYSHEV_RANDOM_H
8
9#include <cstdint>
10#include <cstdlib>
11#include <cmath>
12#include <mutex>
13
14
15#include "../core/common.h"
16
17
21#ifndef CH_CUSTOM_RND
22
23#include <random>
24using random_engine = std::mt19937;
25
26#endif
27
28
29namespace chebyshev {
30
33namespace random {
34
35
40
41 private:
42
44 random_engine rnd;
45
47 uint64_t seed {0};
48
49 public:
50
53 this->seed = seed;
54 rnd = random_engine(seed);
55 }
56
57
60 return seed;
61 }
62
63
65 inline uint64_t natural() {
66 return rnd();
67 }
68
69
76 inline real_t uniform(real_t a, real_t b) {
77
78 const uint64_t max = uint64_t(1) << 31;
79 const real_t u = (natural() % max) / prec_t(max);
80
81 return u * (b - a) + a;
82 }
83
84
92 template<typename Vector>
93 inline Vector& uniform(Vector& x, real_t a, real_t b) {
94
95 for (auto& x_i : x)
96 x_i = uniform(a, b);
97
98 return x;
99 }
100
101
108 template<typename Vector>
109 inline Vector& uniform(Vector& x, const std::vector<prec::interval> intervals) {
110
111 if(x.size() != intervals.size()) {
112 throw std::runtime_error(
113 "Vector and domain size mismatch in chebyshev::uniform"
114 );
115 }
116
117 for (unsigned int i = 0; i < x.size(); ++i)
118 x[i] = uniform(intervals[i].a, intervals[i].b);
119
120 return x;
121 }
122
123
130
131 const real_t x = uniform(0.0, 1.0);
132 const real_t y = uniform(0.0, 1.0);
133
134 const real_t v = std::sqrt(-2 * std::log(x));
135 const real_t u = v * std::cos(2 * PI_CONST * y);
136
137 return mean + sigma * u;
138 }
139
140
148 template<typename Vector>
150
151 for (auto& x_i : x)
153
154 return x;
155 }
156
157
164 inline std::string string(size_t length) {
165
166 std::string str;
167 str.resize(length);
168
169 for (unsigned int i = 0; i < length; ++i)
170 str[i] = '!' + (char) (natural() % 95);
171
172 return str;
173 }
174
175
183 inline std::string string(size_t length, std::string alphabet) {
184
185 if(!alphabet.size()) {
186 throw std::runtime_error(
187 "alphabet in chebyshev::random::string cannot be empty."
188 );
189 }
190
191 std::string str;
192 str.resize(length);
193
194 for (unsigned int i = 0; i < length; ++i)
195 str[i] = alphabet[natural() % alphabet.size()];
196
197 return str;
198 }
199
200
208 inline std::string string(size_t length, std::vector<char> alphabet) {
209
210 if(!alphabet.size()) {
211 throw std::runtime_error(
212 "alphabet in chebyshev::random::string cannot be empty."
213 );
214 }
215
216 std::string str;
217 str.resize(length);
218
219 for (unsigned int i = 0; i < length; ++i)
220 str[i] = alphabet[natural() % alphabet.size()];
221
222 return str;
223 }
224
225
232 template<typename T>
233 inline std::vector<T> string(size_t length, std::vector<T> alphabet) {
234
235 if(!alphabet.size()) {
236 throw std::runtime_error(
237 "alphabet in chebyshev::random::string cannot be empty."
238 );
239 }
240
241 std::vector<T> str;
242 str.resize(length);
243
244 for (unsigned int i = 0; i < length; ++i)
245 str[i] = alphabet[natural() % alphabet.size()];
246
247 return str;
248 }
249
250 };
251
252
255 struct random_settings { };
256
257
260 private:
261
263 random_engine rnd;
264
266 std::mutex rndMutex;
267
268 public:
269
272
276 inline void setup(uint64_t seed = 0) {
277
278 if(seed == 0) {
279 std::random_device entropy;
280 seed = entropy();
281 }
282
283 // Initialize the random source
284 rnd = random_engine(seed);
285 }
286
287
289 inline void terminate() { }
290
291
294 setup(seed);
295 }
296
299
300 rnd = other.rnd;
301 settings = other.settings;
302 }
303
304
307
308 rnd = other.rnd;
309 settings = other.settings;
310 return *this;
311 }
312
313
317
318 std::lock_guard<std::mutex> lock(rndMutex);
319 return random_source(rnd());
320 }
321
322 };
323
324}}
325
326#endif
context.
Definition random.h:259
random_context & operator=(const random_context &other)
Custom assignment operator to avoid copying std::mutex.
Definition random.h:306
void terminate()
Terminate the random module (currently empty).
Definition random.h:289
random_context(uint64_t seed=0)
Initialize the random module with the given seed.
Definition random.h:293
random_settings settings
Settings for the random context.
Definition random.h:271
random_source get_rnd()
Instantiate a new random source, automatically seeded with a new random seed.
Definition random.h:316
void setup(uint64_t seed=0)
Initialize the random module with the given seed.
Definition random.h:276
random_context(const random_context &other)
Custom copy constructor to avoid copying std::mutex.
Definition random.h:298
long double prec_t
Floating-point type of higher precision, used in computations, such as error estimation.
Definition common.h:42
double real_t
Floating-point type, used as default for function arguments.
Definition common.h:37
General namespace of the framework.
Definition benchmark.h:22
constexpr FloatType get_nan()
Get a quiet NaN of the specified floating point type.
Definition common.h:65
const prec_t PI_CONST
The Pi mathematical constant.
Definition common.h:71
std::mt19937 random_engine
Define the CH_CUSTOM_RND macro and a type random_engine to use a custom random number generator.
Definition random.h:24
Settings for the random module.
Definition random.h:255
A source of pseudorandom numbers.
Definition random.h:39
uint64_t get_seed()
Get the seed used to generate the random source.
Definition random.h:59
Vector & uniform(Vector &x, real_t a, real_t b)
Fill an already allocated vector with uniformly distributed numbers over the same interval.
Definition random.h:93
Vector & gaussian(Vector &x, real_t mean, real_t sigma)
Fill an already allocated vector with Gaussian distributed numbers with the same mean and standard de...
Definition random.h:149
random_source(uint64_t seed)
Construct a random engine with the given seed.
Definition random.h:52
std::string string(size_t length, std::vector< char > alphabet)
Generate a random string made of the elements of the given alphabet.
Definition random.h:208
std::string string(size_t length, std::string alphabet)
Generate a random string made of the elements of the given alphabet.
Definition random.h:183
real_t gaussian(real_t mean, real_t sigma)
Generate a Gaussian distributed random number.
Definition random.h:129
Vector & uniform(Vector &x, const std::vector< prec::interval > intervals)
Fill an already allocated vector with uniformly distributed numbers over different intervals.
Definition random.h:109
std::string string(size_t length)
Generate a random string made of human-readable ASCII characters.
Definition random.h:164
real_t uniform(real_t a, real_t b)
Generate a uniformly distributed random number.
Definition random.h:76
uint64_t natural()
Generate a random natural number.
Definition random.h:65
std::vector< T > string(size_t length, std::vector< T > alphabet)
Generate a random string made of the elements of the given alphabet, of arbitrary type.
Definition random.h:233