Generate a Random Number Using the RND and PEEK Commands in Atari BASIC (5 mins)

Task: Generate a Random Number Using the RND AND PEEK Commands in Atari BASIC

Needed: An Atari 8-Bit Computer or Emulator Running BASIC

Time: 5 Minutes or Less

Introduction

Generating pseudo random numbers on retro computers has always been an interesting topic, especially for machines like the Atari 2600 that do not have a clock for setting the seed. The Atari 8-bit computers have a built-in random number generator that is included in the POKEY chip. This routine uses a linear feedback shift register (LFSR) that was popular in the 1980s. Interestingly, this is located in the POKEY because the random number sequences are used to generate the noise channel for sound output (think explosion sounds). As users, we can access these random numbers easily from BASIC.

Instructions

Boot an Atari 8-bit computer or emulator in BASIC. You can call a random number between 0 and 1 in two different ways. Both use the POKEY LFSR routine with seeds based on internal hardware timing.

  1. PRINT RND(1)
  2. PRINT PEEK(53770)/256
Random Numbers in Atari BASIC
Random Numbers in Atari BASIC

Comments

I did a deep dive into random number generation when I programmed by Atari 2600 game Gene Medic. I implemented an LFSR but had no way to generate the random seed since the 2600 has no clock. I ended up using the X position of the player on the screen to set the seed which I think works quite well. Below is the assembly code I used.

Random Numbers in the Atari 2600
Random Numbers in the Atari 2600