ampler/random.c

60 lines
1.0 KiB
C

// random.c
// !!!!YOU WERE HERE!!!!!//
// MAKE A DOOM STYLE SHUFFLED RANDOMIZER, FOR JUST 8 BITS
//
// original uses unsigned long, tcc says that it's 4 bytes
// public domain impl from:
// http://lomont.org/papers/2008/Lomont_PRNG_2008.pdf
u32 well_512() {
// TODO: Make this more random / make sure it's correct ?
/* new Uint32Array([2 << 30]).map(x => Math.random() * x).join(",\n") */
static u32 state[16] = {
1096601398, 347948986, 707021053, 1924450882,
1184298871, 1860618357, 1301703596, 86165936,
160838326, 1276044826, 98793404, 1014941842,
1604941344,1520346171, 726203645, 1872316350,
};
static u32 index = 0;
u32 a, b, c, d;
a = state[index];
c = state[(index + 13) & 15];
b = a ^ c ^ (a << 16) ^ (c << 15);
c = state[(index + 9) & 15];
c ^= (c >> 11);
a = state[index] = b ^ c;
d = a ^ ((a << 5) & 0xda442d24UL);
index = (index + 15) & 15;
a = state[index];
state[index] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28);
return state[index];
}
u8 rnd_u8() {
return well_512() & 0xff;
}