Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/random random(6): Use arc4random_uniform to simplify code



details:   https://anonhg.NetBSD.org/src/rev/2a3aa68b7719
branches:  trunk
changeset: 974228:2a3aa68b7719
user:      nia <nia%NetBSD.org@localhost>
date:      Sun Jul 26 15:24:00 2020 +0000

description:
random(6): Use arc4random_uniform to simplify code

diffstat:

 games/random/random.c |  14 +++++---------
 1 files changed, 5 insertions(+), 9 deletions(-)

diffs (58 lines):

diff -r 44d2aec05f49 -r 2a3aa68b7719 games/random/random.c
--- a/games/random/random.c     Sun Jul 26 15:14:23 2020 +0000
+++ b/games/random/random.c     Sun Jul 26 15:24:00 2020 +0000
@@ -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 @@
 #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 @@
 int
 main(int argc, char *argv[])
 {
-       struct timeval tp;
        double denom;
        int ch, random_exit, selected, unbuffer_output;
        char *ep;
@@ -103,12 +102,9 @@
                /* NOTREACHED */
        }
 
-       (void)gettimeofday(&tp, 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 @@
         * 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 @@
                                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))



Home | Main Index | Thread Index | Old Index