Path: utzoo!utgpu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!samsung!uunet!bfmny0!tneff From: tneff@bfmny0.BFM.COM (Tom Neff) Newsgroups: alt.sources Subject: Repost - Better SysV random() Summary: uses drand48 facility Keywords: sysv random rand48 Message-ID: <15839@bfmny0.BFM.COM> Date: 10 Sep 90 06:54:05 GMT Reply-To: tneff@bfmny0.BFM.COM (Tom Neff) Followup-To: alt.sources.d Lines: 87 Supersedes: <15838@bfmny0.BFM.COM> Archive-name: AAAAArand48 [ Somehow a line got dropped last time -- try again. TMN ] I needed this. Here it is. #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create: # rand48.c # This archive created: Sun Sep 9 07:26:46 1990 export PATH; PATH=/bin:/usr/bin:$PATH if test -f 'rand48.c' then echo shar: "will not over-write existing file 'rand48.c'" else sed 's/^X//' << \SHAR_EOF > 'rand48.c' X/**** X * X * rand48 - improved random(1) using System V drand48(3). X * X * Uses pow() from libm.a; also getopt. X * X * Build with X * cc -o rand48 -O rand48.c -lm X * X * Public domain. September 1990. X * X ****/ X X#include X Xextern double drand48(); Xextern double atof(); Xextern double pow(); X X /* The command mainline. */ X Xmain(argc, argv) Xint argc; Xchar **argv; X{ X int c; X int errflg = 0; X X extern char *optarg; X extern int optind; X X double weighting = 1.0; X long count = 1; X long scale = 2; X X /* Collect option switches */ X X while ((c=getopt(argc, argv, "s:w:?")) != -1) X switch (c) { X case 's': X scale = atol(optarg); X break; X case 'w': X weighting = atof(optarg); X break; X default: X errflg++; X } X X /* Validate args and print usage message if bad */ X X if ((argc - optind) > 1) X errflg++; X else if ((argc - optind) == 1) X count = atol(argv[optind]); X X if (errflg) { X fprintf(stderr, "Usage: %s [-s scale] [-w weight] [count]\n", argv[0]); X exit(1); X } X X srand48(time(0)+getpid()); X X while (count--) X printf("%i\n", (long) (scale * pow(drand48(), weighting))); X} SHAR_EOF fi exit 0 # End of shell archive