Commit d16ae622 authored by Paul Eggert's avatar Paul Eggert

* fns.c (Frandom): Fix rare bug where the result isn't random.

parent 37ca9077
2014-03-19 Paul Eggert <eggert@cs.ucla.edu>
* fns.c (Frandom): Fix rare bug where the result isn't random.
Fix porting inconsistency about rounding to even.
* floatfns.c (emacs_rint) [!HAVE_RINT]: Round to even.
This way, the unusual !HAVE_RINT case acts like the usual
......
......@@ -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);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment