Using RND() and MOD() to Shuffle a Deck of Cards


Use this code to fill an array with playing card values. Each element of the array is initially assigned a number 1 through 52, then those numbers are randomly swapped using the RND() function. The Mod() function is used to decipher the card's suite and value. Adding +1 to the returned Mod() value eliminates 0's and keeps suit values 1 - 4 (clubs, diamonds, hearts, spades) and numeric values 1 - 13 (or ace through king).

Deciphering the Card Value with Mod()

The Mod() function returns the remainder when one number is divided by another. The variable s represents the suit and the variable c represents the card value.
    s = (n(i) mod 4) + 1
    c = (n(i) mod 13) + 1

Number
(n Mod 13) + 1
Value
(n Mod 4) + 1
Suit
1
1 + 1
2
1 + 1
2 (Diamonds)
2
2 + 1
3
2 + 1
3 (Hearts)
3
3 + 1
4
3 + 1
4 (Spades)
4
4 + 1
5
0 + 1
1 (Clubs)
5
5 + 1
6
1 + 1
2 (Diamonds)
6
6 + 1
7
2 + 1
3 (Hearts)
7
7 + 1
8
3 + 1
4 (Spades)
8
8 + 1
9
0 + 1
1 (Clubs)
9
9 + 1
10
1 + 1
2 (Diamonds)
10
10 + 1
11 (Jack)
2 + 1
3 (Hearts)
11
11 + 1
12 (Queen)
3 + 1
4 (Spades)
12
12 + 1
13 (King)
0 + 1
1 (Clubs)
13
0 + 1
1 (Ace)
1 + 1
2 (Diamonds)
22
9 + 1
10
2 + 1
3 (Hearts)
23
10 + 1
11 (Jack)
3 + 1
4 (Spades)
24
11 + 1
12 (Queen)
0 + 1
1 (Clubs)
25
12 + 1
13 (King)
1 + 1
2 (Diamonds)
26
0 + 1
1 (Ace)
2 + 1
3 (Hearts)





30
4 + 1
5
2 + 1
3 (Hearts)
31
5 + 1
6
3 + 1
4 (Spades)
32
6 + 1
7
0 + 1
1 (Clubs)





51
12 + 1
13 (King)
3 + 1
4 (Spades)
52
0 + 1
1 (Ace)
0 + 1
3 (Clubs)

Shuffling the Deck

With each shuffle, 52 swaps are made. i1 and i2 represent the two cards to be swapped. In order to complete the swap, a temporary holder must be used. The contents of array n(i1) is placed in the temporary holder h2. The contents of n(i2) is then copied to n(i1). Because n(i1) no longer holds its original value, the contents of the temporary holder h2 is copied to n(i2).
The number of times these 52 swaps are made, or the number of shuffles, is determined by the variable numShuffles.
for i = 1 to 52
    i1 = int(rnd(1)*52) + 1
    i2 = int(rnd(1)*52) + 1
    h2 = n(i1)
    n(i1) = n(i2)
    n(i2) = h2
next i

The Code

The full code to simulate shuffling a deck of cards
' ---------------------------------
' Card deck
' ---------------------------------
dim suite$(4)
suite$(1) = "Clubs"
suite$(2) = "Diamonds"
suite$(3) = "Hearts"
suite$(4) = "Spades"
 
dim card$(13)
card$(1) = "Ace"
card$(2) = "2"
card$(3) = "3"
card$(4) = "4"
card$(5) = "5"
card$(6) = "6"
card$(7) = "7"
card$(8) = "8"
card$(9) = "9"
card$(10) = "10"
card$(11) = "Jack"
card$(12) = "Queen"
card$(13) = "King"
 
dim n(55)
' ordered deck
for i = 1 to 52
    n(i) = i
next i
 
' shuffle deck
numShuffles = 3
for j = 1 to numShuffles
    gosub [shuffle]
next j
 
print "======= Shuffled Deck =================="
for i = 1 to 52
    s = (n(i) mod 4) + 1
    c = (n(i) mod 13) + 1
    print i;" ";n(i);" c:";c;" s:";s;" ";chr$(9);card$(c);" of ";suite$(s)
next i
wait
 
[shuffle]
for i = 1 to 52
    i1 = int(rnd(1)*52) + 1
    i2 = int(rnd(1)*52) + 1
    h2 = n(i1)
    n(i1) = n(i2)
    n(i2) = h2
next i
RETURN



Thanks to the fine people at the Run BASIC Forum for continuing to submit good stuff.