changeset 27856:b3a03dbde858

Create equally-spaced floating point values for rand ("double"). * randmtzig.cc (randu53): Get random integer from randi53(). Test and exclude value of 0 (left endpoint of range). Divide by 2^53 to normalize in to range (0, 1) with uniform spacing of eps/2.
author Rik <rik@octave.org>
date Thu, 19 Dec 2019 12:19:30 -0800
parents 9405e2be91d0
children 81ada4b7d85b
files liboctave/numeric/randmtzig.cc
diffstat 1 files changed, 9 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/numeric/randmtzig.cc	Thu Dec 19 11:57:54 2019 -0800
+++ b/liboctave/numeric/randmtzig.cc	Thu Dec 19 12:19:30 2019 -0800
@@ -393,9 +393,15 @@
   /* generates a random number on (0,1) with 53-bit resolution */
   static double randu53 (void)
   {
-    const uint32_t a = randi32 () >> 5;
-    const uint32_t b = randi32 () >> 6;
-    return (a*67108864.0+b+0.4) * (1.0/9007199254740992.0);
+    uint64_t i;
+
+    do
+      {
+        i = randi53 ();
+      }
+    while (i == 0);
+
+    return i * (1.0 / 9007199254740992.0);
   }
 
   /* Determine mantissa for uniform doubles */