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
|
.\" @(#)random.6 8.2 (Berkeley) 3/31/94
|
||||||
.\" $FreeBSD$
|
.\" $FreeBSD$
|
||||||
.\"
|
.\"
|
||||||
.Dd February 8, 2003
|
.Dd December 12, 2019
|
||||||
.Dt RANDOM 6
|
.Dt RANDOM 6
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
@ -62,9 +62,7 @@ space characters as determined by
|
|||||||
The default
|
The default
|
||||||
.Ar denominator
|
.Ar denominator
|
||||||
for this mode of operation is 1, which gives each line a chance to be
|
for this mode of operation is 1, which gives each line a chance to be
|
||||||
displayed, but in a
|
displayed, but in a random order.
|
||||||
.Xr random 3
|
|
||||||
order.
|
|
||||||
.Pp
|
.Pp
|
||||||
The options are as follows:
|
The options are as follows:
|
||||||
.Bl -tag -width Ds
|
.Bl -tag -width Ds
|
||||||
@ -112,7 +110,6 @@ Randomize words separated by
|
|||||||
instead of newlines.
|
instead of newlines.
|
||||||
.El
|
.El
|
||||||
.Sh SEE ALSO
|
.Sh SEE ALSO
|
||||||
.Xr random 3 ,
|
|
||||||
.Xr fortune 6
|
.Xr fortune 6
|
||||||
.Sh HISTORY
|
.Sh HISTORY
|
||||||
The
|
The
|
||||||
|
@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -135,8 +136,6 @@ main(int argc, char *argv[])
|
|||||||
/* NOTREACHED */
|
/* NOTREACHED */
|
||||||
}
|
}
|
||||||
|
|
||||||
srandomdev();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Act as a filter, randomly choosing lines of the standard input
|
* Act as a filter, randomly choosing lines of the standard input
|
||||||
* to write to the standard output.
|
* 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. */
|
/* Compute a random exit status between 0 and denom - 1. */
|
||||||
if (random_exit)
|
if (random_exit)
|
||||||
return (int)(denom * random() / RANDOM_MAX_PLUS1);
|
return (arc4random_uniform(denom));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Select whether to print the first line. (Prime the pump.)
|
* 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
|
* 0 (which has a 1 / denom chance of being true), we select the
|
||||||
* line.
|
* line.
|
||||||
*/
|
*/
|
||||||
selected = (int)(denom * random() / RANDOM_MAX_PLUS1) == 0;
|
selected = (arc4random_uniform(denom) == 0);
|
||||||
while ((ch = getchar()) != EOF) {
|
while ((ch = getchar()) != EOF) {
|
||||||
if (selected)
|
if (selected)
|
||||||
(void)putchar(ch);
|
(void)putchar(ch);
|
||||||
@ -176,8 +175,7 @@ main(int argc, char *argv[])
|
|||||||
err(2, "stdout");
|
err(2, "stdout");
|
||||||
|
|
||||||
/* Now see if the next line is to be printed. */
|
/* Now see if the next line is to be printed. */
|
||||||
selected = (int)(denom * random() /
|
selected = (arc4random_uniform(denom) == 0);
|
||||||
RANDOM_MAX_PLUS1) == 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ferror(stdin))
|
if (ferror(stdin))
|
||||||
|
@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
#include <err.h>
|
#include <err.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.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])) ||
|
(type == RANDOM_TYPE_WORDS && isspace(buf[i])) ||
|
||||||
(eof && i == buflen - 1)) {
|
(eof && i == buflen - 1)) {
|
||||||
make_token:
|
make_token:
|
||||||
if (numnode == RANDOM_MAX_PLUS1) {
|
if (numnode == UINT32_MAX - 1) {
|
||||||
errno = EFBIG;
|
errno = EFBIG;
|
||||||
err(1, "too many delimiters");
|
err(1, "too many delimiters");
|
||||||
}
|
}
|
||||||
@ -210,15 +211,14 @@ randomize_fd(int fd, int type, int unique, double denom)
|
|||||||
free(buf);
|
free(buf);
|
||||||
|
|
||||||
for (i = numnode; i > 0; i--) {
|
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) {
|
for (j = 0, prev = n = rand_root; n != NULL; j++, prev = n, n = n->next) {
|
||||||
if (j == selected) {
|
if (j == selected) {
|
||||||
if (n->cp == NULL)
|
if (n->cp == NULL)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if ((int)(denom * random() /
|
if (arc4random_uniform(denom) == 0) {
|
||||||
RANDOM_MAX_PLUS1) == 0) {
|
|
||||||
ret = printf("%.*s",
|
ret = printf("%.*s",
|
||||||
(int)n->len - 1, n->cp);
|
(int)n->len - 1, n->cp);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
|
@ -29,12 +29,6 @@
|
|||||||
#ifndef __RANDOMIZE_FD__
|
#ifndef __RANDOMIZE_FD__
|
||||||
#define __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_UNSET 0
|
||||||
#define RANDOM_TYPE_LINES 1
|
#define RANDOM_TYPE_LINES 1
|
||||||
#define RANDOM_TYPE_WORDS 2
|
#define RANDOM_TYPE_WORDS 2
|
||||||
|
Loading…
Reference in New Issue
Block a user