SHUFFLE2.PAS                        COMPLETE LISTING


PROGRAM demonstrate_shuffling;
VAR
  deck           : ARRAY[1..52] OF Integer; {or of anything else}
  saved, i, j    : 1..52;

BEGIN
  {place initial values in array}
  FOR i := 1 TO 52 DO
    deck[i] := i;

  {seed random number generator}
  Randomize;

  {here's the guts of it}

   {shuffle the array - pass *once* through the array, swapping each element
    with another, randomly chosen, element}

  FOR i := 52 DOWNTO 2 DO
    BEGIN
      {swap element in ith place with any
       element at or below that place}
      j := Random(i)+1;
      saved := deck[i];
      deck[i] := deck[j];
      deck[j] := saved
    END;

  {output for testing purposes}
  FOR i := 1 TO 52 DO
    BEGIN
      Write(deck[i]:3);
      IF i MOD 10 = 0 THEN WriteLn;
    END;
  WriteLn;

END.
