home *** CD-ROM | disk | FTP | other *** search
- /****
- *
- * rand48 - improved random(1) using System V drand48(3).
- *
- * Uses pow() from libm.a; also getopt.
- *
- * Build with
- * cc -o rand48 -O rand48.c -lm
- *
- * Public domain. September 1990.
- *
- ****/
-
- #include <stdio.h>
-
- extern double drand48();
- extern double atof();
- extern double pow();
-
- /* The command mainline. */
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- int c;
- int errflg = 0;
-
- extern char *optarg;
- extern int optind;
-
- double weighting = 1.0;
- long count = 1;
- long scale = 2;
-
- /* Collect option switches */
-
- while ((c=getopt(argc, argv, "s:w:?")) != -1)
- switch (c) {
- case 's':
- scale = atol(optarg);
- break;
- case 'w':
- weighting = atof(optarg);
- break;
- default:
- errflg++;
- }
-
- /* Validate args and print usage message if bad */
-
- if ((argc - optind) > 1)
- errflg++;
- else if ((argc - optind) == 1)
- count = atol(argv[optind]);
-
- if (errflg) {
- fprintf(stderr, "Usage: %s [-s scale] [-w weight] [count]\n", argv[0]);
- exit(1);
- }
-
- srand48(time(0)+getpid());
-
- while (count--)
- printf("%i\n", (long) (scale * pow(drand48(), weighting)));
- }
-