Skip to content

Instantly share code, notes, and snippets.

@schwern
Created April 29, 2016 22:35
Show Gist options
  • Save schwern/5d0d1ea3c1817fbba0d0d50c1d7b636b to your computer and use it in GitHub Desktop.
Save schwern/5d0d1ea3c1817fbba0d0d50c1d7b636b to your computer and use it in GitHub Desktop.
Demonstrate it's safe to use a PRNG to seed more PRNGs with a GOOD PRNG.
/* See http://www.pcg-random.org/using-pcg-c-basic.html */
#include "pcg_basic.h"
#include <stdio.h>
void try_random(unsigned int seed, int initseq) {
pcg32_random_t rng;
pcg32_srandom_r(&rng, seed, initseq);
printf("Seed: %u\n", seed);
for( int i = 0; i < 5; i++ ) {
printf("%u ", pcg32_random_r(&rng));
}
printf("\n");
}
int main(void) {
/* Deliberately using a fixed init sequence to remove that as a
source of entropy for the purpose of this test. Normally use
something like the pointer to the rng. */
int initseq = 6;
/* The initial seed does not matter */
int master_seed = 5;
pcg32_random_t seed_rng;
pcg32_srandom_r(&seed_rng, master_seed, initseq);
for( int i = 0; i < 5; i++ ) {
unsigned int seed = pcg32_random_r(&seed_rng);
try_random(seed, initseq);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment