Theoretica
A C++ numerical and automatic mathematical library
Loading...
Searching...
No Matches
prng.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_PRNG_H
7#define THEORETICA_PRNG_H
8
9#include <vector>
10#include "./pseudorandom.h"
11#include "../core/error.h"
12
13
14namespace theoretica {
15
16
19 class PRNG {
20 private:
21
25
27 uint64_t x;
28
30 std::vector<uint64_t> param;
31
32 public:
33
38 const std::vector<uint64_t>& s) : f(p), x(seed), param(s) {}
39
40
44
45
49 *this = PRNG::xoshiro(seed);
50 }
51
52
54 inline void seed(uint64_t seed) {
55 x = seed;
56 }
57
58
60 inline uint64_t next() {
61 return x = f(x, param);
62 }
63
64
68 return next();
69 }
70
71
74 inline void discard(uint64_t n) {
75 for (uint64_t i = 0; i < n; ++i)
76 next();
77 }
78
79
81 inline uint64_t last() const {
82 return x;
83 }
84
85
88 f = p;
89 }
90
93 return f;
94 }
95
96
98 inline void set_param(const std::vector<uint64_t>& v) {
99 param = v;
100 }
101
103 inline void set_param(unsigned int i, uint64_t value) {
104 param[i] = value;
105 }
106
108 inline std::vector<uint64_t> get_param() const {
109 return param;
110 }
111
112
115 n = next();
116 return *this;
117 }
118
119
124
125 if(seed == 0)
126 seed = 1;
127
128 return PRNG(
130 {48271, 0, ((uint64_t) 1 << 31) - 1}
131 );
132 }
133
134
138 inline static PRNG xoshiro(const std::vector<uint64_t>& p) {
139 return PRNG(randgen_xoshiro, 0, p);
140 }
141
142
149 inline static PRNG xoshiro(uint64_t seed = 1) {
150
151 if(seed == 0)
152 seed = 1;
153
158
159 return PRNG(randgen_xoshiro, 0, {n1, n2, n3, n4});
160 }
161
162
166 inline static PRNG splitmix64(uint64_t seed = 1) {
167
168 if(seed == 0)
169 seed = 1;
170
172 }
173
174
176 inline static PRNG wyrand(uint64_t seed = 1,
177 uint64_t p1 = 2549536629329, uint64_t p2 = 136137137) {
178
179 if(seed == 0)
180 seed = 1;
181
182 if(p2 == 0)
184
185 return PRNG(randgen_wyrand, 0, {seed, p1, p2});
186 }
187
188
190 inline static PRNG middlesquare(
191 uint64_t seed, uint64_t offset = 765872292751861) {
192
193 if(seed == 0)
194 seed = randgen_splitmix64(765872292751861);
195
196 if(offset == 0)
197 offset = 765872292751861;
198
200 }
201
202 };
203
204
210 template<typename Vector>
211 inline void shuffle(Vector& v, PRNG& g, unsigned int rounds = 0) {
212
213 if(v.size() == 0)
214 TH_MATH_ERROR("shuffle", v.size(), INVALID_ARGUMENT);
215
216 // The number of rounds defaults to (N - 1)^2
217 if(rounds == 0)
218 rounds = square(v.size() - 1);
219
220 for (unsigned int i = 0; i < rounds; ++i) {
221
222 // Generate two random indices
223 const size_t index1 = g() % v.size();
224 const size_t index2 = g() % v.size();
225
226 // Exchange the two values at the random indices
227 const auto x1 = v[index1];
228 v[index1] = v[index2];
229 v[index2] = x1;
230 }
231 }
232
233}
234
235#endif
A pseudorandom number generator.
Definition prng.h:19
static PRNG middlesquare(uint64_t seed, uint64_t offset=765872292751861)
Returns a Middle-square generator.
Definition prng.h:190
uint64_t last() const
Return the last generated number.
Definition prng.h:81
static PRNG linear_congruential(uint64_t seed=1)
Returns a standard linear congruential generator.
Definition prng.h:123
void set_param(const std::vector< uint64_t > &v)
Set the generator's parameters.
Definition prng.h:98
void seed(uint64_t seed)
Seed the PRNG.
Definition prng.h:54
uint64_t operator()()
Generate a pseudorandom number.
Definition prng.h:67
PRNG(pseudorandom_function p, uint64_t seed)
Construct a PRNG with the given generating algorithm and seed.
Definition prng.h:43
static PRNG wyrand(uint64_t seed=1, uint64_t p1=2549536629329, uint64_t p2=136137137)
Returns a Wyrand generator.
Definition prng.h:176
void discard(uint64_t n)
Discard n numbers from the generator.
Definition prng.h:74
pseudorandom_function get_function() const
Get the generating function.
Definition prng.h:92
static PRNG xoshiro(uint64_t seed=1)
Returns a Xoshiro256++ generator.
Definition prng.h:149
std::vector< uint64_t > get_param() const
Get the generator's parameters.
Definition prng.h:108
static PRNG xoshiro(const std::vector< uint64_t > &p)
Returns a Xoshiro256++ generator.
Definition prng.h:138
static PRNG splitmix64(uint64_t seed=1)
Returns a Splitmix64 generator.
Definition prng.h:166
PRNG & operator>>(uint64_t &n)
Stream the next generated number.
Definition prng.h:114
void set_function(pseudorandom_function p)
Set the generating function.
Definition prng.h:87
void set_param(unsigned int i, uint64_t value)
Set a specific parameter by index.
Definition prng.h:103
PRNG(uint64_t seed)
Construct a PRNG with the default generator and the given seed.
Definition prng.h:48
uint64_t next()
Generate a pseudorandom number.
Definition prng.h:60
PRNG(pseudorandom_function p, uint64_t seed, const std::vector< uint64_t > &s)
Construct a PRNG with the given generating algorithm p, seed x and parameters s.
Definition prng.h:36
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition error.h:225
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
uint64_t randgen_xoshiro(uint64_t &a, uint64_t &b, uint64_t &c, uint64_t &d)
Generate a pseudorandom natural number using the xoshiro256++ pseudorandom number generation algorith...
Definition pseudorandom.h:86
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition core_traits.h:134
void shuffle(Vector &v, PRNG &g, unsigned int rounds=0)
Shuffle a set by exchanging random couples of elements.
Definition prng.h:211
uint64_t randgen_splitmix64(uint64_t x)
Generate a pseudorandom natural number using the SplitMix64 pseudorandom number generation algorithm.
Definition pseudorandom.h:130
uint64_t(*)(uint64_t, std::vector< uint64_t > &) pseudorandom_function
A function pointer which wraps a pseudorandom generator, taking as input the previous generated value...
Definition pseudorandom.h:23
uint64_t randgen_middlesquare(uint64_t seed, uint64_t offset=765872292751861)
Generate a pseudorandom natural number using the middle-square pseudorandom number generation algorit...
Definition pseudorandom.h:194
uint64_t randgen_congruential(uint64_t x, uint64_t a=48271, uint64_t c=0, uint64_t m=((uint64_t) 1<< 31) - 1)
Generate a pseudorandom natural number using the congruential pseudorandom number generation algorith...
Definition pseudorandom.h:39
uint64_t randgen_wyrand(uint64_t &seed, uint64_t p1, uint64_t p2)
Generate a pseudorandom natural number using the Wyrand pseudorandom number generation,...
Definition pseudorandom.h:160
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition dual2_functions.h:23
Pseudorandom number generation algorithms.