mirror of
https://github.com/freebsd/freebsd-src
synced 2024-10-15 12:54:27 +00:00
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.
This commit is contained in:
parent
d82e4d759d
commit
c7b8411cc9
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=355693
|
@ -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
|
||||
|
|
|
@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$");
|
|||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <locale.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -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))
|
||||
|
|
|
@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$");
|
|||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue