To illustrate different number ranges, change the values of M and N in line
10.
For example, make
M=l
and
N=6;
this
will
generate
an
unending sequence
of random numbers between 1 and
6.
A quick scan of the display above shows that numbers repeat
within
the
first 100 generated. That
is,
every
101
numbers
will
not sort from -50 to
+50
with
every number present and no duplications. This
is
fine
in.
say.
a dice game where
Vou
take the rolls
as
they come. For other random number uses, however,
Vou
may need to develop random numbers
in
a certain range
where
every number
is
accounted for and there are
no
duplications. An example
is
sorting a deck
of cards.
Here vou need to pick a card, and when that card
has
been picked
it
can-
not
be
picked again
du
ring the same deal.
The program
below
shows one
way
to program shuffling a deck of cards
on
the PET. It fills a 52-element table 0%*
with
the numbers 1 through 52 in a
random sequence. The cards can
be
pegged to the random numbers in any way,
such
as:
A=1,
2~2. 3~3,
H'
Q~12,
K=13
Spades=O. Hearts=
13,
Diamonds=26, Clubs=42
4
44
41
15
36
12
:31
49
3(1
24
42
20
33
51
36 34
.")C'
,;.._1
24
2·
...
28
1
.:,.
1
.-..-.
37
:~:
.:,
.
.::.
16
11
6
10
5
21
13
46
32
9
:3
(1
4(1
29
With
this scheme the Ace of Spades = 1
+0=
1,
the Queen of
Spades=12+0=12,
The Three of
Hearts=3+
13=
16.
etc.
ln the shuffle program, a 52-element flag table
FL
keeps track of whether a
card
has
been picked or not.
PRINT
statements are inserted to print out the seed
value, followed by the numbers in a continuous-line format. Note that exactly 52
numbers are printed out and that no number
is
repeated. Each program run
will
produce a new random sequence.
10
D1M
FL(52»D~(52)
20
X=RND(-T1):PR1NT X
3(1
FOI<:
1=1
TO
52
40
C~=52*I<:ND(1)+1
5(1
IF
FL
(c;,~)
O(l~
GOTO
40
60
D~(1)=C~:FL(C~)=l
7(1
PR
l
NT
C;~
.;
8(1
t'~EXT
l
RUN
1.
18586613E-05
48 40
13
37
50
43
23
38
25
11
9
35
26
5 6 1
45
10
21
14
34
18
52
47
7 16 8
19
17 3
22
27
29
28
39
2
F.:UN
1.
01154728E-06
14
35
52
50
26
48
27
18
20
41
33
39
7
46
12
43
2 31
44
4
22
45
48
42 49
9 51
17
8 15
38
"Element 0
is
not
used.
296