Source-Changes-HG archive

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

[src/trunk]: src/games/fortune fortune: arc4random_uniform for better uniform...



details:   https://anonhg.NetBSD.org/src/rev/f176067562a1
branches:  trunk
changeset: 1012076:f176067562a1
user:      nia <nia%NetBSD.org@localhost>
date:      Tue Jul 21 03:05:40 2020 +0000

description:
fortune: arc4random_uniform for better uniform values than random() % ...

diffstat:

 games/fortune/fortune/fortune.c |  23 ++++++++---------------
 games/fortune/strfile/strfile.c |   8 +++-----
 2 files changed, 11 insertions(+), 20 deletions(-)

diffs (127 lines):

diff -r 5edb5fe6f7d6 -r f176067562a1 games/fortune/fortune/fortune.c
--- a/games/fortune/fortune/fortune.c   Tue Jul 21 02:42:05 2020 +0000
+++ b/games/fortune/fortune/fortune.c   Tue Jul 21 03:05:40 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fortune.c,v 1.64 2012/06/19 05:46:08 dholland Exp $    */
+/*     $NetBSD: fortune.c,v 1.65 2020/07/21 03:05:40 nia Exp $ */
 
 /*-
  * Copyright (c) 1986, 1993
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)fortune.c  8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: fortune.c,v 1.64 2012/06/19 05:46:08 dholland Exp $");
+__RCSID("$NetBSD: fortune.c,v 1.65 2020/07/21 03:05:40 nia Exp $");
 #endif
 #endif /* not lint */
 
@@ -190,7 +190,6 @@
 int
 main(int ac, char *av[])
 {
-       struct timeval tv;
 #ifdef OK_TO_WRITE_DISK
        int fd;
 #endif /* OK_TO_WRITE_DISK */
@@ -203,12 +202,6 @@
 #endif
 
        init_prob();
-       if (gettimeofday(&tv, NULL) != 0)
-               err(1, "gettimeofday()");
-       srandom(((unsigned long)tv.tv_sec)    *
-                ((unsigned long)tv.tv_usec+1) *
-               ((unsigned long)getpid()+1)   *
-                ((unsigned long)getppid()+1));
        do {
                get_fort();
        } while ((Short_only && fortlen() > SLEN) ||
@@ -933,7 +926,7 @@
        if (File_list->next == NULL || File_list->percent == NO_PROB)
                fp = File_list;
        else {
-               choice = random() % 100;
+               choice = arc4random_uniform(100);
                DPRINTF(1, (stderr, "choice = %d\n", choice));
                for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
                        if (choice < fp->percent)
@@ -953,7 +946,7 @@
        else {
                if (fp->next != NULL) {
                        sum_noprobs(fp);
-                       choice = random() % Noprob_tbl.str_numstr;
+                       choice = arc4random_uniform(Noprob_tbl.str_numstr);
                        DPRINTF(1, (stderr, "choice = %d (of %d) \n", choice,
                                    Noprob_tbl.str_numstr));
                        while ((u_int32_t)choice >= fp->tbl.str_numstr) {
@@ -994,7 +987,7 @@
        int  choice;
 
        if (Equal_probs) {
-               choice = random() % parent->num_children;
+               choice = arc4random_uniform(parent->num_children);
                DPRINTF(1, (stderr, "    choice = %d (of %d)\n",
                            choice, parent->num_children));
                for (fp = parent->child; choice--; fp = fp->next)
@@ -1004,7 +997,7 @@
        }
        else {
                get_tbl(parent);
-               choice = random() % parent->tbl.str_numstr;
+               choice = arc4random_uniform(parent->tbl.str_numstr);
                DPRINTF(1, (stderr, "    choice = %d (of %d)\n",
                            choice, parent->tbl.str_numstr));
                for (fp = parent->child; (u_int32_t)choice >= fp->tbl.str_numstr;
@@ -1084,13 +1077,13 @@
 #ifdef OK_TO_WRITE_DISK
                if ((fd = open(fp->posfile, O_RDONLY)) < 0 ||
                    read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
-                       fp->pos = random() % fp->tbl.str_numstr;
+                       fp->pos = arc4random_uniform(fp->tbl.str_numstr);
                else if (fp->pos >= fp->tbl.str_numstr)
                        fp->pos %= fp->tbl.str_numstr;
                if (fd >= 0)
                        (void) close(fd);
 #else
-               fp->pos = random() % fp->tbl.str_numstr;
+               fp->pos = arc4random_uniform(fp->tbl.str_numstr);
 #endif /* OK_TO_WRITE_DISK */
        }
        if ((u_int64_t)++(fp->pos) >= fp->tbl.str_numstr)
diff -r 5edb5fe6f7d6 -r f176067562a1 games/fortune/strfile/strfile.c
--- a/games/fortune/strfile/strfile.c   Tue Jul 21 02:42:05 2020 +0000
+++ b/games/fortune/strfile/strfile.c   Tue Jul 21 03:05:40 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: strfile.c,v 1.40 2020/04/30 12:32:26 christos Exp $    */
+/*     $NetBSD: strfile.c,v 1.41 2020/07/21 03:05:40 nia Exp $ */
 
 /*-
  * Copyright (c) 1989, 1993
@@ -47,7 +47,7 @@
 #if 0
 static char sccsid[] = "@(#)strfile.c  8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: strfile.c,v 1.40 2020/04/30 12:32:26 christos Exp $");
+__RCSID("$NetBSD: strfile.c,v 1.41 2020/07/21 03:05:40 nia Exp $");
 #endif
 #endif /* not lint */
 #endif /* __NetBSD__ */
@@ -438,8 +438,6 @@
        off_t   tmp;
        off_t   *sp;
 
-       srandom((int)(time(NULL) + getpid()));
-
        Tbl.str_flags |= STR_RANDOM;
        cnt = Tbl.str_numstr;
 
@@ -448,7 +446,7 @@
         */
 
        for (sp = Seekpts; cnt > 0; cnt--, sp++) {
-               i = random() % cnt;
+               i = arc4random_uniform(cnt);
                tmp = sp[0];
                sp[0] = sp[i];
                sp[i] = tmp;



Home | Main Index | Thread Index | Old Index