Example
The RND function
is
often used in game programs to produce unpredicatable
numbers,
as
in games
of
chance.
Let's
try
using
the
RND function
to
investigate the
percentage
of
times each
of
the six sides
of
a die comes up by simulating the action
of
throwing
it
a given
number
of
times.
Since the sides
of
each die are numbered from 1 to 6, we
must
multiply the value
returned
by
the
RND function
by
6.
X6
O<R
NO
C
1)
< 1
---
---.
0<6>k
RNO C1 )
<6
Then We
must
use
the
INT function
to
convert the value obtained to an integer.
The
result will be an integer between 0 and 5; now 1 is added
to
obtain the numbers
which correspond
to
the
number
of
dots on each
of
the 6 sides
of
a die.
This sequence
is
performed a specified
number
of
times for each die thrown. Now
let's incorporate
the
sequence
into
a program and check the results.
10
PRINT
11
ENTER
NO.
OF
The
RND
TIMES
DIE
THROWN
11
function
generates numbers
in
the
range
from
0.0000001
to
20
I
NPUT
N
0.99999999.
30
FOR
0=1
TO
N
40
R=
I NT C6>kRNO (1
))
+1
50
IF
R=1
THEN
N1=N1+
1
~
·
60
IF
R=2
THEN
N2=N2+1
70
I F
R=3
THEN
N3=N3+1
80
IF
R=4
THEN
N4=N4+1
0
~lfff~~tz~
®a
o»»~®
90
IF
R=5
THEN
N5=N5+1
100
IF
R=6
THEN
N6=N6+1
~@
or7J
or@
110
NE XT 0
120
P1=N1
/ N :
P2=N2
/ N :
P3=N3
/ N
130
P4=N4
/ N :
P5=N5
/ N :
P6=N6
/ N
14
0
PRINT
P1. P2. P3. P4. P5.
P6
1
50
END
How
about
it?
If
the
die is thrown enough times, the percentage
of
the time each
number
appears should be
about
the
same. Mathematically speaking, each
number
should
occur
an average
of
once in six throws,
or
about
16.7%
of
the
time. This
mathematical ideal is approached more closely as the number
of
throws is increased.
-------------------------
--
--------------------------------------------
73