commit 5ef01afdfb77b64891f0e5983f431e552f94caea
parent 8c9298b2b8694b8b32fa5c182299a6eab7706ebc
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Thu, 5 May 2022 11:28:20 +0200
remove stray argument in synopsis
Diffstat:
A | randcounts.c | | | 100 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
D | randnum.c | | | 71 | ----------------------------------------------------------------------- |
M | range.1 | | | 1 | - |
3 files changed, 100 insertions(+), 72 deletions(-)
diff --git a/randcounts.c b/randcounts.c
@@ -0,0 +1,100 @@
+#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 [-h] [-n num] [-s seed] "
+ "weight1 [weight2 ...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int i, ret, s = 0, N;
+ long j, seed, *counts = NULL, n = 1;
+ double val = 0.0, weightsum = 0.0, *weights = NULL;
+ struct timeval t1;
+
+ if (pledge("stdio", NULL) == -1)
+ err(2, "pledge");
+
+ ARGBEGIN {
+ case 'h':
+ usage();
+ break;
+ case 'n':
+ n = atol(EARGF(usage()));
+ break;
+ case 's':
+ s = 1;
+ seed = atol(EARGF(usage()));
+ break;
+ default:
+ usage();
+ } ARGEND;
+
+ if (argc < 1)
+ usage();
+
+ N = argc;
+ if (!(weights = calloc(N, sizeof(double))) ||
+ !(counts = calloc(N, sizeof(long))))
+ err(1, "calloc");
+
+ for (i = 0; i < N; i++) {
+ counts[i] = 0;
+ weights[i] = atof(argv[i]);
+ if (weights[i] <= 0.0)
+ errx(1, "weight %d is not positive (%g)", i, weights[i]);
+ if (weights[i] > 1.0)
+ errx(1, "weight %d is greater than 1 (%g)", i, weights[i]);
+ }
+
+ for (i = 0; i < N; i++)
+ weightsum += weights[i];
+ if (fabs(weightsum - 1.0) > 1e-3)
+ errx(1, "weights do not sum to 1 (%g)", weightsum);
+
+ if (s)
+ srand48(seed);
+ else {
+ gettimeofday(&t1, NULL);
+ srand48(t1.tv_sec * t1.tv_usec); /* Caveat: identical seed for each ms */
+ }
+
+ for (j = 0; j < n; j++) {
+ val = drand48();
+ weightsum = 0.0;
+ for (i = 0; i < N; i++) {
+ weightsum += weights[i];
+ if (val <= weightsum) {
+ counts[i]++;
+ break;
+ }
+ }
+ }
+
+ for (i = 0; i < N; i++) {
+ printf("%ld", counts[i]);
+ if (i < N - 1)
+ putchar('\t');
+ else
+ putchar('\n');
+ }
+
+ free(counts);
+ free(weights);
+
+ return 0;
+}
diff --git a/randnum.c b/randnum.c
@@ -1,71 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <err.h>
-#include <limits.h>
-#include <string.h>
-#include <math.h>
-
-#include "arg.h"
-#include "util.h"
-
-char *argv0;
-
-static void
-usage(void)
-{
- errx(1, "usage: %s [-f fmtstr] [-h] [-n num] [-s] "
- "val1 [val2 ...]\n", argv0);
-}
-
-int
-main(int argc, char *argv[])
-{
- int i, ret, n = 1, N;
- double val = 0.0, *weights = NULL;
- char fmtstr[PATH_MAX] = "%g\n";
-
- 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;
- default:
- usage();
- } ARGEND;
-
- if (argc < 1)
- usage();
-
- N = argc;
- if (!(weights = calloc(N, sizeof(double))))
- err(1, "calloc");
-
- for (i = 0; i < N; i++) {
- weights[i] = atof(argv[i]);
- if (weights[i] <= 0.0)
- errx(1, "weight %d is not positive (%g)", i, weights[i]);
- if (weights[i] > 1.0)
- errx(1, "weight %d is greater than 1 (%g)", i, weights[i]);
- }
- val = 0.0;
- for (i = 0; i < N; i++)
- val += weights[i];
- if (fabs(val - 1.0) > 1e-3)
- errx(1, "weights do not sum to 1 (%g)", val);
-
-
-
- free(weights);
-
- return 0;
-}
diff --git a/range.1 b/range.1
@@ -6,7 +6,6 @@
.Nd generates an evenly spaced vector over a range
.Sh SYNOPSIS
.Nm
-.Ar cmd
.Op Fl b
.Op Fl e
.Op Fl f Ar fmtstr