Better SysV random()
Tom Neff
tneff at bfmny0.BFM.COM
Sun Sep 9 20:28:06 AEST 1990
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 <stdio.h>
X
Xextern double drand48();
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
More information about the Alt.sources
mailing list