diff options
author | dds <dds@FreeBSD.org> | 2006-11-06 10:30:29 +0000 |
---|---|---|
committer | dds <dds@FreeBSD.org> | 2006-11-06 10:30:29 +0000 |
commit | f4df3d376b390d38cec1fc4e4eb4d5f3b8254d85 (patch) | |
tree | 3562aeff409349f9e9b5c31d99ff32bc1e144e2b /usr.bin/jot | |
parent | e9549fc27653ea1b3477df4090e58596a0bbfe6c (diff) | |
download | FreeBSD-src-f4df3d376b390d38cec1fc4e4eb4d5f3b8254d85.zip FreeBSD-src-f4df3d376b390d38cec1fc4e4eb4d5f3b8254d85.tar.gz |
Restore jot's ability to use a seed for producing a deterministic
sequence of random numbers.
This functionality was lost in revision 1.9 when the random number
generator was switched to arc4random.
PR: docs/54879
MFC after: 2 weeks
Diffstat (limited to 'usr.bin/jot')
-rw-r--r-- | usr.bin/jot/jot.1 | 6 | ||||
-rw-r--r-- | usr.bin/jot/jot.c | 19 |
2 files changed, 18 insertions, 7 deletions
diff --git a/usr.bin/jot/jot.1 b/usr.bin/jot/jot.1 index 97cd8f3..8ba90a2 100644 --- a/usr.bin/jot/jot.1 +++ b/usr.bin/jot/jot.1 @@ -139,7 +139,11 @@ representing the corresponding value in The last argument must be a real number. .Pp Random numbers are obtained through -.Xr random 3 . +.Xr arc4random 3 +when no seed is specified, +and through +.Xr random 3 +when a seed is given. The name .Nm derives in part from diff --git a/usr.bin/jot/jot.c b/usr.bin/jot/jot.c index 70003cf..bb3073b 100644 --- a/usr.bin/jot/jot.c +++ b/usr.bin/jot/jot.c @@ -57,6 +57,7 @@ __FBSDID("$FreeBSD$"); #include <stdio.h> #include <stdint.h> #include <stdlib.h> +#include <stdbool.h> #include <string.h> #include <time.h> #include <unistd.h> @@ -102,6 +103,7 @@ main(int argc, char **argv) unsigned int mask = 0; int n = 0; int ch; + bool have_seed = false; while ((ch = getopt(argc, argv, "rb:w:cs:np:")) != -1) switch (ch) { @@ -142,6 +144,8 @@ main(int argc, char **argv) if (!sscanf(argv[3], "%lf", &s)) errx(1, "bad s value: %s", argv[3]); mask |= HAVE_STEP; + if (randomize) + have_seed = true; } /* FALLTHROUGH */ case 3: @@ -206,7 +210,7 @@ main(int argc, char **argv) mask |= HAVE_BEGIN; break; case HAVE_REPS | HAVE_ENDER: - s = (randomize ? time(NULL) : STEP_DEF); + s = STEP_DEF; mask = HAVE_REPS | HAVE_ENDER | HAVE_STEP; break; case HAVE_REPS | HAVE_ENDER | HAVE_STEP: @@ -218,7 +222,7 @@ main(int argc, char **argv) mask = 0; break; case HAVE_REPS | HAVE_BEGIN: - s = (randomize ? -1.0 : STEP_DEF); + s = STEP_DEF; mask = HAVE_REPS | HAVE_BEGIN | HAVE_STEP; break; case HAVE_REPS | HAVE_BEGIN | HAVE_STEP: @@ -229,9 +233,7 @@ main(int argc, char **argv) mask = 0; break; case HAVE_REPS | HAVE_BEGIN | HAVE_ENDER: - if (randomize) - s = -1.0; - else if (reps == 0) + if (reps == 0) errx(1, "infinite sequences cannot be bounded"); else if (reps == 1) s = 0.0; @@ -257,8 +259,13 @@ main(int argc, char **argv) infinity = 1; if (randomize) { x = (ender - begin) * (ender > begin ? 1 : -1); + if (have_seed) + srandom((unsigned long)s); for (i = 1; i <= reps || infinity; i++) { - y = arc4random() / ((double)UINT32_MAX + 1); + if (have_seed) + y = random() / ((double)LONG_MAX + 1); + else + y = arc4random() / ((double)UINT32_MAX + 1); if (putdata(y * x + begin, reps - i)) errx(1, "range error in conversion"); } |