commit 487db706cdb3ec0cbd23efe3cade7409e2cc08c3
parent 8b5e9b0cf97126537c548e49329ead6bad47c105
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Mon, 9 May 2022 17:26:24 +0200
stddev.1, stdvar.1: make synopsis more precise
Diffstat:
M | Makefile | | | 2 | ++ |
A | randnum.1 | | | 79 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | randnum.c | | | 76 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | stddev.1 | | | 2 | +- |
M | stdvar.1 | | | 2 | +- |
5 files changed, 159 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -13,6 +13,7 @@ BIN = \
mean \
min \
randcounts \
+ randnum \
range \
rangetest \
stddev \
@@ -59,6 +60,7 @@ max: max.o
mean: mean.o
min: min.o
randcounts: randcounts.o
+randnum: randnum.o
range: range.o
rangetest: rangetest.o
stddev: stddev.o
diff --git a/randnum.1 b/randnum.1
@@ -0,0 +1,79 @@
+.Dd $Mdocdate$
+.Dt RANDNUM 1
+.Os
+.Sh NAME
+.Nm randnum
+.Nd produces random numbers in a range
+.Sh SYNOPSIS
+.Nm
+.Op Fl f Ar fmtstr
+.Op Fl h
+.Op Fl n Ar num
+.Op Fl s Ar seed
+.Oo
+.Op Ar min_val
+.Ar max_val
+.Oc
+.Sh DESCRIPTION
+.Nm
+randomly generates evenly distributed double-precision numbers, by in
+the range
+.Ar [ min_val ;
+.Ar max_val ].
+If
+.Ar max_val
+is not specified, it is 1.
+If
+.Ar min_val
+is not specified, it is 0.
+If
+.Ar min_val
+and/or
+.Ar max_val
+are negative, they must be prefixed with the '--' option.
+.Pp
+The number values are drawn from a pseudo-random number generator,
+seeded from current time of day with microsecond resolution.
+Invokations of
+.Nm
+within the same microsecond will produce the same result.
+.Pp
+The options are as follows:
+.Bl -tag -width Ds
+.It Fl f Ar fmtstr
+Formatting string to use as documented in
+.Xr printf 3 .
+When including a format specifier (%..), only use forms that are
+compatible with
+.Vt double
+types.
+The default format string is '%.17g\n'.
+.It Fl h
+Show usage information.
+.It Fl n Ar num
+Number of random points to generate.
+The default is 1.
+.It Fl s Ar seed
+Seed the pseudo-random number generator with this value, which is used
+to generate reproducable binning.
+.El
+.Sh EXAMPLES
+Generate a single value in the default range [0;1[.
+Due to the randomness, your output may differ:
+.Dl $ randnum
+.Dl 0.38385568287140259
+.Pp
+Generate five points in the range [-10;10[ and print with three significant digits:
+.Dl $ randnum -n 5 -f '%.3g\n' -- -10 10
+.Dl -4.16
+.Dl -3.36
+.Dl -5.8
+.Dl -2.31
+.Dl 4.4
+.Sh SEE ALSO
+.Xr randcounts 1 ,
+.Xr range 1 ,
+.Xr rangetest 1 ,
+.Xr sum 1
+.Sh AUTHORS
+.An Anders Damsgaard Aq Mt anders@adamsgaard.dk
diff --git a/randnum.c b/randnum.c
@@ -0,0 +1,76 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <err.h>
+#include <limits.h>
+#include <string.h>
+#include <math.h>
+#include <sys/time.h>
+
+#include "arg.h"
+#include "util.h"
+
+char *argv0;
+
+static void
+usage(void)
+{
+ errx(1, "usage: %s [-f fmtstr] [-h] [-n num] [-s seed] "
+ "[[min_val] max_val]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int i, ret, s = 0;
+ long j, seed, n = 1;
+ double val, minv = 0.0, maxv = 1.0;
+ char fmtstr[PATH_MAX] = "%.17g\n";
+ struct timeval t1;
+
+ if (pledge("stdio", NULL) == -1)
+ err(2, "pledge");
+
+ ARGBEGIN {
+ case 'f':
+ ret = snprintf(fmtstr, sizeof(fmtstr), "%s", EARGF(usage()));
+ if (ret < 0 || (size_t)ret >= sizeof(fmtstr))
+ errx(1, "%s: could not write fmtstr", __func__);
+ break;
+ case 'h':
+ usage();
+ break;
+ case 'n':
+ n = atoi(EARGF(usage()));
+ break;
+ case 's':
+ s = 1;
+ seed = atol(EARGF(usage()));
+ break;
+ default:
+ usage();
+ } ARGEND;
+
+ if (argc > 2)
+ usage();
+ else if (argc == 2) {
+ minv = atof(argv[0]);
+ maxv = atof(argv[1]);
+ } else if (argc == 1)
+ maxv = atof(argv[0]);
+
+ if (s)
+#ifdef __OpenBSD__
+ srand48_deterministic(seed);
+#else
+ srand48(seed);
+ else {
+ gettimeofday(&t1, NULL);
+ srand48(t1.tv_sec * t1.tv_usec); /* Caveat: identical seed for each ms */
+ }
+#endif
+
+ for (i = 0; i < n; i++)
+ printf(fmtstr, drand48() * (maxv - minv) + minv);
+
+ return 0;
+}
diff --git a/stddev.1 b/stddev.1
@@ -3,7 +3,7 @@
.Os
.Sh NAME
.Nm stddev
-.Nd returns the standard deviation for each column
+.Nd returns the standard deviation for each input column
.Sh SYNOPSIS
.Nm
.Op Fl f Ar fmtstr
diff --git a/stdvar.1 b/stdvar.1
@@ -3,7 +3,7 @@
.Os
.Sh NAME
.Nm stdvar
-.Nd returns the standard variance for each column
+.Nd returns the standard variance for each input column
.Sh SYNOPSIS
.Nm
.Op Fl f Ar fmtstr