3 ms·
LFSR's are mind blowing; the fact that you can generate a randomish sequence that covers an entire ^2 range visiting each number once, and only once, seems like
by jacknews 4y ago
LFSR's are mind blowing; the fact that you can generate a randomish sequence that covers an entire ^2 range visiting each number once, and only once, seems like a lucky piece of magic.
And in just a couple of lines, excuse my quickie untested code:
#include <stdio.h>
#include <stdint.h>
// 5-bit maximal LSFR taps to get pseudo-random sequence of 31 unique numbers in range 1-31
// 0x12, 0x14, 0x17, 0x1B, 0x1D, 0x1E, 0x18, 0x17
int main(int c, char **v) {
uint8_t taps = 0x17; // could be any of the taps above
uint8_t seed = 0x7; // start, could be 1-31
uint8_t lfsr = seed;
do {
printf("%d\n", lfsr);
lfsr = (lfsr & 1)
? (lfsr >> 1) ^ taps
: (lfsr >> 1);
} while ( lfsr != seed );
return 0;
}