Skip to content

Instantly share code, notes, and snippets.

@lemire
Created February 9, 2019 14:38
Show Gist options
  • Save lemire/97945a4ba828ac8634ed75a071464ff1 to your computer and use it in GitHub Desktop.
Save lemire/97945a4ba828ac8634ed75a071464ff1 to your computer and use it in GitHub Desktop.
/***
result:
152ca78d
027c6003
cb07bbf3
f98befee
1cd777e3
a4e29590
661e4b6d
093b9e0e
b7e9851d
e71f2e4d
bdb2a071
469753f2
d4195b44
8d5b2e0a
e749bf46
7370bb1c
b9ad21f8
cfad21e0
843fa922
f16b535e
*/
/* Modified by D. Lemire based on original code by M. O'Neill, August 2017 */
#include <stdint.h>
#include <stdio.h>
struct pcg_state_setseq_64 {
uint64_t
state; // RNG state. All values are possible. Will change over time.
uint64_t inc; // Controls which RNG sequence (stream) is
// selected. Must *always* be odd. Might be changed by the user, never by our
// code.
};
typedef struct pcg_state_setseq_64 pcg32_random_t;
static pcg32_random_t pcg32_global; // global state
static inline uint32_t pcg32_random_r(pcg32_random_t *rng) {
uint64_t oldstate = rng->state;
rng->state = oldstate * UINT64_C(0x5851f42d4c957f2d) + rng->inc;
uint32_t xorshifted = ((oldstate >> 18) ^ oldstate) >> 27;
uint32_t rot = oldstate >> 59;
return (xorshifted >> rot) | (xorshifted << (32 - rot));
}
static inline uint32_t pcg32(void) { return pcg32_random_r(&pcg32_global); }
int main() {
pcg32_global.state = UINT64_C(0x853c49e6748fea9b);
pcg32_global.inc = UINT64_C(0xda3e39cb94b95bdb);
for (uint32_t t = 0; t < 20; t++) {
printf("%08x\n", pcg32());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment