# cpct_getRandom_xsp40_u8

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.

#### Assembly call

`call cpct_getRandom_xsp40_u8_asm`

#### Return value (Assembly calls, return L=A=random 8-bits)

 Next 8-bits pseudo-random value.

#### Important details

• This function will not work from a ROM, as it uses self-modifying code.

#### Details

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

#### Time Measures

```   Case     | microSecs (us) | CPU Cycles
-----------------------------------------
Any      |      45        |    180
-----------------------------------------```

#### Random quality measures

• Dieharder tests rank: (113 Pass, 1 Weak, 0 Failed) (340/342 = 99.42%)
• Pseudo-random bit stream velocity: 5,625 us / bit.  (8 bits produced in 45 us)
• Pseudo-random bits per second: 177.777,77 bps.

#### Credits

unsigned char (u8 = unsigned 8-bits, 1 byte )
Generates an 8-bits, high-quality, pseudo-random number with each call.