6 #ifndef THEORETICA_PSEUDORANDOM_H
7 #define THEORETICA_PSEUDORANDOM_H
12 #include "../core/bit_op.h"
13 #include "../core/error.h"
43 uint64_t m = ((uint64_t) 1 << 31) - 1) {
45 return (a * x + c) % m;
59 if(state.size() != 3) {
60 TH_MATH_ERROR(
"rand_congruential", state.size(), INVALID_ARGUMENT);
64 const uint64_t a = state[0];
65 const uint64_t c = state[1];
66 const uint64_t m = state[2];
86 inline uint64_t
rand_xoshiro(uint64_t& a, uint64_t& b, uint64_t& c, uint64_t& d) {
89 const uint64_t result =
bit_rotate(a + d, 23) + a;
90 const uint64_t temp = b << 17;
112 inline uint64_t
rand_xoshiro(uint64_t x, std::vector<uint64_t>& state) {
114 if(state.size() != 4) {
115 TH_MATH_ERROR(
"rand_xoshiro", state.size(), INVALID_ARGUMENT);
119 return rand_xoshiro(state[0], state[1], state[2], state[3]);
132 x += 0x9e3779b97f4a7c15;
135 res = (res ^ (res >> 30)) * 0xbf58476d1ce4e5b9;
136 res = (res ^ (res >> 27)) * 0x94d049bb133111eb;
138 return res ^ (res >> 31);
160 inline uint64_t
rand_wyrand(uint64_t& seed, uint64_t p1, uint64_t p2) {
162 return mix_mum(seed ^ p2, seed);
175 inline uint64_t
rand_wyrand(uint64_t x, std::vector<uint64_t>& p) {
200 return (high << 32) | (low >> 32);
213 TH_MATH_ERROR(
"rand_middlesquare", p.size(), INVALID_ARGUMENT);
#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:219
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
uint64_t rand_splitmix64(uint64_t x)
Generate a pseudorandom number using the SplitMix64 pseudorandom number generation algorithm.
Definition: pseudorandom.h:130
TH_CONSTEXPR UnsignedIntType bit_rotate(UnsignedIntType x, unsigned int i)
Bit rotation of unsigned integer types using shifts.
Definition: bit_op.h:78
uint64_t rand_wyrand(uint64_t &seed, uint64_t p1, uint64_t p2)
Generate a pseudorandom number using the Wyrand pseudorandom number generation, as invented by Yi Wan...
Definition: pseudorandom.h:160
uint64_t mix_mum(uint64_t a, uint64_t b)
MUM bit mixing function, computes the 128-bit product of a and b and the XOR of their high and low 64...
Definition: bit_op.h:62
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition: dataset.h:330
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 rand_congruential(uint64_t x, uint64_t a=48271, uint64_t c=0, uint64_t m=((uint64_t) 1<< 31) - 1)
Generate a pseudorandom number using the congruential pseudorandom number generation algorithm.
Definition: pseudorandom.h:39
uint64_t rand_middlesquare(uint64_t seed, uint64_t offset=765872292751861)
Generate a pseudorandom number using the middle-square pseudorandom number generation algorithm.
Definition: pseudorandom.h:194
void mul_uint128(uint64_t a, uint64_t b, uint64_t &c_low, uint64_t &c_high)
Multiply two 64-bit unsigned integers and store the result in two 64-bit variables,...
Definition: bit_op.h:24
uint64_t rand_xoshiro(uint64_t &a, uint64_t &b, uint64_t &c, uint64_t &d)
Generate a pseudorandom number using the xoshiro256++ pseudorandom number generation algorithm.
Definition: pseudorandom.h:86