/* ex: set ts=2 et: */ /* by Ryan Flynn (pizza@parseerror.com) */ /* generate psuedo-randomness via cellular automata */ /* (http://mathworld.wolfram.com/CellularAutomaton.html) */ /* constraints: minimal memory and CPU */ #include #include static uint8_t Rule = 89; /* rule #, previous bits used to decide future bits */ static uint16_t State = 0x6969; /* seed value, use whatever */ static void dumpbits(void) { /* print bits to screen */ uint16_t i = 1; do fputc('0' + !!(State & i), stdout); while (i <<= 1); } /* return a psuedo-random 16-bit value */ static uint16_t prand(void) { /* advance State to next */ uint16_t s = 0; /* next state */ uint8_t i = 15; do { if (Rule & (1 << ((State >> i) & 0x7))) s |= 1 << i; } while (i--); State = s; } int main(void) { int i = 32; uint16_t r; do { r = prand(); printf("[%2u] ", i); dumpbits(); printf("\n"); } while (--i); return 0; }