152
The VIC 20
User
Guide
2
Y=3:REM
DECIMAL
DIGITS
WANTED
IN
NUMBER
30
R3=INT(X*R1+.5):REM
ROUND
TO
NEAREST
WHOLE
NUMBER
40
R4=INT(X*R1*10fY.5)/10fY:REM
ROUND
TO
Y
DECIMAL
PLACES
50
PRINT"
RAW
."iRl
55
PRINT"RANGED
*,";R2
60
PRINT"ROUNDED
";R3
65
PRINT"ROUNDED
DECIMFlL"iR4
The variable Y controls the number
of
decimal digits
of
precision in the
rounded decimal number. Your results will be similar to the following:
RAW
*
.672457317
RANGED
*'
67.2457317
ROUNDED
67
ROUNDED
DECIMAL
67.246
Generating
Random
Dice
Throws
Random
numbers are generated in the range 0 through
not
quite I (the
limit
of
I, in calculus terms). You will have to convert the random number to
whatever range you require. Suppose numbers must range from 1 to 6 (as in
one die number
of
a dice game). You will need to mUltiply the random
number by
6.
6 *
RND(l)
This returns a real number in a range just greater
than
0
but
less
than
6.
Add
I to get a number between I and
6.
6 *
RND(l)
+ 1
Then convert the number to
an
integer. This discards any fractional part
of
a
number, returning the number in the range I to 6 but in integer form.
INT(6 * RND(O) +
I)
or:
A%
= 6 * RND(O) + 1
The general cases for converting the
RND
fraction to whole number
ranges are shown below.
INT
(RND(O) * N)
INT
(RND(O) * N + I)
INT
(RND
(0) * N + M)
Range 0
to
N
Range 1
to
N
Range M
to
N