Sharp MZ-700 Series Owner's Manual page 75

Personal computer
Hide thumbs Also See for MZ-700 Series:
Table of Contents

Advertisement

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.
x 6
0 < R N D ( 1 ) < 1
*
0 < 6 - X R N D ( 1 )
< 6
Then we must use the INT function to convert the value obtained to an integer.
I N T ( 6 X R N D ( 1 ) ) ->Os 1 , 2s 3 , 4s
5
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.
I N T ( 6 X R N D ( 1 ) ) + 1 —1 s 2s 3s 4 s 5s
6
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.
1 0
P R I N T
" E N T E R
N O .
O F
T I M E S
D I E
T H R O W N " ;
2 0
I N P U T
N
3 0
F O R
J = 1
T O
N
4 0
R = l N T ( 6 X R N D ( 1 ) ) + 1
5 0
I F
R = 1
T H E N
N 1 = N 1 + 1
6 0
I F
R = 2
T H E N
N 2 = N 2 + 1
7 0
I F
R = 3
T H E N
N 3 = N 3 + 1
8 0
I F
R = 4
T H E N
N 4 = N 4 + 1
9 0
I F
R = 5
T H E N
N 5 = N 5 + 1
1 0 0
I F
R = 6
T H E N
N 6 = N 6 + 1
or
S )
1 1 0
N E X T
U
1 2 0
P 1 = N 1 / N : P 2 = N 2 / N : P 3 = N 3 / N
1 3 0
P 4 = N 4 / N : P 5 = N 5 0 N : P 6 = N 6 0 N
1 4 0
P R I N T
P 1 , P 2 , P 3 . P 4 , P 5 ,
P 6
1 5 0
E N D
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.
T h e R N D
____
f u n c t i o n generates numbers in
the range f r o m 0 . 0 0 0 0 0 0 1 t o
0.99999999.
DKl^seTOEXtDjK®
=
/ S
Ft7
(5)
73-

Advertisement

Table of Contents
loading

Table of Contents