From: Pat Thoyts Date: Tue, 16 Dec 2014 13:04:13 +0000 (+0000) Subject: Use 16 bit galois field based PRNG. X-Git-Url: https://privyetmir.co.uk/gitweb.cgi?a=commitdiff_plain;h=b321441f72bff1b839f7c9782ea5ebe4102ce7e8;p=vetinari_clock Use 16 bit galois field based PRNG. This has much better distribution in the low bits over its period. --- diff --git a/main.c b/main.c index 34c5e8e..0ab0b8d 100644 --- a/main.c +++ b/main.c @@ -19,16 +19,19 @@ #define CLKP BIT6 /* P1.6 */ #define CLKN BIT7 /* P1.7 */ -static unsigned long next = 0; -static void my_srand(unsigned long seed) +static uint16_t prng_state = 0xACE1u; +static void my_srand(uint16_t seed) { - next = seed; + prng_state = seed; } -static unsigned int +static uint16_t my_rand() { - next = next * 1103515245 + 12345; - return (unsigned int)next; + unsigned lsb = prng_state & 1; + prng_state >>= 1; + if (lsb == 1) + prng_state ^= 0xB400u; + return prng_state; } static void @@ -53,11 +56,10 @@ main(void) WDTCTL = WDTPW + WDTHOLD; /* external 32.768kHz crystal */ - BCSCTL1 |= DIVA_3; /* ACLK/8 */ + BCSCTL1 |= DIVA_3; /* ACLK/8: 4096 clock interrupts per second */ BCSCTL3 |= XCAP_3; /* enable 12.5pF internal capacitance */ - srand(2); - my_srand(0xdeadbeef); + my_srand(0xACE1u); /* FIX ME: get a physically derived seed */ setup(); /* enable global interrupts */