random-source 0.3.0.11 → 0.3.0.12
raw patch · 2 files changed
+21/−21 lines, 2 files
Files
- random-source.cabal +7/−7
- src/Data/Random/Source/StdGen.hs +14/−14
random-source.cabal view
@@ -1,5 +1,5 @@ name: random-source-version: 0.3.0.11+version: 0.3.0.12 stability: provisional cabal-version: >= 1.10@@ -47,7 +47,7 @@ Data.Random.Internal.Source other-modules: Data.Random.Source.Internal.Prim Data.Random.Source.Internal.TH- + if impl(ghc >= 6.10) -- mwc-random depends on vector, which doesn't build on GHC < 6.10. -- I considered breaking this module out into another package, but I@@ -58,18 +58,18 @@ -- this cabal file is hardly any additional effort. exposed-modules: Data.Random.Source.MWC build-depends: mwc-random- + if flag(mtl2)- build-depends: mtl == 2.*+ build-depends: mtl >= 2.0 && < 2.3 cpp-options: -DMTL2 else build-depends: mtl == 1.*- + if flag(base4)- build-depends: base >= 4 && <5, syb+ build-depends: base >= 4 && <4.16, syb else build-depends: base >= 3 && <4- + build-depends: flexible-defaults >= 0.0.0.2, mersenne-random-pure64, random >= 1.2.0 && < 1.3,
src/Data/Random/Source/StdGen.hs view
@@ -1,6 +1,6 @@ {-# LANGUAGE CPP,- MultiParamTypeClasses, FlexibleInstances, UndecidableInstances, GADTs,+ MultiParamTypeClasses, FlexibleContexts, FlexibleInstances, UndecidableInstances, GADTs, BangPatterns, RankNTypes, ScopedTypeVariables #-}@@ -15,7 +15,7 @@ ( StdGen , mkStdGen , newStdGen- + , getRandomPrimFromStdGenIO , getRandomPrimFromRandomGenRef , getRandomPrimFromRandomGenState@@ -41,7 +41,7 @@ -- Note that this instance is probably a Bad Idea. STM allows random variables -- to interact in spooky quantum-esque ways - One transaction can 'retry' until--- it gets a \"random\" answer it likes, which causes it to selectively consume +-- it gets a \"random\" answer it likes, which causes it to selectively consume -- entropy, biasing the supply from which other random variables will draw. -- instance (Monad m, ModifyRef (TVar StdGen) m StdGen) => RandomSource m (TVar StdGen) where -- {-# SPECIALIZE instance RandomSource IO (TVar StdGen) #-}@@ -55,22 +55,22 @@ getRandomPrimFrom = getRandomPrimFromRandomGenRef getRandomPrimFromStdGenIO :: Prim a -> IO a-getRandomPrimFromStdGenIO +getRandomPrimFromStdGenIO = getStdRandom . runState . getRandomPrim -- |Given a mutable reference to a 'RandomGen' generator, we can make a -- 'RandomSource' usable in any monad in which the reference can be modified.--- +-- -- See "Data.Random.Source.PureMT".'getRandomPrimFromMTRef' for more detailed -- usage hints - this function serves exactly the same purpose except for a -- 'StdGen' generator instead of a 'PureMT' generator. getRandomPrimFromRandomGenRef :: (Monad m, ModifyRef sr m g, RandomGen g) => sr -> Prim a -> m a-getRandomPrimFromRandomGenRef ref - = atomicModifyReference' ref - . runState +getRandomPrimFromRandomGenRef ref+ = atomicModifyReference' ref+ . runState . getRandomPrimFromRandomGenState atomicModifyReference' :: ModifyRef sr m a => sr -> (a -> (b, a)) -> m b@@ -84,15 +84,15 @@ -- Additionally, the standard mtl state monads have 'MonadRandom' instances -- which do precisely that, allowing an easy conversion of 'RVar's and -- other 'Distribution' instances to \"pure\" random variables.--- +-- -- Again, see "Data.Random.Source.PureMT".'getRandomPrimFromMTState' for more--- detailed usage hints - this function serves exactly the same purpose except +-- detailed usage hints - this function serves exactly the same purpose except -- for a 'StdGen' generator instead of a 'PureMT' generator. {-# SPECIALIZE getRandomPrimFromRandomGenState :: Prim a -> State StdGen a #-} {-# SPECIALIZE getRandomPrimFromRandomGenState :: Monad m => Prim a -> StateT StdGen m a #-} getRandomPrimFromRandomGenState :: forall g m a. (RandomGen g, MonadState g m) => Prim a -> m a getRandomPrimFromRandomGenState = genPrim- where + where {-# INLINE genPrim #-} genPrim :: forall t. Prim t -> m t genPrim PrimWord8 = getThing (randomR (0, 0xff)) (fromIntegral :: Int -> Word8)@@ -100,12 +100,12 @@ genPrim PrimWord32 = getThing (randomR (0, 0xffffffff)) (fromInteger) genPrim PrimWord64 = getThing (randomR (0, 0xffffffffffffffff)) (fromInteger) genPrim PrimDouble = getThing (randomR (0, 0x000fffffffffffff)) (flip encodeFloat (-52))- {- not using the Random Double instance for 2 reasons. 1st, it only generates 32 bits of entropy, when - a [0,1) Double has room for 52. Second, it appears there's a bug where it can actually generate a + {- not using the Random Double instance for 2 reasons. 1st, it only generates 32 bits of entropy, when+ a [0,1) Double has room for 52. Second, it appears there's a bug where it can actually generate a negative number in the case where randomIvalInteger returns minBound::Int32. -} -- genPrim PrimDouble = getThing (randomR (0, 1.0)) (id) genPrim (PrimNByteInteger n) = getThing (randomR (0, iterate (*256) 1 !! n)) id- + {-# INLINE getThing #-} getThing :: forall b t. (g -> (b, g)) -> (b -> t) -> m t getThing thing f = do