1/* 2 * Copyright (c) 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Guy Harris at Network Appliance Corp. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 32 unchanged lines hidden (view full) --- 41 The Regents of the University of California. All rights reserved.\n"; 42#endif /* not lint */ 43 44#ifndef lint 45static const char sccsid[] = "@(#)random.c 8.5 (Berkeley) 4/5/94"; 46#endif /* not lint */ 47#endif 48#include <sys/cdefs.h> |
49__FBSDID("$FreeBSD: head/games/random/random.c 181527 2008-08-10 11:31:56Z ache $"); |
50 51#include <sys/types.h> 52 53#include <err.h> 54#include <errno.h> 55#include <fcntl.h> 56#include <limits.h> 57#include <locale.h> --- 99 unchanged lines hidden (view full) --- 157 err(1, "%s", filename); 158 ret = randomize_fd(fd, random_type, unique_output, denom); 159 if (!random_exit) 160 return(ret); 161 } 162 163 /* Compute a random exit status between 0 and denom - 1. */ 164 if (random_exit) |
165 return (int)(denom * random() / RANDOM_MAX_PLUS1); |
166 167 /* 168 * Select whether to print the first line. (Prime the pump.) 169 * We find a random number between 0 and denom - 1 and, if it's 170 * 0 (which has a 1 / denom chance of being true), we select the 171 * line. 172 */ |
173 selected = (int)(denom * random() / RANDOM_MAX_PLUS1) == 0; |
174 while ((ch = getchar()) != EOF) { 175 if (selected) 176 (void)putchar(ch); 177 if (ch == '\n') { 178 /* End of that line. See if we got an error. */ 179 if (ferror(stdout)) 180 err(2, "stdout"); 181 182 /* Now see if the next line is to be printed. */ |
183 selected = (int)(denom * random() / RANDOM_MAX_PLUS1) == 0; |
184 } 185 } 186 if (ferror(stdin)) 187 err(2, "stdin"); 188 exit (0); 189} 190 191static void 192usage(void) 193{ 194 195 fprintf(stderr, "usage: random [-elrUuw] [-f filename] [denominator]\n"); 196 exit(1); 197} |