CVS commit: src/games/random

2020-07-26 Thread Nia Alarie
Module Name:src
Committed By:   nia
Date:   Sun Jul 26 15:24:00 UTC 2020

Modified Files:
src/games/random: random.c

Log Message:
random(6): Use arc4random_uniform to simplify code


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/games/random/random.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/games/random/random.c
diff -u src/games/random/random.c:1.14 src/games/random/random.c:1.15
--- src/games/random/random.c:1.14	Wed Aug 12 08:27:24 2009
+++ src/games/random/random.c	Sun Jul 26 15:24:00 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: random.c,v 1.14 2009/08/12 08:27:24 dholland Exp $	*/
+/*	$NetBSD: random.c,v 1.15 2020/07/26 15:24:00 nia Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1994\
 #if 0
 static char sccsid[] = "@(#)random.c	8.6 (Berkeley) 6/1/94";
 #else
-__RCSID("$NetBSD: random.c,v 1.14 2009/08/12 08:27:24 dholland Exp $");
+__RCSID("$NetBSD: random.c,v 1.15 2020/07/26 15:24:00 nia Exp $");
 #endif
 #endif /* not lint */
 
@@ -62,7 +62,6 @@ static void usage(void) __dead;
 int
 main(int argc, char *argv[])
 {
-	struct timeval tp;
 	double denom;
 	int ch, random_exit, selected, unbuffer_output;
 	char *ep;
@@ -103,12 +102,9 @@ main(int argc, char *argv[])
 		/* NOTREACHED */
 	}
 
-	(void)gettimeofday(, NULL);
-	srandom((unsigned long)tp.tv_usec + tp.tv_sec + getpid());
-
 	/* Compute a random exit status between 0 and denom - 1. */
 	if (random_exit)
-		return ((denom * random()) / RANDOM_MAX);
+		return arc4random_uniform(denom);
 
 	/*
 	 * Act as a filter, randomly choosing lines of the standard input
@@ -123,7 +119,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) == 0;
+	selected = (arc4random_uniform(denom) == 0);
 	while ((ch = getchar()) != EOF) {
 		if (selected)
 			(void)putchar(ch);
@@ -133,7 +129,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) == 0;
+			selected = (arc4random_uniform(denom) == 0);
 		}
 	}
 	if (ferror(stdin))



CVS commit: src/games/random

2009-08-12 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Wed Aug 12 08:27:24 UTC 2009

Modified Files:
src/games/random: random.c

Log Message:
sprinkle static


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/games/random/random.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/games/random/random.c
diff -u src/games/random/random.c:1.13 src/games/random/random.c:1.14
--- src/games/random/random.c:1.13	Mon Jul 20 05:33:35 2009
+++ src/games/random/random.c	Wed Aug 12 08:27:24 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: random.c,v 1.13 2009/07/20 05:33:35 dholland Exp $	*/
+/*	$NetBSD: random.c,v 1.14 2009/08/12 08:27:24 dholland Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = @(#)random.c	8.6 (Berkeley) 6/1/94;
 #else
-__RCSID($NetBSD: random.c,v 1.13 2009/07/20 05:33:35 dholland Exp $);
+__RCSID($NetBSD: random.c,v 1.14 2009/08/12 08:27:24 dholland Exp $);
 #endif
 #endif /* not lint */
 
@@ -57,7 +57,7 @@
 #include unistd.h
 #include limits.h
 
-void usage(void) __dead;
+static void usage(void) __dead;
 
 int
 main(int argc, char *argv[])
@@ -143,7 +143,7 @@
 	return 0;
 }
 
-void
+static void
 usage(void)
 {
 



CVS commit: src/games/random

2009-07-19 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Mon Jul 20 05:33:35 UTC 2009

Modified Files:
src/games/random: random.c

Log Message:
ANSIfy. Also, we now have RANDOM_MAX; use it. Object diffs checked...


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/games/random/random.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/games/random/random.c
diff -u src/games/random/random.c:1.12 src/games/random/random.c:1.13
--- src/games/random/random.c:1.12	Sun Jul 20 01:03:22 2008
+++ src/games/random/random.c	Mon Jul 20 05:33:35 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: random.c,v 1.12 2008/07/20 01:03:22 lukem Exp $	*/
+/*	$NetBSD: random.c,v 1.13 2009/07/20 05:33:35 dholland Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = @(#)random.c	8.6 (Berkeley) 6/1/94;
 #else
-__RCSID($NetBSD: random.c,v 1.12 2008/07/20 01:03:22 lukem Exp $);
+__RCSID($NetBSD: random.c,v 1.13 2009/07/20 05:33:35 dholland Exp $);
 #endif
 #endif /* not lint */
 
@@ -57,15 +57,10 @@
 #include unistd.h
 #include limits.h
 
-#define MAXRANDOM	2147483647
-
-int  main(int, char **);
 void usage(void) __dead;
 
 int
-main(argc, argv)
-	int argc;
-	char *argv[];
+main(int argc, char *argv[])
 {
 	struct timeval tp;
 	double denom;
@@ -113,7 +108,7 @@
 
 	/* Compute a random exit status between 0 and denom - 1. */
 	if (random_exit)
-		return ((denom * random()) / MAXRANDOM);
+		return ((denom * random()) / RANDOM_MAX);
 
 	/*
 	 * Act as a filter, randomly choosing lines of the standard input
@@ -128,7 +123,7 @@
 	 * 0 (which has a 1 / denom chance of being true), we select the
 	 * line.
 	 */
-	selected = (int)(denom * random() / MAXRANDOM) == 0;
+	selected = (int)(denom * random() / RANDOM_MAX) == 0;
 	while ((ch = getchar()) != EOF) {
 		if (selected)
 			(void)putchar(ch);
@@ -138,7 +133,7 @@
 err(2, stdout);
 
 			/* Now see if the next line is to be printed. */
-			selected = (int)(denom * random() / MAXRANDOM) == 0;
+			selected = (int)(denom * random() / RANDOM_MAX) == 0;
 		}
 	}
 	if (ferror(stdin))
@@ -149,7 +144,7 @@
 }
 
 void
-usage()
+usage(void)
 {
 
 	(void)fprintf(stderr, usage: random [-er] [denominator]\n);