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 long double uniform(long double a, long double b) {
77
78 const long double u = natural() / (long double) (uint64_t(1) << 63);
79 return u * (b - a) + a;
80 }
81
82
90 template<typename Vector>
91 inline Vector& uniform(Vector& x, long double a, long double b) {
92
93 for (int i = 0; i < x.size(); ++i)
94 x[i] = uniform(a, b);
95
96 return x;
97 }
98
99
106 template<typename Vector>
107 inline Vector& uniform(Vector& x, const std::vector<prec::interval> intervals) {
108
109 if(x.size() != intervals.size()) {
110 throw std::runtime_error(
111 "Vector and domain size mismatch in chebyshev::uniform"
112 );
113 }
114
115 for (int i = 0; i < x.size(); ++i)
116 x[i] = uniform(intervals[i].a, intervals[i].b);
117
118 return x;
119 }
120
121
127 inline long double gaussian(long double m, long double s) {
128
129 const long double x = uniform(0.0, 1.0);
130 const long double y = uniform(0.0, 1.0);
131
132 const long double v = std::sqrt(-2 * std::log(x));
133 const long double u = v * std::cos(2 * PI_CONST * y);
134
135 return m + s * u;
136 }
137
138
145 inline std::string string(size_t length) {
146
147 std::string str;
148 str.resize(length);
149
150 for (unsigned int i = 0; i < length; ++i)
151 str[i] = '!' + (char) (natural() % 95);
152
153 return str;
154 }
155
156
164 inline std::string string(size_t length, std::string alphabet) {
165
166 if(!alphabet.size()) {
167 throw std::runtime_error(
168 "alphabet in chebyshev::random::string cannot be empty."
169 );
170 }
171
172 std::string str;
173 str.resize(length);
174
175 for (unsigned int i = 0; i < length; ++i)
176 str[i] = alphabet[natural() % alphabet.size()];
177
178 return str;
179 }
180
181
189 inline std::string string(size_t length, std::vector<char> alphabet) {
190
191 if(!alphabet.size()) {
192 throw std::runtime_error(
193 "alphabet in chebyshev::random::string cannot be empty."
194 );
195 }
196
197 std::string str;
198 str.resize(length);
199
200 for (unsigned int i = 0; i < length; ++i)
201 str[i] = alphabet[natural() % alphabet.size()];
202
203 return str;
204 }
205
206
213 template<typename T>
214 inline std::vector<T> string(size_t length, std::vector<T> alphabet) {
215
216 if(!alphabet.size()) {
217 throw std::runtime_error(
218 "alphabet in chebyshev::random::string cannot be empty."
219 );
220 }
221
222 std::vector<T> str;
223 str.resize(length);
224
225 for (unsigned int i = 0; i < length; ++i)
226 str[i] = alphabet[natural() % alphabet.size()];
227
228 return str;
229 }
230
231 };
232
233
236 struct random_settings { };
237
238
241 private:
242
244 random_engine rnd;
245
247 std::mutex rndMutex;
248
249 public:
250
253
257 inline void setup(uint64_t seed = 0) {
258
259 if(seed == 0) {
260 std::random_device entropy;
261 seed = entropy();
262 }
263
264 // Initialize the random source
265 rnd = random_engine(seed);
266 }
267
268
270 inline void terminate() { }
271
272
275 setup(seed);
276 }
277
280
281 rnd = other.rnd;
282 settings = other.settings;
283 }
284
285
288
289 rnd = other.rnd;
290 settings = other.settings;
291 return *this;
292 }
293
294
298
299 std::lock_guard<std::mutex> lock(rndMutex);
300 return random_source(rnd());
301 }
302
303 };
304
305}}
306
307#endif
context.
Definition random.h:240
random_context & operator=(const random_context &other)
Custom assignment operator to avoid copying std::mutex.
Definition random.h:287
void terminate()
Terminate the random module (currently empty).
Definition random.h:270
random_context(uint64_t seed=0)
Initialize the random module with the given seed.
Definition random.h:274
random_settings settings
Settings for the random context.
Definition random.h:252
random_source get_rnd()
Instantiate a new random source, automatically seeded with a new random seed.
Definition random.h:297
void setup(uint64_t seed=0)
Initialize the random module with the given seed.
Definition random.h:257
random_context(const random_context &other)
Custom copy constructor to avoid copying std::mutex.
Definition random.h:279
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 long double 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:236
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
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:189
long double uniform(long double a, long double b)
Generate a uniformly distributed random number.
Definition random.h:76
std::string string(size_t length, std::string alphabet)
Generate a random string made of the elements of the given alphabet.
Definition random.h:164
long double gaussian(long double m, long double s)
Generate a Gaussian distributed random number.
Definition random.h:127
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:107
std::string string(size_t length)
Generate a random string made of human-readable ASCII characters.
Definition random.h:145
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:214
Vector & uniform(Vector &x, long double a, long double b)
Fill an already allocated vector with uniformly distributed numbers over the same interval.
Definition random.h:91