system-random-effect 0.1.2.0 → 0.2.0
raw patch · 3 files changed
+35/−16 lines, 3 files
Files
- src/System/Random/Effect.hs +9/−14
- system-random-effect.cabal +1/−1
- test/Test.hs +25/−1
src/System/Random/Effect.hs view
@@ -102,7 +102,7 @@ -> Eff (State Random :> r) w -> Eff r w runRandomState seed computation =- fmap snd (runState seed computation)+ snd <$> runState seed computation {-# INLINE runRandomState #-} -- | A generalized form of generating a random number of the correct type@@ -205,10 +205,7 @@ -- -- Returns a number in the inclusive range [0, range]. ----- 'hMask' is the mask to get the number of bits per random number--- we generate.------ 'nBits' is the number of bits in 'range'.+-- 'numBits' is the number of bits in 'range'. uniformIntDist' :: Member (State Random) r => Integer -- ^ range -> Int -- ^ numBits@@ -300,8 +297,6 @@ -- -- t must be >= 0 -- p must be in the range [0, 1].------ Warning: NOT IMPLEMENTED! binomialDist :: Member (State Random) r => Int -- ^ t -> Rational -- ^ p@@ -315,9 +310,8 @@ trials <- V.replicateM t randomDouble let p' = realToFrac p- succeeded = V.map (<= p') trials numSuccesses =- V.foldl' (\s b -> if b then s+1 else s) 0 succeeded+ V.foldl' (\s x -> if x <= p' then s+1 else s) 0 trials return numSuccesses @@ -489,11 +483,12 @@ -- | Performs O(n) work building a table which we can later use -- sample with 'discreteDist'.-buildDDH :: [Integer] -> DiscreteDistributionHelper+buildDDH :: [Word64] -> DiscreteDistributionHelper buildDDH xs =- let vs = V.fromList xs- s = V.sum vs- ns = V.map (% s) vs -- normalize the list+ let vs = V.fromList xs+ s = fromIntegral (V.sum vs)+ vs' = V.map fromIntegral vs+ ns = V.map (% s) vs' -- normalize the list in DDH (V.postscanl' (+) 0 ns) -- | Given a pre-build 'DiscreteDistributionHelper' (use 'buildDDH'),@@ -510,7 +505,7 @@ discreteDist (DDH xs) = do y <- realToFrac <$> randomDouble return $ runST $ do- mv <- V.thaw xs+ mv <- V.unsafeThaw xs binarySearch mv y -- | This function produces random floating-point numbers, which
system-random-effect.cabal view
@@ -1,5 +1,5 @@ name: system-random-effect-version: 0.1.2.0+version: 0.2.0 synopsis: Random number generation for extensible effects. homepage: https://github.com/wowus/system-random-effect license: BSD3
test/Test.hs view
@@ -3,6 +3,7 @@ module Main ( main ) where import Control.Eff+import Control.Eff.State import System.Random.Effect @@ -20,6 +21,9 @@ main :: IO () main = defaultMain tests +runWithSeed :: Word64 -> Eff (State Random :> ()) a -> a+runWithSeed seed = run . runRandomState (mkRandom seed)+ checkRange :: (Integer, Integer) -> Integer -> Bool checkRange (low, high) x = x >= low && x <= high@@ -29,9 +33,29 @@ let low = min a b high = max a b - in checkRange (low, high) $ run $ runRandomState (mkRandom seed) $ do+ in checkRange (low, high) . runWithSeed seed $ do uniformIntDist a b +testDiscreteDistributionInRange :: [Word64] -> Word64 -> Bool+testDiscreteDistributionInRange xs seed =+ let ddh = buildDDH xs+ minVal = 0+ maxVal = length xs - 1+ in length xs == 0 || sum xs == 0 ||+ ((\x -> x >= minVal && x <= maxVal) . runWithSeed seed $+ discreteDist ddh)++testNoZeroDiscreteDistributionPick :: [Word64] -> Word64 -> Bool+testNoZeroDiscreteDistributionPick xs seed =+ let ddh = buildDDH xs+ minVal = 0+ maxVal = length xs - 1+ in length xs == 0 || sum xs == 0 ||+ ((\x -> (xs !! x) /= 0) . runWithSeed seed $+ discreteDist ddh)+ tests = [ testProperty "random range" testUniformRandom+ , testProperty "discrete dist range" testDiscreteDistributionInRange+ , testProperty "no non-zero discrete dist pick" testNoZeroDiscreteDistributionPick ]