Commodore VIC-20 User Manual page 168

Hide thumbs Also See for VIC-20:
Table of Contents

Advertisement

154
The VIC 20 User Guide
cards on the VIC 20. This program fills a 52-element table D% with the
numbers I through 52 in a random sequence. (Element D% (0) is not used.)
The cards can be pegged to the random numbers in any way, such as
A= 1,2= 2, 3= 3, ... , Q= 12, K=13
Spades =0, Hearts = 13, Diamonds = 26, Clubs = 42
With this scheme, the Ace of Spades
=
1
+
0
=
1, the Queen of
Spades
=
12
+
0
=
12, the Three of Hearts
=
3
+
13
=
16, and so on.
In the shuffle program, a 52-element flag table FL keeps track of
whether a card has been chosen. PRINT statements are inserted to display
the seed value, followed by the numbers, in a continuous-line format. Note
that exactly 52 numbers are displayed and that no number is repeated. Each
program run will produce a new random sequence.
10 DIM
FL(~2),D~(52)
20 X=RND(-TI):PRINT X
30 FOR
I-I
TO
52
40
:C~-52*RND(I)+1
50
:IF
FL(C~)<)e
OOTO 41
60 : D:Y.(
n-c"
70
:PRINT
C:Y.;
80
NEXT
RUN
1. 18586613E-05
48 40 13 37 50 43 46 31 49 44
23 38
25
11
,
35 32 30 24 41
26 5 6 1 45
10 21
14 42 20 15
34 18
52 47 7
16
8
19
33
36
4
17 3 22 27 29 28 39 2 51
12
RUN
1.01154728E-06
14 35
52 50 26 48 27 36 34 25
18 20 41
33 39 7 46 24 23 28 1
9 3
12 43 2 31
44 4 1 32 37 3
o
40 22 45 48 42 49 16 11 6 10
2' 9 51
17 8 15 38 5 21
13
Notice that this program runs more slowly as it nears the 52nd number.
It
is especially slow on the last card. This is because the program has to fetch
more and more random numbers to find one that has not already been
selected. A simple routine such as this has much room for improvement, of
course. You can speed it up just by finding the last number in the program
from the table rather than waiting until it is selected randomly.

Advertisement

Table of Contents
loading

Table of Contents