64-bit LFSR-based PRNG implementation in Octo
All 8 bytes of state are used to produce a single byte of output, in an attempt to mask patterns in the LFSR's sequence. If you have ideas about how to improve the output, please let me know.
If you don't want to get the same numbers every time your program runs, you will need to seed the state of the PRNG/LFSR with some random numbers:
v0 := random 0xFF
v1 := random 0xFF
v2 := random 0xFF
v3 := random 0xFF
v4 := random 0xFF
v5 := random 0xFF
v6 := random 0xFF
v7 := random 0xFF
save lfsr_state
You can seed the state with a constant value if you do want the same sequuence every time.
To get a sequence of pseudo-random values, you will need advance the state and collapse the output for each byte:
lfsr_advance_state # This must be called to go to the next number
lfsr_collapse_output # This turns the 64-bit state into a single 8-bit number
vE := v1 # That number is returned in v1
The lfsr_collapse_output
function simply XORs the bytes of state together. This could probably stand to be a bit more
complicated.
State can be saved with the lfsr_save_*
functions and loaded with the lfsr_load_*
functions.
Two memory locations (lfsr_saved_0
and lfsr_saved_1
) are provided by default.
To simplify the load/save functions, one function is used for each possible storage location.
If you need more, you will have to duplicate those functions and provide another 8-byte area to save to.