The program generates random numbers
th
at are always one less
than the number by which they are multiplie
d.
The first line 10
generates the numbers 0, 1, and 2.
To
generate random numbers
0,
1,
2,
and 3, the program would be writt
en
this way:
1.0
PRINT
INT
(RNIH
0)
*4)
RUN
To
generate only the numbers 1,
2,
and
3,
the program should look
like
this:
1.0
PRINT
INT
CRND(0)*3)+1.
RUN
To generate three numbers starti
ng
at
20,
write the program this way:
1.0
PRINT
INT
CRND(0)*3)+20
RUN
Random Number Game
Random-number programs are very flexible. You can even use them to
play games with your computer. Type the following program.
Remember that to get the bent arrow
in
line 5, press I
Es
c
I,
hold down
~
or I Control I , and press
IClear\.
NEW
1.
REM
***
NUMBER.
GAM
***
5
PRINT"
"'
."
1.0
SECRETNUM=INTCRND(0)*3)
+1.
20
PRINT:
PRINT
"I
AM
THINKING
OF
A
NUMBER
..
EITHER
1.
..
2
..
OR
3.
TRV
TO
GUESS
IT.
"
30
INPUT
GUESS
40
IF
GUESS=SECRETHUM
THEN
PRINT
"VOU
WON.
"
50
IF
GUESS<>SECRETNUM
THEN
PRINT
"VOU
LOST.
"
60
GOTO
1.0
Line 10 assigns the random number to the numeric variable called
SECRETNUM. Line
30
lets the user type
in
a guess and assigns this
number to the numeric
variable ca
ll
ed
GUESS. (Remember that
numeric
variables do not need to
be
dimensioned or tagged at the end
the way that string
variables do.) Line 40 compares the guess to the
secret number.
If they equal each other, the computer prints
"YO
U
WON
."
Line 50 also compares the guess to the secret number. If they
are not
equal (the symbols < > mean not equal to), the computer prints
"Y
OU
LOST
."
Line 60 makes a loop so that you can play the game
again. (The next chapter
explains
IF
-THEN statements
in
more detail.)
52