Classic Computer Magazine Archive COMPUTE! ISSUE 90 / NOVEMBER 1987 / PAGE 65

The Beginner's Page


C. Regena

The Random Function

The random number feature is a key ingredient in computer games and educational drill programs. This month, we will discuss how you can use random numbers in your own BASIC programs.

RND is the function for getting a random number in most versions of BASIC. The RND function returns a decimal fractional value between 0 and 1 (but never exactly 0 or 1). For example, try entering PRINT RND(1) and see what your computer does. Try it again. You should get a different number.

Actually, the computer is too logical and methodical to produce a truly random number. The RND function returns pseudo random values. It takes a given seed value and plugs it into a formula to generate the "random" value. If you knew the algorithm and seed value, you could predict the value that RND would return. However, the formula used is sufficiently complex that the results closely approximate a random distribution.

In real life, you don't usually want fractions—you want whole numbers. For example, you may want to generate two random numbers, each from 1 to 6, to simulate the roll of two dice. Or you may want to place obstacles on a game screen at randomly selected row and column positions. In these examples, we need to use whole numbers or integers. To change our random decimal fraction into an integer, we need BASIC's INTeger function. The INT function yields the integer, or whole number portion of a number. For example, INT(8.7914) is equal to 8.

To generate a random integer between 0 and 9, enter this line:

PRINT INT(10*RND(1))

The computer multiplies 10 by the random decimal fraction, and then converts it into an integer. Note that if the random fraction is less than .1, the value of INT(10*RND (1)) is 0. If the random fraction is greater than or equal to .9, the value of INT(10*RND(1)) is 9. Thus, our range of random numbers is 0 to 9. To get random numbers from 1 to 10, just add 1: INT(10*RND(1))+1.

Rolling Dice

Let's try another example. In rolling one die, the possibilities are numbers from 1 to 6. To simulate a roll in BASIC, we use the statement

D - INT (6 * RND(1)) + 1

Now let's shake two dice. Your total number will be a number from 2 (one dot on each die) to 12 (six dots per die). The formula is INT(11*RND(1))+2. In general, to choose a random integer number from A to B, inclusive, the formula is N=INT((B-A+1)*RND(1))+A.

Now let's try a short program that prints ten random numbers in the range 1–10:

100 FOR N = 1 TO 10
110 PRINT INT(10 * RND(1)) + 1
120 NEXT N

Run the program several times. If you are using an IBM PC, Atari ST, or Amiga, you get the same sequence of ten numbers each time you run the program. Knowing this sequence may help when you are debugging a program, but if you use the same sequence for a game, the game soon loses its appeal. So, for the IBM PC, Atari ST, and Amiga computers, you'll need to add the RANDOMIZE statement. RANDOMIZE needs to appear before any use of RND. To add it to the program above, add this line:

90 RANDOMIZE

Now, each time you run the program, you will get a different sequence of ten random numbers.

Different versions of BASIC use RANDOMIZE differently. You may need to specify a seed value that tells the computer what value it should start with. If your computer requires a seed, and you use RANDOMIZE by itself, the computer stops the program and asks you to enter a number. This number is used to generate the random number series. Having to enter a number can be a nuisance, so RANDOMIZE lets you specify a seed value like this:

RANDOMIZE X

or

RANDOMIZE 0

or

RANDOMIZE 532

However, if you use a constant number as the seed, the numbers will still be the same every time the program is run. On the Atari ST, RANDOMIZE 0 (that's a zero) makes the computer provide its own random seed. On the PC and Amiga, the statement RANDOMIZE TIMER tells the computer to use its internal clock value as a random seed. The TIMER value is almost always a different number each time the program is run.

The RND(1) function in the versions of BASIC for the Apple II and Commodore eight-bit computers does not return the same sequence of numbers each time a program is run, but it does always start with the same sequence after the computer is turned on or is reset. Although these computers do not have a RANDOMIZE command, you can still reseed the random number sequence. For the Commodore 64, a statement of the form X = RND(-TI) is equivalent to the RANDOMIZE TIMER statement described above.

In Atari eight-bit computers, the RND function returns random values generated by a hardware counter in the POKEY sound chip. Atari BASIC's RND function is therefore more truly random than in other BASICs, and no special randomization steps are required.