568a7e41ba0f814d95f4b45f75248b840fb51382
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Guy Harris at Network Appliance Corp.
* %sccs.include.redist.c%
static char copyright
[] =
"@(#) Copyright (c) 1994\n\
The Regents of the University of California. All rights reserved.\n";
static char sccsid
[] = "@(#)random.c 8.2 (Berkeley) %G%";
int ch
, random_exit
, selected
, unbuffer_output
;
random_exit
= unbuffer_output
= 0;
while ((ch
= getopt(argc
, argv
, "er")) != EOF
)
denom
= strtod(*argv
, &ep
);
if (denom
== 0 || *ep
!= '\0')
errx(1, "denominator is not valid.");
srandom((u_int
)(now
+ getpid()));
/* Compute a random exit status between 0 and denom - 1. */
return ((denom
* random()) / LONG_MAX
);
* Act as a filter, randomly choosing lines of the standard input
* to write to the standard output.
* Select whether to print the first line. (Prime the pump.)
* We find a random number between 0 and denom - 1 and, if it's
* 0 (which has a 1 / denom chance of being true), we select the
selected
= !((denom
* random()) / LONG_MAX
);
while ((ch
= getchar()) != EOF
) {
/* End of that line. See if we got an error. */
/* Now see if the next line is to be printed. */
selected
= !((denom
* random()) / LONG_MAX
);
(void)fprintf(stderr
, "usage: random [-er] [divisor]\n");