[gnumeric] RANDBETWEEN: Fix rare case where we overshoot.



commit 9c77270dd426ea9ae90c50e991c18d2ef30aa861
Author: Morten Welinder <terra gnome org>
Date:   Thu Mar 24 15:12:25 2016 -0400

    RANDBETWEEN: Fix rare case where we overshoot.
    
    We used to compute (h+1-l) which can end up producing (h+2-l) when
    h is 2^53+2.

 NEWS                          |    1 +
 plugins/fn-random/ChangeLog   |    6 ++++++
 plugins/fn-random/functions.c |   11 +++++++++--
 3 files changed, 16 insertions(+), 2 deletions(-)
---
diff --git a/NEWS b/NEWS
index 8c9ec93..3d905cb 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,7 @@ Gnumeric 1.12.29
 
 Morten:
        * Documentation fixes for complex numbers.
+       * Fix rare RANDBETWEEN problem.
 
 --------------------------------------------------------------------------
 Gnumeric 1.12.28
diff --git a/plugins/fn-random/ChangeLog b/plugins/fn-random/ChangeLog
index a2bb861..938850d 100644
--- a/plugins/fn-random/ChangeLog
+++ b/plugins/fn-random/ChangeLog
@@ -1,3 +1,9 @@
+2016-03-24  Morten Welinder  <terra gnome org>
+
+       * functions.c (gnumeric_randbetween): Handle extreme arguments.
+       We used to get RANDBETWEEN(2^52,2^52+2) wrong and produce results
+       up to and including 2^52+3.
+
 2016-03-22  Morten Welinder <terra gnome org>
 
        * Release 1.12.28
diff --git a/plugins/fn-random/functions.c b/plugins/fn-random/functions.c
index ec691c4..3b27035 100644
--- a/plugins/fn-random/functions.c
+++ b/plugins/fn-random/functions.c
@@ -258,15 +258,22 @@ gnumeric_randbetween (GnmFuncEvalInfo *ei, GnmValue const * const *argv)
 {
        gnm_float bottom = value_get_as_float (argv[0]);
        gnm_float top = value_get_as_float (argv[1]);
+       gnm_float choices;
 
        if (bottom > top)
                return value_new_error_NUM (ei->pos);
 
-       /* Bottom is ceiled, top floored.  Neither is truncated.  */
+       // Bottom is ceiled, top floored.  We do this on the limits, not the
+       // resulting random number, in order to ensure all integers in the
+       // range have the same probability.  Tests show that Excel 2013 does
+       // the same thing.
        bottom = gnm_ceil (bottom);
        top = gnm_floor (top);
 
-       return value_new_float (bottom + gnm_floor ((top + 1.0 - bottom) * random_01 ()));
+       // Rounding alert: adding 1 could add 2 after rounding.
+       choices = gnm_ceil ((top - bottom) + 0.875);
+
+       return value_new_float (bottom + gnm_floor (choices * random_01 ()));
 }
 
 /***************************************************************************/


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]