home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1807 / rand48.c
Encoding:
C/C++ Source or Header  |  1990-12-28  |  1.1 KB  |  67 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 atof();
  18. extern double pow();
  19.  
  20.     /*  The command mainline. */
  21.  
  22. main(argc, argv)
  23. int argc;
  24. char **argv;
  25. {
  26.     int c;
  27.     int errflg = 0;
  28.  
  29.     extern char *optarg;
  30.     extern int optind;
  31.  
  32.     double weighting = 1.0;
  33.     long count = 1;
  34.     long scale = 2;
  35.  
  36.     /*  Collect option switches  */
  37.  
  38.     while ((c=getopt(argc, argv, "s:w:?")) != -1)
  39.         switch (c) {
  40.         case 's':
  41.             scale = atol(optarg);
  42.             break;
  43.         case 'w':
  44.             weighting = atof(optarg);
  45.             break;
  46.         default:
  47.             errflg++;
  48.         }
  49.  
  50.     /*  Validate args and print usage message if bad  */
  51.  
  52.     if ((argc - optind) > 1)
  53.         errflg++;
  54.     else if ((argc - optind) == 1)
  55.         count = atol(argv[optind]);
  56.  
  57.     if (errflg) {
  58.         fprintf(stderr, "Usage: %s [-s scale] [-w weight] [count]\n", argv[0]);
  59.         exit(1);
  60.     }
  61.  
  62.     srand48(time(0)+getpid());
  63.  
  64.     while (count--)
  65.         printf("%i\n", (long) (scale * pow(drand48(), weighting)));
  66. }
  67.