home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1797 / rand48.c
Encoding:
C/C++ Source or Header  |  1990-12-28  |  1.0 KB  |  66 lines

  1. /****
  2.  *
  3.  * rand48 - improved random(1) using System V drand48(3).
  4.  *
  5.  * Uses pow() from libm.a; also getopt.
  6.  *
  7.  * Build with
  8.  *   cc -o rand48 -O rand48.c -lm
  9.  *
  10.  * Public domain.    September 1990.
  11.  * 
  12.  ****/
  13.  
  14. #include <stdio.h>
  15.  
  16. extern double drand48();
  17. extern double pow();
  18.  
  19.     /*  The command mainline. */
  20.  
  21. main(argc, argv)
  22. int argc;
  23. char **argv;
  24. {
  25.     int c;
  26.     int errflg = 0;
  27.  
  28.     extern char *optarg;
  29.     extern int optind;
  30.  
  31.     double weighting = 1.0;
  32.     long count = 1;
  33.     long scale = 2;
  34.  
  35.     /*  Collect option switches  */
  36.  
  37.     while ((c=getopt(argc, argv, "s:w:?")) != -1)
  38.         switch (c) {
  39.         case 's':
  40.             scale = atol(optarg);
  41.             break;
  42.         case 'w':
  43.             weighting = atof(optarg);
  44.             break;
  45.         default:
  46.             errflg++;
  47.         }
  48.  
  49.     /*  Validate args and print usage message if bad  */
  50.  
  51.     if ((argc - optind) > 1)
  52.         errflg++;
  53.     else if ((argc - optind) == 1)
  54.         count = atol(argv[optind]);
  55.  
  56.     if (errflg) {
  57.         fprintf(stderr, "Usage: %s [-s scale] [-w weight] [count]\n", argv[0]);
  58.         exit(1);
  59.     }
  60.  
  61.     srand48(time(0)+getpid());
  62.  
  63.     while (count--)
  64.         printf("%i\n", (long) (scale * pow(drand48(), weighting)));
  65. }
  66.