home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / MM1 / SOUNDUTILS / mm1_tracker.lzh / TRACKER4.6 / randomize.c < prev    next >
C/C++ Source or Header  |  1994-11-26  |  2KB  |  79 lines

  1. /* randomize.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: randomize.c,v 4.3 1994/11/15 16:11:01 espie Exp espie $ 
  6.  * $Log: randomize.c,v $
  7.  * Revision 4.3  1994/11/15  16:11:01  espie
  8.  * *** empty log message ***
  9.  *
  10.  */
  11.  
  12. /* input: a series of names (as argv[1:argc - 1])
  13.  * output: the same names, in a random order.
  14.  * with the new database lookup facility, very useful for e.g.,
  15.  * tracker `randomize *` (jukebox)
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include "defs.h"
  21.  
  22. ID("$Id: randomize.c,v 4.3 1994/11/15 16:11:01 espie Exp espie $")
  23.  
  24. /* n = random_range(max): output a number in the range 0:max - 1.
  25.  * For our purpose, we don't have to get a very random number,
  26.  * so the standard generator is alright.
  27.  */
  28. int random_range(max)
  29. int max;
  30.     {
  31.     static init = 0;
  32.  
  33.         /* initialize the generator to an appropriate seed eventually */
  34.     if (!init)
  35.         {
  36.         srand(time(0));
  37.         init = 1;
  38.         }
  39.     return rand()%max;
  40.     }
  41.  
  42. /* output(s): output s in a suitable format. Ideally, output() should use
  43.  * the shell quoting conventions for difficult names. Right now, it doesn't
  44.  */
  45. void output(s)
  46. char *s;
  47.     {
  48.     for(; *s; s++)
  49.         switch(*s)
  50.             {
  51.     /*    case ' ':
  52.         case '(':
  53.         case ')':
  54.         case '\\':
  55.             putchar('\\');
  56.             */
  57.         default:
  58.             putchar(*s);
  59.             }
  60.     putchar(' ');
  61.     }
  62.  
  63. int main(argc, argv)
  64. int argc;
  65. char *argv[];
  66.     {
  67.     int i, k;
  68.  
  69.         /* set up everything so that our names are in argv[0 : argc - 2] */
  70.     for (i = argc - 1, argv++; i; i--)
  71.         {
  72.             /* invariant: the remaining names are in argv[0: i - 1] */
  73.         k = random_range(i);
  74.         output(argv[k]);
  75.         argv[k] = argv[i - 1];
  76.         }
  77.    exit(0);
  78.     }
  79.