home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol8n20.zip / SHUFFLE.C < prev    next >
Text File  |  1989-10-18  |  3KB  |  81 lines

  1.  
  2.  
  3. /* SHUFFLE.C - rearranges a given set of specific objects - in this
  4.                case a deck of playing cards - into random order. This 
  5.                illustrates the proper use of the subroutine shuffle().
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11.  
  12. #define N 52        // number of objects in the set - 52 for a deck of cards
  13. #define NMAX 100    /* maximum number that shuffle() can rearrange: must be
  14.                        greater than or equal to N */
  15.  
  16. typedef struct {
  17.                 int word1;
  18.                 int word2;
  19.                 } dword;     // structure needed by shuffle()
  20.  
  21. char *suit[4] = {"spades", "hearts", "clubs", "diamonds"};
  22. char *value[13] = {"ace", "two", "three", "four", "five", "six", "seven",
  23.                    "eight", "nine", "ten", "jack", "queen", "king"};
  24.  
  25. void shuffle(int, int *, unsigned int);      // function prototypes
  26. int compare(dword *, dword *);
  27.  
  28. main()
  29. {
  30.      int i;
  31.      int a[N];           // one-dimensional array to be shuffled
  32.      unsigned int see;   // arbitrary integer to seed rand()
  33.      long tval;
  34.  
  35.      seed = (unsigned int) (time(&tval) % 65536); // random seed based on time
  36.      shuffle(N, a, seed);
  37.      for(i=0; i<N; i++)
  38.           printf("\n%s of %s", value[a[i] % 13], suit[a[i]/13]);
  39. }
  40.  
  41.  
  42. /* shuffle() - This is the heart of the program. It assigns a one-dimensional
  43.                aray of integers, whose beginning address is base, all the
  44.                values from 0 to N-1 in random order. The seed is arbitrary 
  45.                and is used only to have repeated calls to shufle() return
  46.                different sequences.
  47. */
  48.  
  49. void shuffle(int n, int *base, unsigned int seed)
  50. {
  51.      int i;
  52.      dword RandomIndex[NMAX];     /* "dword" structure randomizing vehicle.
  53.                                      NMAX is the maximum possible number of
  54.                                      objects in the set, and must be
  55.                                      initialized by the user. */
  56.  
  57.      srand(seed);                 // initialize rand()
  58.      for(i=0; i<n; i+)            // initialize RandomIndex
  59.           {
  60.             RandomIndex[i].word1 = rand();  // first part is a random number
  61.             RandomIndex[i].word2 = i;       // second starts as a
  62.            }
  63.  
  64.      qsort(RandomIndex, n, length, compare); /* sort first part only, but
  65.                                                 "drag" second parts along */
  66.  
  67.      for(i=0; i<n; i++)
  68.           *base+i) = Randomndex[i].word2;    /* assign array to newly
  69.                                                 randomized second parts */
  70. }
  71.  
  72.  
  73. /* compare() - routine needed by qsort() to compare the values of
  74.                dwords according to their first parts only */
  75.  
  76. compare(dword *elem1, dword *elem2)
  77. {
  78.      return(elem1->word1 - elem2->word1);
  79. }
  80.  
  81.