31#ifndef ETL_RANDOM_INCLUDED
32#define ETL_RANDOM_INCLUDED
41#if defined(ETL_POLYMORPHIC_RANDOM)
53 virtual void initialise(uint32_t seed) = 0;
54 virtual uint32_t operator()() = 0;
55 virtual uint32_t range(uint32_t low, uint32_t high) = 0;
92 uintptr_t n =
reinterpret_cast<uintptr_t
>(
this);
93 uint32_t seed =
static_cast<uint32_t
>(n);
116 state[3] = seed + 11;
124 uint32_t n = state[3];
131 n ^= state[0] >> 19U;
140 uint32_t
range(uint32_t low, uint32_t high)
142 uint32_t r = high - low + 1UL;
172 uintptr_t n =
reinterpret_cast<uintptr_t
>(
this);
173 uint32_t seed =
static_cast<uint32_t
>(n);
192 seed = (seed == 0) ? 1 : seed;
193 value = (seed > m) ? m : seed;
201 value = (a * value) % m;
209 uint32_t
range(uint32_t low, uint32_t high)
211 uint32_t r = high - low + 1UL;
221 static ETL_CONSTANT uint32_t a = 40014U;
222 static ETL_CONSTANT uint32_t m = 2147483563UL;
244 uintptr_t n =
reinterpret_cast<uintptr_t
>(
this);
245 uint32_t seed =
static_cast<uint32_t
>(n);
264 seed = (seed == 0) ? 1 : seed;
265 value1 = (seed > m1) ? m1 : seed;
266 value2 = (seed > m1) ? m1 : seed;
274 static ETL_CONSTANT uint32_t m = ((m1 > m2) ? m1 : m2);
276 value1 = (a1 * value1) % m1;
277 value2 = (a2 * value2) % m2;
279 return (value1 + value2) % m;
285 uint32_t
range(uint32_t low, uint32_t high)
287 uint32_t r = high - low + 1UL;
297 static ETL_CONSTANT uint32_t a1 = 40014U;
298 static ETL_CONSTANT uint32_t m1 = 2147483563UL;
300 static ETL_CONSTANT uint32_t a2 = 40692U;
301 static ETL_CONSTANT uint32_t m2 = 2147483399UL;
325 uintptr_t n =
reinterpret_cast<uintptr_t
>(
this);
326 uint32_t seed =
static_cast<uint32_t
>(n);
353 static ETL_CONSTANT uint32_t polynomial = 0x80200003UL;
357 if ((value & 1UL) == 1UL)
368 uint32_t
range(uint32_t low, uint32_t high)
370 uint32_t r = high - low + 1UL;
399 uintptr_t n =
reinterpret_cast<uintptr_t
>(
this);
400 uint32_t seed =
static_cast<uint32_t
>(n);
428 value1 = 36969UL * (value1 & 0xFFFFUL) + (value1 >> 16U);
429 value2 = 18000UL * (value2 & 0xFFFFUL) + (value2 >> 16U);
431 return (value1 << 16U) + value2;
437 uint32_t
range(uint32_t low, uint32_t high)
439 uint32_t r = high - low + 1UL;
453#if ETL_USING_64BIT_TYPES
467 uintptr_t n =
reinterpret_cast<uintptr_t
>(
this);
468 value =
static_cast<uint64_t
>(n);
486 value = uint64_t(seed) | (uint64_t(seed) << 32U);
495 unsigned count = (unsigned)(value >> 59U);
497 value = (x * multiplier) + increment;
505 uint32_t
range(uint32_t low, uint32_t high)
507 uint32_t r = high - low + 1UL;
517 static ETL_CONSTANT uint64_t multiplier = 6364136223846793005ULL;
518 static ETL_CONSTANT uint64_t increment = 1ULL;
524#if ETL_USING_8BIT_TYPES
530 template <
typename THash>
531 class random_hash :
public random
539 uintptr_t n =
reinterpret_cast<uintptr_t
>(
this);
540 value =
static_cast<uint32_t
>(n);
547 random_hash(uint32_t seed)
556 void initialise(uint32_t seed)
564 uint32_t operator()()
574 uint32_t range(uint32_t low, uint32_t high)
576 uint32_t r = high - low + 1UL;
577 uint32_t n = operator()();
random_clcg()
Definition: random.h:240
void initialise(uint32_t seed)
Definition: random.h:262
random_clcg(uint32_t seed)
Definition: random.h:253
uint32_t range(uint32_t low, uint32_t high)
Get the next random_clcg number in a specified inclusive range.
Definition: random.h:285
uint32_t operator()()
Get the next random_clcg number.
Definition: random.h:272
random_lcg(uint32_t seed)
Definition: random.h:181
random_lcg()
Definition: random.h:168
void initialise(uint32_t seed)
Definition: random.h:190
uint32_t operator()()
Get the next random_clcg number.
Definition: random.h:199
uint32_t range(uint32_t low, uint32_t high)
Get the next random_clcg number in a specified inclusive range.
Definition: random.h:209
random_lsfr(uint32_t seed)
Definition: random.h:334
random_lsfr()
Definition: random.h:321
void initialise(uint32_t seed)
Definition: random.h:343
uint32_t range(uint32_t low, uint32_t high)
Get the next random_lsfr number in a specified inclusive range.
Definition: random.h:368
uint32_t operator()()
Get the next random_lsfr number.
Definition: random.h:351
random_mwc()
Definition: random.h:395
uint32_t range(uint32_t low, uint32_t high)
Get the next random_lsfr number in a specified inclusive range.
Definition: random.h:437
uint32_t operator()()
Get the next random_lsfr number.
Definition: random.h:426
random_mwc(uint32_t seed)
Definition: random.h:408
void initialise(uint32_t seed)
Definition: random.h:417
random_pcg(uint32_t seed)
Definition: random.h:475
void initialise(uint32_t seed)
Definition: random.h:484
uint32_t operator()()
Get the next random_lsfr number.
Definition: random.h:492
uint32_t range(uint32_t low, uint32_t high)
Get the next random_lsfr number in a specified inclusive range.
Definition: random.h:505
uint32_t operator()()
Get the next random_xorshift number.
Definition: random.h:122
random_xorshift(uint32_t seed)
Definition: random.h:101
random_xorshift()
Definition: random.h:88
uint32_t range(uint32_t low, uint32_t high)
Get the next random_xorshift number in a specified inclusive range.
Definition: random.h:140
void initialise(uint32_t seed)
Definition: random.h:110
ETL_CONSTEXPR14 T rotate_right(T value)
Definition: binary.h:161
bitset_ext
Definition: absolute.h:38