random 1.0.1.0 → 1.0.1.1
raw patch · 2 files changed
+22/−8 lines, 2 files
Files
- System/Random.hs +17/−6
- random.cabal +5/−2
System/Random.hs view
@@ -6,7 +6,7 @@ -- | -- Module : System.Random -- Copyright : (c) The University of Glasgow 2001--- License : BSD-style (see the file libraries/base/LICENSE)+-- License : BSD-style (see the file LICENSE in the 'random' repository) -- -- Maintainer : libraries@haskell.org -- Stability : stable@@ -114,7 +114,11 @@ -- | The class 'RandomGen' provides a common interface to random number -- generators. --+#ifdef ENABLE_SPLITTABLEGEN -- Minimal complete definition: 'next'.+#else+-- Minimal complete definition: 'next' and 'split'.+#endif class RandomGen g where @@ -147,7 +151,7 @@ #ifdef ENABLE_SPLITTABLEGEN -- | The class 'SplittableGen' proivides a way to specify a random number--- generator that can be split into two new generators.+-- generator that can be split into two new generators. class SplittableGen g where #endif -- |The 'split' operation allows one to obtain two distinct random number@@ -355,11 +359,11 @@ random g = randomR (minBound,maxBound) g {-# INLINE randomRFloating #-}-randomRFloating :: (Num a, Ord a, Random a, RandomGen g) => (a, a) -> g -> (a, g)+randomRFloating :: (Fractional a, Num a, Ord a, Random a, RandomGen g) => (a, a) -> g -> (a, g) randomRFloating (l,h) g | l>h = randomRFloating (h,l) g | otherwise = let (coef,g') = random g in - (l + coef * (h-l), g')+ (2.0 * (0.5*l + coef * (0.5*h - 0.5*l)), g') -- avoid overflow instance Random Double where randomR = randomRFloating@@ -423,14 +427,17 @@ | otherwise = case (f n 1 rng) of (v, rng') -> (fromInteger (l + v `mod` k), rng') where k = h - l + 1+ -- ERROR: b here (2^31-87) represents a baked-in assumption about genRange: b = 2147483561 n = iLogBase b k + -- Here we loop until we've generated enough randomness to cover the range: f 0 acc g = (acc, g) f n' acc g = let (x,g') = next g in+ -- We shift over the random bits generated thusfar (* b) and add in the new ones. f (n' - 1) (fromIntegral x + acc * b) g' -- The continuous functions on the other hand take an [inclusive,exclusive) range.@@ -445,8 +452,8 @@ (x, rng') -> let scaled_x = - fromDouble ((l+h)/2) + - fromDouble ((h-l) / realToFrac int32Count) *+ fromDouble (0.5*l + 0.5*h) + -- previously (l+h)/2, overflowed+ fromDouble ((0.5*h - 0.5*l) / (0.5 * realToFrac int32Count)) * -- avoid overflow fromIntegral (x::Int32) in (scaled_x, rng')@@ -454,6 +461,10 @@ int32Count :: Integer int32Count = toInteger (maxBound::Int32) - toInteger (minBound::Int32) + 1 +-- Perform an expensive logarithm on arbitrary-size integers by repeated division.+-- +-- (NOTE: This actually returns ceiling(log(i) base b) except with an+-- incorrect result at iLogBase b b = 2.) iLogBase :: Integer -> Integer -> Integer iLogBase b i = if i < b then 1 else 1 + iLogBase b (i `div` b)
random.cabal view
@@ -1,7 +1,8 @@ name: random-version: 1.0.1.0+version: 1.0.1.1 -- 1.0.1.0 -- bump for bug fixes, but no SplittableGen yet+-- 1.0.1.1 -- bump for overflow bug fixes license: BSD3 license-file: LICENSE@@ -10,7 +11,9 @@ synopsis: random number library category: System description:- This package provides a random number library.+ This package provides a basic random number generation+ library, including the ability to split random number+ generators. build-type: Simple Cabal-Version: >= 1.6