Generates an 8-bits, high-quality, pseudo-random number with each call. It uses 40 state-bits, giving a period of 962.072.672.512 numbers without repeating.
u8 cpct_getRandom_xsp40_u8 ();
call cpct_getRandom_xsp40_u8_asm
<u8> | Next 8-bits pseudo-random value. |
This function implements a sequence of 40-bits states with period (962.072.672.512). For each produced state, a random sequence of 8-bits is returned. To do this, the function has a 40-bits internal state, that is modified each time it is called. The result is a high-quality pseudo-random number generator that could virtually be considered random for general purposes of Z80-based software.
The sequence calculated by this function is based on a modified version of Marsaglia’s XORshift+ generator using the tuple (1, 2, 3), with the constant C=255, for an extended matrix T, composed by 4x4 8-bit shift matrices. This T matrix is populated with A=(I+L^a)*(I+R^b), B=0, C=(I+R^c), and D=0. Selected tuple does not yield a full-period (which would be 2^40 - 2^8), but it is very close and is enough for passing all Dieharder tests.
To clarify operations, assuming that the 40-bits state s is composed of 5 8-bits numbers s=(x, z, y, w, v), this algorithm produces a new state s’=(x’,z’,y’,w’,v’) proceeding this way:
x' = y; y' = z; z' = w; t = x ^ (x >> 1); t' = t ^ (t >> 2); w' = y ^ (y << 3) ^ t'; v' = v + 255; returned value = w' ^ v'
These operations are performed in an optimized fashion.
AF, DE, HL
37 bytes
Case | microSecs (us) | CPU Cycles ----------------------------------------- Any | 45 | 180 -----------------------------------------