Returns a pseudo-random byte using a fast Linear-Congruential-Algebra (LCG) method (33*Seed mod 257)
u8 cpct_getRandom_lcg_u8 (u8 entropy_byte) __z88dk_fastcall;
(1B L) entropy_byte | An optional byte coming from an entropy source to mix up the sequence. Use 0 if you have no entropy byte. |
call cpct_getRandom_lcg_u8_asm
<u8> | Next 8-bits pseudo-random value. |
This function returns a pseudo-random byte using a Linear-Congruential-Algebra. Each new byte returned is obtained as a result of the next function,
Seed = (33*Seed % 257) - 1; return Seed + 1;
Seed is then updated to the value of the LCG function minus 1, to be used for the next call to the function. The seed may be manually set using the function cpct_setSeed_lcg_u8 to create predictable sequences (starting always with the same seed) or to randomize the start of a the sequence (by entering a seed coming out of an entropy source).
Linear-Congruential-Algebra methods are known for being really simple and low-quality methods. As Marsaglia noted in his works, LCG methods produce numbers equally distributed in predictable hyperplanes in the space, which makes them highly predictable and low-quality. However, these methods might come in handy due to their simplicity, as their cost is also really low.
The parameter entropy_byte is an addition to this function to let the user improve the random quality of the sequence by inputting mixer bytes coming from entropy sources. The function does an XOR operation between entropy_byte and the previous seed before doing the calculations for the next random byte, this way,
Seed = (33*(Seed ^ entropy_byte) % 257) - 1; return Seed + 1;
Therefore, if you use *entropy_byte*s coming from good entropy sources, you will be effectively mixing up the sequence and getting better quality random numbers. This could also be used for other purposes, like creating restricted or repeating sequences, up to the user imagination.
If you have no entropy source or do not want to use the entropy_byte, just input 0 which will have no effect on the sequence.
AF, C, L
17 bytes
Case | microSecs (us) | CPU Cycles ----------------------------------------- Any | 20 | 80 -----------------------------------------
This function comes from Fast RND, which can be found published by z80-info. The original code is reported to be from Spectrum ROM.