Classic Computer Magazine Archive COMPUTE! ISSUE 37 / JUNE 1983 / PAGE 223

Atari
Fast Shuffle
James E Korenthal

What's all this fuss about rearranging a few numbers? I've seen so many articles in various magazines about shuffling numbers in increasingly exotic ways that I'm thoroughly mixed up.
    Here's the technique that I've been using for years. It's simple and fast, and it gives you an equal probability of any given permutation showing up. The program is written in Atari BASIC, but will work on, or can easily be converted to, any other BASIC. It's set up to shuffle 52 cards, represented as numbers from 0-51 in an array called DECK (with subscripts running from 0-51).

10 REM INITIALIZE
20 N=51:DIM DECK(N):REM SHUFFLE N+1 N
   UMBERS
30 FOR J=0 TO N:DECK(J)=J:NEXT J:REM
   FILL THE DECK
40 REM SHUFFLE THE DECK
50 FOR J=N TO 1 STEP -1:REM LOOP BACK
   WARDS THROUGH DECK
60 K=INT(RND(0)*(J+1)):REM PICK POSIT
   ION TO SWAP
70 TEMP=DECK(J):DECK(J)=DECK(K):DECK(
   K)=TEMP
80 NEXT J:REM AND THAT'S ALL THERE IS
    TO IT!
90 FOR J=0 TO N:PRINT DECK(J);" ";:NE
   XT J

    You can easily set up the shuffling loop as a one-line subroutine, and then use a GOSUB when it's time to shuffle. Also, as long as you haven't changed the numbers in the array to be shuffled, you don't have to reinitialize (line 30 in the program) before shuffling.