Chapter
4:
Advanced
BASIC
Programming 153
Now experiment with a variety
of
different random number ranges by
modifying the statements illustrated above.
The program below shows
-TI
being used to generate a random seed.
This program calculates numbers in the range M to
N.
In this program, the
values
of
M
and
N are set in line
10
for a given program run. Note
that
these
values can be negative. In the following example, the display
is
an
unending
sequence
of
random
numbers between
-50
and
+50.
(Press the STOP key to
end the program.) A different sequence
of
numbers will be printed each time
the program runs, since - TI provides a
random
seed. Note
that
the X value
returned from
RND(-TI)
is
displayed instead
of
the TI value.
10
M=-50:N
..
50
20
X=RND(-TI):PRINT
X
30
FOR
1=1
TO
8
40
:C%=(N-M+l)*RND(I)+M
50 . :
PRINT
C"
55
:NEXT
60
GOTO
30:REM
PRINTS
NEW
RANDOM
NUMBERS
RUN
8.
27633085E-06
-14
29
7
35
-32
-12
48
-18
To
illustrate different number ranges, change the values
of
M and N in
line
10
of
the above program.
For
example, make M = 1 and N =
6;
this
will generate
an
unending sequence
of
random numbers between 1 and
6.
Random
Selection
of
Playing
cards
A quick scan
of
the display above shows
that
numbers repeat within the
first
100
generated.
That
is,
101
numbers will not include every number in
the range
-50
to
+50
with no duplications. This
is
fine in, say, a dice game,
but
for other applications you may need to produce random numbers in a
certain range where every number occurs
and
there are no duplications.
Dealing from a deck
of
cards
is
one such application. Once a card has been
selected, it cannot be selected again during the same deal.
The program below shows one way
to
program shuffling a deck
of