Source-Changes-HG archive

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

[src/trunk]: src/games/fish fish: remove modulo bias from random number gener...



details:   https://anonhg.NetBSD.org/src/rev/548b3ae8b419
branches:  trunk
changeset: 378971:548b3ae8b419
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat May 01 21:10:57 2021 +0000

description:
fish: remove modulo bias from random number generation

It probably doesn't matter in practice, but omitting this piece of code
always looks like an oversight.

diffstat:

 games/fish/fish.c |  11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)

diffs (33 lines):

diff -r 6bf8f3a223d5 -r 548b3ae8b419 games/fish/fish.c
--- a/games/fish/fish.c Sat May 01 20:29:23 2021 +0000
+++ b/games/fish/fish.c Sat May 01 21:10:57 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fish.c,v 1.23 2018/03/05 04:59:54 eadler Exp $ */
+/*     $NetBSD: fish.c,v 1.24 2021/05/01 21:10:57 rillig Exp $ */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)fish.c     8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: fish.c,v 1.23 2018/03/05 04:59:54 eadler Exp $");
+__RCSID("$NetBSD: fish.c,v 1.24 2021/05/01 21:10:57 rillig Exp $");
 #endif
 #endif /* not lint */
 
@@ -435,8 +435,13 @@ init(void)
 static int
 nrandom(int n)
 {
+       long r;
 
-       return((int)random() % n);
+       for (;;) {
+               r = random();
+               if (r < RANDOM_MAX - RANDOM_MAX % n)
+                       return (int)(r % n);
+       }
 }
 
 static void



Home | Main Index | Thread Index | Old Index