summaryrefslogtreecommitdiff
path: root/src/fns.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2014-03-19 14:14:32 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2014-03-19 14:14:32 -0700
commitd16ae6228826c561bdb3701bd65d7517bd9e30d5 (patch)
tree74f1bc879dedc2c73c2780ebb3e2fb41787571ee /src/fns.c
parent37ca9077224a3c0e1c2051a47c58148d826812e8 (diff)
downloademacs-d16ae6228826c561bdb3701bd65d7517bd9e30d5.tar.gz
* fns.c (Frandom): Fix rare bug where the result isn't random.
Diffstat (limited to 'src/fns.c')
-rw-r--r--src/fns.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/fns.c b/src/fns.c
index 7b3d41d5374..499e4b490a6 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -79,8 +79,17 @@ See Info node `(elisp)Random Numbers' for more details. */)
seed_random (SSDATA (limit), SBYTES (limit));
val = get_random ();
- if (NATNUMP (limit) && XFASTINT (limit) != 0)
- val %= XFASTINT (limit);
+ if (INTEGERP (limit) && 0 < XINT (limit))
+ while (true)
+ {
+ /* Return the remainder, except reject the rare case where
+ get_random returns a number so close to INTMASK that the
+ remainder isn't random. */
+ EMACS_INT remainder = val % XINT (limit);
+ if (val - remainder <= INTMASK - XINT (limit) + 1)
+ return make_number (remainder);
+ val = get_random ();
+ }
return make_number (val);
}