home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / byteunix.lzh / byte.1 / mkperm.c < prev    next >
C/C++ Source or Header  |  1990-05-11  |  1KB  |  57 lines

  1. /*******************************************************************************
  2.  *  The BYTE UNIX Benchmarks - Release 2
  3.  *          Module: mkperm.c   SID: 2.4 4/17/90 16:45:35
  4.  *          
  5.  *******************************************************************************
  6.  * Bug reports, patches, comments, suggestions should be sent to:
  7.  *
  8.  *    Ben Smith or Rick Grehan at BYTE Magazine
  9.  *    bensmith@bixpb.UUCP    rick_g@bixpb.UUCP
  10.  *
  11.  *******************************************************************************
  12.  *  Modification Log:
  13.  *  $Header: mkperm.c,v 1.2 87/06/22 14:32:26 kjmcdonell Beta $
  14.  *
  15.  ******************************************************************************/
  16. char SCCSid[] = "@(#) @(#)mkperm.c:2.4 -- 4/17/90 16:45:35";
  17. #include <stdio.h>
  18. #ifndef lint
  19. #endif
  20.  
  21. /* define rand and srand if you have random() from USENET */
  22. /* #define rand random */
  23. /* #define srand srandom */
  24.  
  25. main(argc, argv)
  26. int    argc;
  27. char    *argv[];
  28. {
  29.     int    n;    /* generate a permutation of {1,2,3,...,n} */
  30.     int    i;
  31.     int    t;
  32.     char    *mask;
  33.  
  34.     if (argc > 2 && strcmp(argv[1], "-s") == 0) {
  35.         t = atoi(argv[2]);
  36.         if (t < 16)
  37.             t = 1 << t;
  38.         srand(t);
  39.         argv++;
  40.         argv++;
  41.     }
  42.     n = atoi(argv[1]);
  43.     mask = (char *)malloc(n);
  44.  
  45.     for (i=0; i<n; i++)
  46.         mask[i] = '\0';
  47.     for (i=0; i<n; i++) {
  48.         do {
  49.             t = rand() % n;
  50.         } while (mask[t]);
  51.         mask[t] = '\1';
  52.         printf("%d ", t+1);
  53.     }
  54.     putchar('\n');
  55.     exit(0);
  56. }
  57.