From a26a66124e2aab78e90cbd62c66518d7e4a6a3db Mon Sep 17 00:00:00 2001 From: Diomidis Spinellis Date: Mon, 6 Nov 2006 10:30:29 +0000 Subject: [PATCH] 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 --- usr.bin/jot/jot.1 | 6 +++++- 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 97cd8f304d5f..8ba90a2873d4 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 70003cf4527b..bb3073bba105 100644 --- a/usr.bin/jot/jot.c +++ b/usr.bin/jot/jot.c @@ -57,6 +57,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -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"); }