Skip to content

Instantly share code, notes, and snippets.

@alwynallan
Created January 21, 2024 23:06
Show Gist options
  • Save alwynallan/489b6fd14b63605f7838c657340008c6 to your computer and use it in GitHub Desktop.
Save alwynallan/489b6fd14b63605f7838c657340008c6 to your computer and use it in GitHub Desktop.
PRNG with seeding and output - see link in code
// https://www.reddit.com/r/RNG/comments/19a2q24/collatzweyl_generators/?utm_source=share&utm_medium=web2x&context=3
// $ gcc -Wall -O3 Collatz-Weyl.c -o Collatz-Weyl
// $ ./Collatz-Weyl | pv > /dev/null <-- 650MiB/s on 2018 Zeon VM
// compared with 400MiB/s for ChaCha20
#include <stdio.h>
#include <assert.h>
static __uint128_t c[4] = {0, 0, 0, 0}; // c[0] must be odd
__uint128_t CWG128(void) {
c[1] = (c[1] >> 1) * ((c[2] += c[1]) | 1) ^ (c[3] += c[0]);
return c[2] >> 96 ^ c[1];
}
int main(void) {
FILE * fr = fopen("/dev/random", "r");
assert(fr);
assert(1 == fread(&c[0], sizeof(c[0]), 1, fr));
fclose(fr);
c[0] |= 1U; // odd
for(int i=0; i<96; i++) CWG128();
while(1) {
__uint128_t o = CWG128();
fwrite(&o, sizeof(o), 1, stdout);
}
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment