cpct_getRandom_mxor_u8

Gets a high-quality 8-bit pseudo-random number using Marsaglia’s XOR-shift algorithm (Using a 32-bits state)

C Definition

u8 cpct_getRandom_mxor_u8 ();

Assembly call

call cpct_getRandom_mxor_u8_asm

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

<u8>Next 8-bits pseudo-random value.

Known limitations

  • This function cannot be used from ROM, as is uses self-modifying code.

Details

This function uses cpct_nextRandom_mxor_u32 to produce a stream of pseudo-random 32-bits numbers, and use them as groups of 4 8-bits values.  Then, it returns each one of the last 4 8-bit values produced before calling cpct_nextRandom_mxor_u32 again.  It uses cpct_mxor32_seed as storage buffer for the last 4 pseudo-random values got from cpct_nextRandom_mxor_u32.

As cpct_nextRandom_mxor_u32 produces (2^32)-1 32-bits numbers without repetition, this function will produce (2^34)-1 8-bits values without repetition.

It is important to know that this function will have 2 different behaviors

  • [available] 3 out of every 4 calls, it will only return the next random-byte from the buffer (cpct_mxor32_seed).
  • [production] 1 out of every 4 calls, it will also call cpct_nextRandom_mxor_u32 to produce 4 new random bytes.  This behaviour is reflected in the time measures.

This function uses Marsaglia’s XOR-shift standard algorithm with a concrete shift tuple.  Check cpct_nextRandom_mxor_u32 to know details on how this is produced.

Destroyed Register values

AF, BC, DE, HL

Required memory

76 bytes divided in,

Time Measures

   Case     | microSecs (us) | CPU Cycles
-----------------------------------------
 available  |      23        |     92
 production |      88        |    352
-----------------------------------------
 average-4  |     39,25      |    157
-----------------------------------------
unsigned char (u8 = unsigned 8-bits, 1 byte )
Gets a high-quality 8-bit pseudo-random number using Marsaglia’s XOR-shift algorithm (Using a 32-bits state)
Calculates next 32-bits pseudo-random number in Marsaglia’s XOR-shift 8-9-23 sequence.
_cpct_mxor32_seed:: .dw #0x1A7B, #0x59F2
Contains the 32-bits seed for Marsaglia’s XOR-shift random number generator.
Close