Z80 Assembly 29: Simple Random Number Generation (RNG) for Games
The Challenge of Randomness CPUs are deterministic: they do the same thing every time. To generate a truly unpredictable sequence, we need a source of entropy (unpredictable noise) from the outside world. Method 1: Hardware Noise (Simple Seed) The fastest way to get a simple random number is to read a register or port whose value is constantly changing due to external factors. Noise Sources: I/O Port Reading: Reading from an unused or floating I/O port can return unpredictable bits of electrical noise. The Refresh Register (R): As discussed earlier, the R register increments automatically, making its low bits somewhat unpredictable depending on when it’s read. The Keyboard: Polling the keyboard port and using the lower bits of the input byte before any key is pressed. Example: Reading an Unpredictable Port ...