summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrillig <rillig@NetBSD.org>2021-05-01 21:10:57 +0000
committerCameron Katri <me@cameronkatri.com>2021-05-05 14:35:12 -0400
commit0a101f8439154af1932c3ec996ca202cb5888f97 (patch)
treea53b592943c9f79164b73b10e9d750c4c220ba03
parentad4d95adcc9a0623340db740895a6103fef33381 (diff)
downloadbsdgames-darwin-0a101f8439154af1932c3ec996ca202cb5888f97.tar.gz
bsdgames-darwin-0a101f8439154af1932c3ec996ca202cb5888f97.tar.zst
bsdgames-darwin-0a101f8439154af1932c3ec996ca202cb5888f97.zip
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.
-rw-r--r--fish/fish.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/fish/fish.c b/fish/fish.c
index 78e2dc52..36800d03 100644
--- a/fish/fish.c
+++ b/fish/fish.c
@@ -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 @@ __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
#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