36 lines
1.1 KiB
C++
36 lines
1.1 KiB
C++
// *Really* minimal PCG32 code / (c) 2014 M.E. O'Neill / pcg-random.org
|
|
// Licensed under Apache License 2.0 (NO WARRANTY, etc. see website)
|
|
|
|
#include "pcg.h"
|
|
|
|
uint32_t pcg32_random_r(pcg32_random_t* rng)
|
|
{
|
|
uint64_t oldstate = rng->state;
|
|
// Advance internal state
|
|
rng->state = oldstate * 6364136223846793005ULL + (rng->inc|1);
|
|
// Calculate output function (XSH RR), uses old state for max ILP
|
|
uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
|
|
uint32_t rot = oldstate >> 59u;
|
|
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
|
|
}
|
|
|
|
// Source from http://www.pcg-random.org/downloads/pcg-c-basic-0.9.zip
|
|
void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate, uint64_t initseq)
|
|
{
|
|
rng->state = 0U;
|
|
rng->inc = (initseq << 1u) | 1u;
|
|
pcg32_random_r(rng);
|
|
rng->state += initstate;
|
|
pcg32_random_r(rng);
|
|
}
|
|
|
|
// Source from https://github.com/imneme/pcg-c-basic/blob/master/pcg_basic.c
|
|
uint32_t pcg32_boundedrand_r(pcg32_random_t *rng, uint32_t bound) {
|
|
uint32_t threshold = -bound % bound;
|
|
for (;;) {
|
|
uint32_t r = pcg32_random_r(rng);
|
|
if (r >= threshold)
|
|
return r % bound;
|
|
}
|
|
}
|