Random numbers

When you throw dice it is hard to guess, or predict, what numbers you will get. Unpredictable numbers like these are called random numbers. Below you can find out how to make a computer produce random numbers. The command is quite complicated, but it is useful in all sorts of programs.

PRINT INT(RND*10+1)

This produces a random number between 1 and 10.

Displaying random numbers

Here is a simple random number program to try. Line 40 tells the computer to pick a number between 1 and 20 and display it on the screen. Run the program a few times. The computer will pick a new number each time.

NEW
10 CLS
20 PRINT "HERE IS A NUMBER"
30 PRINT "BETWEEN 1 AND 20"
40 PRINT INT(RND*20+1)
RUN

How does RND work?

To complete the instruction you need to multiply by the number of numbers the computer can choose from, and add the lowest number it can pick.

For example, to make the computer pick numbers from 1 to 6, like a dice, you need the instruction below.

PRINT INT(RND*6+1)

Lucky draw program

The monster gang are having a lucky draw. They have sold 30 tickets, numbered from 1 to 30, and they want the computer to pick the winning number. The program below will make the cmputer do this, but it is not complete. See if you can fill in the missing numbers in line 20. This is the line that tells the computer to pick a random number between 1 and 30.

NEW
10 CLS
20 W = INT(RND * ?? + ??)
30 PRINT "HELLO MONSTERS"
40 PRINT "THE LUCKY WINNER"
50 PRINT "IS NUMBER";W
RUN