[commit: packages/random] new_api: Intermediate checkin. Fixes for randomIvalBits. Still a couple corner cases to handle before its even a full candidate, however. (3581598)

git at git.haskell.org git at git.haskell.org
Thu Mar 19 15:43:57 UTC 2015


Repository : ssh://git@git.haskell.org/random

On branch  : new_api
Link       : http://git.haskell.org/packages/random.git/commitdiff/3581598e57ef995fa3d936fa321b201b7259d276

>---------------------------------------------------------------

commit 3581598e57ef995fa3d936fa321b201b7259d276
Author: Ryan Newton <rrnewton at gmail.com>
Date:   Mon Jun 27 13:26:57 2011 -0400

    Intermediate checkin.  Fixes for randomIvalBits.  Still a couple corner cases to handle before its even a full candidate, however.


>---------------------------------------------------------------

3581598e57ef995fa3d936fa321b201b7259d276
 System/Random.hs | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/System/Random.hs b/System/Random.hs
index 7a162ef..4d4644b 100644
--- a/System/Random.hs
+++ b/System/Random.hs
@@ -91,7 +91,7 @@ import Data.IORef
 import Numeric		( readDec )
 
 
-#define DEBUGRAND
+--define DEBUGRAND
 #ifdef DEBUGRAND
 import Numeric		( showIntAtBase )
 import Data.Char     ( intToDigit )
@@ -484,18 +484,19 @@ randomIvalBits (l,h) rng
     pow2 = findBoundingPow2 range
     -- Bounding is the largest number we will generate with pow2 random bits:
     -- bounding = (1 `shiftL` pow2) - 1 -- This could overflow!
-    bounding = complement 0 `shiftR` (maxbits - pow2)
+    -- Here we explicitly counter sign-extension in shiftR:
+    bounding = (clearBit (complement 0) (maxbits-1)) `shiftR` (maxbits - pow2 - 1)
     cutoff = --if pow2 == maxbits 
 	     --then error "UNFINISHED"
 	     --else 
 	     bounding - (bounding `rem` range)
     -- rollAndTrash rolls the dice repeatedly, trashing results it doesn't
     -- like.  In the worst case, it can trash up to 50% of the
-    -- results, but usually it should be much much less.
+    -- results (but typically much much less).
     rollAndTrash g = 
       case randomBits pow2 g of 
         (x,g') | x >= cutoff -> rollAndTrash g'
-        pair                 -> pair
+        (x,g')               -> (x `mod` range, g')
 
 -- Find the smallest power of two greater than or equal to the given number.
 -- findBoundingPow2 :: (Bits a, Ord a) => a -> Int



More information about the ghc-commits mailing list