Sets machine state (seed) for 16-bits Galois Linear-Feedback Shift Register pseudo-random number generator.

C Definition

void cpct_setSeed_glfsr16 (u16 newseed) __z88dk_fastcall;

Input Parameters (2 bytes)

(2B HL) newseednew machine state (seed) to be set as internal state for the G-LFSR random number generator.

Assembly call

call cpct_setSeed_glfsr16_asm

Parameter Restrictions

  • newseed - Any value can be used as seed except 0.  If you seed the G-LFSR random number generator with 0, you will get a always 0 sequence.

Known limitations

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


It sets the seed that 16-bits Galois Linear-Feedback Shift Register (G-LSFR16) will use from now on to produce pseudo-random numbers.  This is useful in two main cases:

  • You want to produce always the same pseudo-random sequence
  • You want to produce a different pseudo-random sequence

In the first case, initializing the pseudo-random number generator always with the same seed will produce exactly the same sequence.  Producing the same sequence let you repeat a generation completely (for instance, if you are generating a labyrinth, you may generate the same one again).  On the contrary, the second case requires the seed to be as random as possible.  If you introduce a more-or-less random feed, the sequence will be different each time.  For this purpose, you may get pseudo-random inputs like noise coming from cassette tape reader, or time between keypresses of a user while typing input.

With respect to the pseudo-random sequence generated, take into account that G-LSFR16 algorithms implement a 65535 numbers concrete pseudo-random sequence.  When you set the seed, you are picking up which one of that 65535 numbers will be the start of the sequence, but that sequence is cyclic.  If you want the sequence to have a different internal order, you may use cpct_setTaps_glfsr16 along with one of the 1024 different possible sequences, codified in a TAPSET from <GLFSR16_TAPS>.

Destroyed Register values

All preserved

Required memory

4 bytes

Time Measures

   Case     | microSecs (us) | CPU Cycles
   Any      |       8        |     32
Sets machine state (seed) for 16-bits Galois Linear-Feedback Shift Register pseudo-random number generator.
unsigned int (u16 = unsigned 16-bits, 2 bytes)
Returns a pseudo-random byte uniformly distributed using fast method (33*Seed mod 257)