From c7b8411cc9e09b1a9879a0bca6a39b55de7247d8 Mon Sep 17 00:00:00 2001 From: Conrad Meyer Date: Fri, 13 Dec 2019 04:37:39 +0000 Subject: [PATCH] random(6): produce random results This program is trash and there's no reason to keep it in base. But as long as we're shipping a silly program named 'random', let's actually make it random. --- usr.bin/random/random.6 | 7 ++----- usr.bin/random/random.c | 10 ++++------ usr.bin/random/randomize_fd.c | 8 ++++---- usr.bin/random/randomize_fd.h | 6 ------ 4 files changed, 10 insertions(+), 21 deletions(-) diff --git a/usr.bin/random/random.6 b/usr.bin/random/random.6 index bd38ba628ebb..75fa7a378d3d 100644 --- a/usr.bin/random/random.6 +++ b/usr.bin/random/random.6 @@ -28,7 +28,7 @@ .\" @(#)random.6 8.2 (Berkeley) 3/31/94 .\" $FreeBSD$ .\" -.Dd February 8, 2003 +.Dd December 12, 2019 .Dt RANDOM 6 .Os .Sh NAME @@ -62,9 +62,7 @@ space characters as determined by The default .Ar denominator for this mode of operation is 1, which gives each line a chance to be -displayed, but in a -.Xr random 3 -order. +displayed, but in a random order. .Pp The options are as follows: .Bl -tag -width Ds @@ -112,7 +110,6 @@ Randomize words separated by instead of newlines. .El .Sh SEE ALSO -.Xr random 3 , .Xr fortune 6 .Sh HISTORY The diff --git a/usr.bin/random/random.c b/usr.bin/random/random.c index 99f9d900f1b8..9f78a2711cc9 100644 --- a/usr.bin/random/random.c +++ b/usr.bin/random/random.c @@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -135,8 +136,6 @@ main(int argc, char *argv[]) /* NOTREACHED */ } - srandomdev(); - /* * Act as a filter, randomly choosing lines of the standard input * to write to the standard output. @@ -158,7 +157,7 @@ main(int argc, char *argv[]) /* Compute a random exit status between 0 and denom - 1. */ if (random_exit) - return (int)(denom * random() / RANDOM_MAX_PLUS1); + return (arc4random_uniform(denom)); /* * Select whether to print the first line. (Prime the pump.) @@ -166,7 +165,7 @@ main(int argc, char *argv[]) * 0 (which has a 1 / denom chance of being true), we select the * line. */ - selected = (int)(denom * random() / RANDOM_MAX_PLUS1) == 0; + selected = (arc4random_uniform(denom) == 0); while ((ch = getchar()) != EOF) { if (selected) (void)putchar(ch); @@ -176,8 +175,7 @@ main(int argc, char *argv[]) err(2, "stdout"); /* Now see if the next line is to be printed. */ - selected = (int)(denom * random() / - RANDOM_MAX_PLUS1) == 0; + selected = (arc4random_uniform(denom) == 0); } } if (ferror(stdin)) diff --git a/usr.bin/random/randomize_fd.c b/usr.bin/random/randomize_fd.c index 684c84ec58e4..7ee41cd99bc9 100644 --- a/usr.bin/random/randomize_fd.c +++ b/usr.bin/random/randomize_fd.c @@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -175,7 +176,7 @@ randomize_fd(int fd, int type, int unique, double denom) (type == RANDOM_TYPE_WORDS && isspace(buf[i])) || (eof && i == buflen - 1)) { make_token: - if (numnode == RANDOM_MAX_PLUS1) { + if (numnode == UINT32_MAX - 1) { errno = EFBIG; err(1, "too many delimiters"); } @@ -210,15 +211,14 @@ randomize_fd(int fd, int type, int unique, double denom) free(buf); for (i = numnode; i > 0; i--) { - selected = random() % numnode; + selected = arc4random_uniform(numnode + 1); for (j = 0, prev = n = rand_root; n != NULL; j++, prev = n, n = n->next) { if (j == selected) { if (n->cp == NULL) break; - if ((int)(denom * random() / - RANDOM_MAX_PLUS1) == 0) { + if (arc4random_uniform(denom) == 0) { ret = printf("%.*s", (int)n->len - 1, n->cp); if (ret < 0) diff --git a/usr.bin/random/randomize_fd.h b/usr.bin/random/randomize_fd.h index de3f87325e3f..5b50e6265504 100644 --- a/usr.bin/random/randomize_fd.h +++ b/usr.bin/random/randomize_fd.h @@ -29,12 +29,6 @@ #ifndef __RANDOMIZE_FD__ #define __RANDOMIZE_FD__ -/* - * The random() function is defined to return values between 0 and - * 2^31 - 1 inclusive in random(3). - */ -#define RANDOM_MAX_PLUS1 0x80000000UL - #define RANDOM_TYPE_UNSET 0 #define RANDOM_TYPE_LINES 1 #define RANDOM_TYPE_WORDS 2