home *** CD-ROM | disk | FTP | other *** search
/ Lion Share / lionsharecd.iso / utils_mz / v10n14.zip / SHUFFLE2.PAS < prev    next >
Pascal/Delphi Source File  |  1991-07-02  |  874b  |  41 lines

  1. SHUFFLE2.PAS                        COMPLETE LISTING
  2.  
  3.  
  4. PROGRAM demonstrate_shuffling;
  5. VAR
  6.   deck           : ARRAY[1..52] OF Integer; {or of anything else}
  7.   saved, i, j    : 1..52;
  8.  
  9. BEGIN
  10.   {place initial values in array}
  11.   FOR i := 1 TO 52 DO
  12.     deck[i] := i;
  13.  
  14.   {seed random number generator}
  15.   Randomize;
  16.  
  17.   {here's the guts of it}
  18.  
  19.    {shuffle the array - pass *once* through the array, swapping each element
  20.     with another, randomly chosen, element}
  21.  
  22.   FOR i := 52 DOWNTO 2 DO
  23.     BEGIN
  24.       {swap element in ith place with any
  25.        element at or below that place}
  26.       j := Random(i)+1;
  27.       saved := deck[i];
  28.       deck[i] := deck[j];
  29.       deck[j] := saved
  30.     END;
  31.  
  32.   {output for testing purposes}
  33.   FOR i := 1 TO 52 DO
  34.     BEGIN
  35.       Write(deck[i]:3);
  36.       IF i MOD 10 = 0 THEN WriteLn;
  37.     END;
  38.   WriteLn;
  39.  
  40. END.
  41.