Pseudo Random Number Generator Tests

I've written these two programs to verify that the random number generators which I use for my games keep their promises in regard to the repetition rates. After the check these programs outlive their usefulness I'm afraid, however they may prove to be educational to you all the same.


Generating random numbers is a science in itself. I won't treat it very scientific myself though, because I don't really care how random my random numbers really are. They will be used for innocent little games here, so who cares!
In fact the random numbers we are talking about are better described as pseudo random numbers. This is because they are not at all random, but are somehow mathematically related to one another. For instance if the numbers were truly random it would be possible to generate a particular number out of a pool of n numbers twice in a row. With our pseudo random number generators this is not possible because the whole sequence will not repeat until all possible numbers are generated.

The 8-bit pseudo random number generator below will repeat its sequence after 255 calls (0 is excluded from the pool of random numbers). Whereas the 16-bit pseudo random number generator below will repeat its sequence after 65536 calls (all numbers from 0 are 65535 are included in the sequence).
To prove this I have written two small programs, called RND8TST.ASM and RND16TST.ASM. These programs are included in the download package on the left. The file name for RND8TST is 0005, while the file name for RND16TST is 0006.

                LD      A,(SEED)        Get random number seed
                LD      C,A             Calculate next random number
                AND     A,10111000B
                JP      PO,.SF
.SF             LD      A,C
                LD      C,A
                LD      (SEED),A        Save random number seed

The piece of Z80 code above shows the 8-bit pseudo random number generator routine. SEED is the starting point for calculating the next pseudo random number. The resulting pseudo random number is again saved as the SEED value for the next pseudo random number.
You must take care that the value of SEED may never be 0 because, if it is the code will only generate zeroes.
This routine generates a sequence of 255 pseudo random numbers. The exact same sequence repeats after 255 calls. The program RND8TST will prove that, because if you run it it will display         F F . Obviously this is a hexadecimal number, which is equal to 255 in decimal notation.

                LD      A,(SEED)        Get the 16-bit SEED value
                LD      L,A
                LD      A,(SEED+1)
                LD      H,A

                LD      D,L             Calculate pseudo random number
                LD      E,6
                SRL     D
                RR      E
                ADD     HL,DE

                LD      A,L             Save the 16-bit SEED value
                LD      (SEED),A
                LD      A,H
                LD      (SEED+1),A

And here we've got the 16-bit pseudo random number generator. It will generate a sequence of 65536 pseudo random 16-bit numbers. This sequence is repeated again after exactly 65536 calls.
The program RND16TST will prove that, although it takes a little longer before the result is visible. When you start the program the display will go blank for a while and then it displays     0 0 0 0 , again this is a hexadecimal number. This doesn't mean that it repeats after 0 calls of course, it means that it repeats after 10000H calls, the overflow from bit 15 to bit 16 got lost during the counting to keep the program simple.

This pseudo random number generator routine was borrowed from the 8052 BASIC code, although it is beyond any recognition because the Z80 doesn't have to move all the bytes through the Accumulator to get them shifted, rolled or added, whereas the 8052 can't do without.

Room For Improvement

As these are rather simple programs, intended for a one time use only, I didn't bother about some issues. However you may, as always, make some changes/improvements to the programs. I can assure you it helps you to better understand assembly programming if you start altering existing programs. Here are some ideas you might implement:

  • There is no "safety net" in these programs. I assume in advance that the sequences will repeat within the limits of the counters. If you don't know that in advance it would be nice to have a "safety net" which exits the loop in case the counter exceeds an acceptable maximum count. Otherwise the program may be caught in an endless loop.
    Just to give you an example: If you change the initial SEED value of the 8-bit random number generator to 0 you'll end up in an endless loop because a SEED of 0 always returns the value 0.
  • If you want to have a look at the sequence generated by the 8-bit generator you can add 3 lines of code to the RND8TST program which will store the entire sequence in RAM. Then, after running the program, you can browse through the list using the monitor.
    Please note that this is not possible with the RND16TST program because that would require 128k of RAM memory for the list alone! Mind you, we've only got 2kB!
  • In the RND16TST program I ignore the carry from bit 15 to bit 16, which results in a counter of 0000H when 65536 calls are made. You may allow this carry to become visible, allowing the correct value to be shown, which will be 10000H.


Here you can download the file, which contains the source listing, the actual plain hex file, a list file and an mp3 file of the program .