xref: /openbsd/games/random/random.c (revision df930be7)
1 /*	$NetBSD: random.c,v 1.3 1995/04/22 07:44:05 cgd Exp $	*/
2 
3 /*
4  * Copyright (c) 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Guy Harris at Network Appliance Corp.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #ifndef lint
40 static char copyright[] =
41 "@(#) Copyright (c) 1994\n\
42 	The Regents of the University of California.  All rights reserved.\n";
43 #endif /* not lint */
44 
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)random.c	8.5 (Berkeley) 4/5/94";
48 #else
49 static char rcsid[] = "$NetBSD: random.c,v 1.3 1995/04/22 07:44:05 cgd Exp $";
50 #endif
51 #endif /* not lint */
52 
53 #include <sys/types.h>
54 
55 #include <err.h>
56 #include <errno.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <time.h>
60 #include <unistd.h>
61 #include <limits.h>
62 
63 void usage __P((void));
64 
65 int
66 main(argc, argv)
67 	int argc;
68 	char *argv[];
69 {
70 	extern int optind;
71 	time_t now;
72 	double denom;
73 	int ch, random_exit, selected, unbuffer_output;
74 	char *ep;
75 
76 	random_exit = unbuffer_output = 0;
77 	while ((ch = getopt(argc, argv, "er")) != EOF)
78 		switch (ch) {
79 		case 'e':
80 			random_exit = 1;
81 			break;
82 		case 'r':
83 			unbuffer_output = 1;
84 			break;
85 		default:
86 		case '?':
87 			usage();
88 			/* NOTREACHED */
89 		}
90 
91 	argc -= optind;
92 	argv += optind;
93 
94 	switch (argc) {
95 	case 0:
96 		denom = 2;
97 		break;
98 	case 1:
99 		errno = 0;
100 		denom = strtod(*argv, &ep);
101 		if (errno == ERANGE)
102 			err(1, "%s", *argv);
103 		if (denom == 0 || *ep != '\0')
104 			errx(1, "denominator is not valid.");
105 		break;
106 	default:
107 		usage();
108 		/* NOTREACHED */
109 	}
110 
111 	(void)time(&now);
112 	srandom((u_int)(now + getpid()));
113 
114 	/* Compute a random exit status between 0 and denom - 1. */
115 	if (random_exit)
116 		return ((denom * random()) / LONG_MAX);
117 
118 	/*
119 	 * Act as a filter, randomly choosing lines of the standard input
120 	 * to write to the standard output.
121 	 */
122 	if (unbuffer_output)
123 		setbuf(stdout, NULL);
124 
125 	/*
126 	 * Select whether to print the first line.  (Prime the pump.)
127 	 * We find a random number between 0 and denom - 1 and, if it's
128 	 * 0 (which has a 1 / denom chance of being true), we select the
129 	 * line.
130 	 */
131 	selected = (int)(denom * random() / LONG_MAX) == 0;
132 	while ((ch = getchar()) != EOF) {
133 		if (selected)
134 			(void)putchar(ch);
135 		if (ch == '\n') {
136 			/* End of that line.  See if we got an error. */
137 			if (ferror(stdout))
138 				err(2, "stdout");
139 
140 			/* Now see if the next line is to be printed. */
141 			selected = (int)(denom * random() / LONG_MAX) == 0;
142 		}
143 	}
144 	if (ferror(stdin))
145 		err(2, "stdin");
146 	exit (0);
147 }
148 
149 void
150 usage()
151 {
152 
153 	(void)fprintf(stderr, "usage: random [-er] [denominator]\n");
154 	exit(1);
155 }
156