Line
68
Execution jumps back to line 20
to
create another coin toss.
Execute and use
(BREAK)
to terminate the program several times. Notice that the
program generates exactly the same outcome every time. This
is
a characteristic
of
RND(l). Every time a program is run, the same sequence
of
random numbers will be
generated. While this is convenient when you are debugging your program, it does not
provide the unpredictability characteristic of true randomness. What is required
is
a
way of starting the sequence of random numbers
at
an arbitrary point.
Add the following line to the program:
15
A=RND(-VAL(RIGHT$(TIME$t2»)
List the program to verify that it
is:
10
CLS
15
A =
RND(-VAL(RIGHT$(TIME$t2»)
2121
A = RND (
1)
3121
IF
A(.5
THEN
A$="HEAD"
ELSE
A$="TAIL"
al2l
PR I
NT
A$,
"PRESS
ENTER
II
5121
A$
=
INKEY$
:
IF
A$
=
""
GOTO
5121
6121
GOTO
2121
Execute the program several times to verify that different sequences of outcomes are
being generated each time.
Remember to use
~
to terminate execution.
The RND function can be used with three types
of
arguments:
x
RND(x)
greater than
0
equal
to
0
less than 0
generates next random number
generates same random number
generates a new sequence of
random numbers dependent
upon the value of
x
In this program, the time
is
used
to
determine the sequence of random numbers by
negating the seconds
of
the current time. This will provide 60 different sequences of
random numbers. Recall that the time
is
stored
as
a string in TIME$
and
must be
converted
to
a numeric value using the VAL function.
Experiment
#8
Slot machine
This experiment will combine sound, animation and random numbers in one program
to
simulate a slot machine.
Enter the following program:
1121
A=RND(-VAL(RIGHT$(TIME$t2»)
2121
CLS
3121
LINE
(85t12)
-
(138,53)t1,6
a0
LINE
(88,22)
-
(110t33)
t1
t6
5121
LINE
-(122,22)
t1
,6
6121
LINE
-(13a,33)t1,6
151