packages feed

random-source 0.3.0.5 → 0.3.0.6

raw patch · 12 files changed

+285/−283 lines, 12 files

Files

random-source.cabal view
@@ -1,5 +1,5 @@ name:                   random-source-version:                0.3.0.5+version:                0.3.0.6 stability:              provisional  cabal-version:          >= 1.6@@ -19,8 +19,10 @@                         easy to define new entropy sources in a way that                         is naturally forward-compatible.                         .-                        Changes in 0.3.0.5: Renamed some internal modules.  No other changes.+                        Changes in 0.3.0.6: Fixed overzealous fix in 0.3.0.5.  The people responsible for sacking the people who have been sacked, etc., have been sacked.                         .+                        Changes in 0.3.0.5: Renamed some internal modules and accidentally some external ones too.  Whoops.  Please don't use this version, it will only end in tears.+                        .                         Changes in 0.3.0.4: Fixed a typo that broke building                         with MTL-1                         .@@ -51,8 +53,8 @@                         Data.Random.Source.PureMT                         Data.Random.Source.Std                         Data.Random.Source.StdGen-                        Data.Random.Source.Internal.Words-                        Data.Random.Source.Internal.Source+                        Data.Random.Internal.Words+                        Data.Random.Internal.Source   other-modules:        Data.Random.Source.Internal.Prim                         Data.Random.Source.Internal.TH   
+ src/Data/Random/Internal/Source.hs view
@@ -0,0 +1,142 @@+{-# LANGUAGE+    TemplateHaskell, GADTs, RankNTypes,+    MultiParamTypeClasses, FlexibleInstances, FlexibleContexts+  #-}++module Data.Random.Internal.Source+    ( Prim(..)+    , MonadRandom(..)+    , RandomSource(..)+    , GetPrim(..)+    ) where++import Data.Random.Source.Internal.Prim+import Data.Word++-- |A typeclass for monads with a chosen source of entropy.  For example,+-- 'RVar' is such a monad - the source from which it is (eventually) sampled+-- is the only source from which a random variable is permitted to draw, so+-- when directly requesting entropy for a random variable these functions+-- are used.+--+-- Minimum implementation is either the internal 'getRandomPrim' or all+-- other functions.  Additionally, this class's interface is subject to +-- extension at any time, so it is very, very strongly recommended that+-- the 'monadRandom' Template Haskell function be used to implement this +-- function rather than directly implementing it.  That function takes care+-- of choosing default implementations for any missing functions; as long as+-- at least one function is implemented, it will derive sensible +-- implementations of all others.+-- +-- To use 'monadRandom', just wrap your instance declaration as follows (and+-- enable the TemplateHaskell and GADTs language extensions):+--+-- > $(monadRandom [d|+-- >         instance MonadRandom FooM where+-- >             getRandomDouble = return pi+-- >             getRandomWord16 = return 4+-- >             {- etc... -}+-- >     |])+class Monad m => MonadRandom m where+    -- |Generate a random value corresponding to the specified primitive.+    -- +    -- This is an internal interface; use at your own risk.  It may change or+    -- disappear at any time.+    getRandomPrim :: Prim t -> m t+    getRandomPrim PrimWord8             = getRandomWord8+    getRandomPrim PrimWord16            = getRandomWord16+    getRandomPrim PrimWord32            = getRandomWord32+    getRandomPrim PrimWord64            = getRandomWord64+    getRandomPrim PrimDouble            = getRandomDouble+    getRandomPrim (PrimNByteInteger n)  = getRandomNByteInteger n+    +    -- |Generate a uniformly distributed random 'Word8'+    getRandomWord8 :: m Word8+    getRandomWord8 = getRandomPrim PrimWord8+    +    -- |Generate a uniformly distributed random 'Word16'+    getRandomWord16 :: m Word16+    getRandomWord16 = getRandomPrim PrimWord16+    +    -- |Generate a uniformly distributed random 'Word32'+    getRandomWord32 :: m Word32+    getRandomWord32 = getRandomPrim PrimWord32+    +    -- |Generate a uniformly distributed random 'Word64'+    getRandomWord64 :: m Word64+    getRandomWord64 = getRandomPrim PrimWord64+    +    -- |Generate a uniformly distributed random 'Double' in the range 0 <= U < 1+    getRandomDouble :: m Double+    getRandomDouble = getRandomPrim PrimDouble+    +    -- |Generate a uniformly distributed random 'Integer' in the range 0 <= U < 256^n+    getRandomNByteInteger :: MonadRandom m => Int -> m Integer+    getRandomNByteInteger n = getRandomPrim (PrimNByteInteger n)+++-- |A source of entropy which can be used in the given monad.+-- +-- See also 'MonadRandom'.+-- +-- Minimum implementation is either the internal 'getRandomPrimFrom' or all+-- other functions.  Additionally, this class's interface is subject to +-- extension at any time, so it is very, very strongly recommended that+-- the 'randomSource' Template Haskell function be used to implement this +-- function rather than directly implementing it.  That function takes care+-- of choosing default implementations for any missing functions; as long as+-- at least one function is implemented, it will derive sensible +-- implementations of all others.+-- +-- To use 'randomSource', just wrap your instance declaration as follows (and+-- enable the TemplateHaskell, MultiParamTypeClasses and GADTs language+-- extensions, as well as any others required by your instances, such as+-- FlexibleInstances):+--+-- > $(randomSource [d|+-- >         instance RandomSource FooM Bar where+-- >             {- at least one RandomSource function... -}+-- >     |])+class Monad m => RandomSource m s where+    -- |Generate a random value corresponding to the specified primitive.+    -- +    -- This is an internal interface; use at your own risk.  It may change or+    -- disappear at any time.+    getRandomPrimFrom :: s -> Prim t -> m t+    getRandomPrimFrom src PrimWord8             = getRandomWord8From  src+    getRandomPrimFrom src PrimWord16            = getRandomWord16From src+    getRandomPrimFrom src PrimWord32            = getRandomWord32From src+    getRandomPrimFrom src PrimWord64            = getRandomWord64From src+    getRandomPrimFrom src PrimDouble            = getRandomDoubleFrom src+    getRandomPrimFrom src (PrimNByteInteger n)  = getRandomNByteIntegerFrom src n+    +    +    -- |Generate a uniformly distributed random 'Word8'+    getRandomWord8From :: s -> m Word8+    getRandomWord8From src = getRandomPrimFrom src PrimWord8+    +    -- |Generate a uniformly distributed random 'Word16'+    getRandomWord16From :: s -> m Word16+    getRandomWord16From src = getRandomPrimFrom src PrimWord16+    +    -- |Generate a uniformly distributed random 'Word32'+    getRandomWord32From :: s -> m Word32+    getRandomWord32From src = getRandomPrimFrom src PrimWord32+    +    -- |Generate a uniformly distributed random 'Word64'+    getRandomWord64From :: s -> m Word64+    getRandomWord64From src = getRandomPrimFrom src PrimWord64+    +    -- |Generate a uniformly distributed random 'Double' in the range 0 <= U < 1+    getRandomDoubleFrom :: s -> m Double+    getRandomDoubleFrom src = getRandomPrimFrom src PrimDouble+    +    -- |Generate a uniformly distributed random 'Integer' in the range 0 <= U < 256^n+    getRandomNByteIntegerFrom :: s -> Int -> m Integer+    getRandomNByteIntegerFrom src n = getRandomPrimFrom src (PrimNByteInteger n)++-- |This type provides a way to define a 'RandomSource' for a monad without actually+-- having to declare an instance.+newtype GetPrim m = GetPrim (forall t. Prim t -> m t)+instance Monad m => RandomSource m (GetPrim m) where+    getRandomPrimFrom (GetPrim f) = f
+ src/Data/Random/Internal/Words.hs view
@@ -0,0 +1,129 @@+-- |A few little functions I found myself writing inline over and over again.+module Data.Random.Internal.Words where++import Data.Bits+import Data.Word+import Foreign.Marshal  (allocaBytes)+import Foreign.Ptr      (castPtr)+import Foreign.Storable (peek, pokeByteOff)+import System.IO.Unsafe (unsafePerformIO)++-- TODO: add a build flag for endianness-invariance, or just find a way+-- to make sure these operations all do the right thing without costing +-- anything extra at runtime++{-# INLINE buildWord16 #-}+-- |Build a word out of 2 bytes.  No promises are made regarding the order+-- in which the bytes are stuffed.  Note that this means that a 'RandomSource'+-- or 'MonadRandom' making use of the default definition of 'getRandomWord', etc.,+-- may return different random values on different platforms when started +-- with the same seed, depending on the platform's endianness.+buildWord16 :: Word8 -> Word8 -> Word16+buildWord16 b0 b1+    = unsafePerformIO . allocaBytes 2 $ \p -> do+        pokeByteOff p 0 b0+        pokeByteOff p 1 b1+        peek (castPtr p)++{-# INLINE buildWord32 #-}+-- |Build a word out of 4 bytes.  No promises are made regarding the order+-- in which the bytes are stuffed.  Note that this means that a 'RandomSource'+-- or 'MonadRandom' making use of the default definition of 'getRandomWord', etc.,+-- may return different random values on different platforms when started +-- with the same seed, depending on the platform's endianness.+buildWord32 :: Word8 -> Word8 -> Word8 -> Word8 -> Word32+buildWord32 b0 b1 b2 b3+    = unsafePerformIO . allocaBytes 4 $ \p -> do+        pokeByteOff p 0 b0+        pokeByteOff p 1 b1+        pokeByteOff p 2 b2+        pokeByteOff p 3 b3+        peek (castPtr p)++{-# INLINE buildWord32' #-}+buildWord32' :: Word16 -> Word16 -> Word32+buildWord32' w0 w1+    = unsafePerformIO . allocaBytes 4 $ \p -> do+        pokeByteOff p 0 w0+        pokeByteOff p 2 w1+        peek (castPtr p)++{-# INLINE buildWord64 #-}+-- |Build a word out of 8 bytes.  No promises are made regarding the order+-- in which the bytes are stuffed.  Note that this means that a 'RandomSource'+-- or 'MonadRandom' making use of the default definition of 'getRandomWord', etc.,+-- may return different random values on different platforms when started +-- with the same seed, depending on the platform's endianness.+buildWord64 :: Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word64+buildWord64 b0 b1 b2 b3 b4 b5 b6 b7+    = unsafePerformIO . allocaBytes 8 $ \p -> do+        pokeByteOff p 0 b0+        pokeByteOff p 1 b1+        pokeByteOff p 2 b2+        pokeByteOff p 3 b3+        pokeByteOff p 4 b4+        pokeByteOff p 5 b5+        pokeByteOff p 6 b6+        pokeByteOff p 7 b7+        peek (castPtr p)++{-# INLINE buildWord64' #-}+buildWord64' :: Word16 -> Word16 -> Word16 -> Word16 -> Word64+buildWord64' w0 w1 w2 w3+    = unsafePerformIO . allocaBytes 8 $ \p -> do+        pokeByteOff p 0 w0+        pokeByteOff p 2 w1+        pokeByteOff p 4 w2+        pokeByteOff p 6 w3+        peek (castPtr p)++{-# INLINE buildWord64'' #-}+buildWord64'' :: Word32 -> Word32 -> Word64+buildWord64'' w0 w1+    = unsafePerformIO . allocaBytes 8 $ \p -> do+        pokeByteOff p 0 w0+        pokeByteOff p 4 w1+        peek (castPtr p)++{-# INLINE word32ToFloat #-}+-- |Pack the low 23 bits from a 'Word32' into a 'Float' in the range [0,1).+-- Used to convert a 'stdUniform' 'Word32' to a 'stdUniform' 'Double'.+word32ToFloat :: Word32 -> Float+word32ToFloat x = (encodeFloat $! toInteger (x .&. 0x007fffff {- 2^23-1 -} )) $ (-23)++{-# INLINE word32ToFloatWithExcess #-}+-- |Same as word32ToFloat, but also return the unused bits (as the 9+-- least significant bits of a 'Word32')+word32ToFloatWithExcess :: Word32 -> (Float, Word32)+word32ToFloatWithExcess x = (word32ToFloat x, x `shiftR` 23)++{-# INLINE wordToFloat #-}+-- |Pack the low 23 bits from a 'Word64' into a 'Float' in the range [0,1).+-- Used to convert a 'stdUniform' 'Word64' to a 'stdUniform' 'Double'.+wordToFloat :: Word64 -> Float+wordToFloat x = (encodeFloat $! toInteger (x .&. 0x007fffff {- 2^23-1 -} )) $ (-23)++{-# INLINE wordToFloatWithExcess #-}+-- |Same as wordToFloat, but also return the unused bits (as the 41+-- least significant bits of a 'Word64')+wordToFloatWithExcess :: Word64 -> (Float, Word64)+wordToFloatWithExcess x = (wordToFloat x, x `shiftR` 23)++{-# INLINE wordToDouble #-}+-- |Pack the low 52 bits from a 'Word64' into a 'Double' in the range [0,1).+-- Used to convert a 'stdUniform' 'Word64' to a 'stdUniform' 'Double'.+wordToDouble :: Word64 -> Double+wordToDouble x = (encodeFloat $! toInteger (x .&. 0x000fffffffffffff {- 2^52-1 -})) $ (-52)++{-# INLINE word32ToDouble #-}+-- |Pack a 'Word32' into a 'Double' in the range [0,1).  Note that a Double's +-- mantissa is 52 bits, so this does not fill all of them.+word32ToDouble :: Word32 -> Double+word32ToDouble x = (encodeFloat $! toInteger x) $ (-32)++{-# INLINE wordToDoubleWithExcess #-}+-- |Same as wordToDouble, but also return the unused bits (as the 12+-- least significant bits of a 'Word64')+wordToDoubleWithExcess :: Word64 -> (Double, Word64)+wordToDoubleWithExcess x = (wordToDouble x, x `shiftR` 52)+
src/Data/Random/Source.hs view
@@ -26,7 +26,7 @@  import Data.Word -import Data.Random.Source.Internal.Source+import Data.Random.Internal.Source import Data.Random.Source.Internal.TH  $(randomSource
src/Data/Random/Source/IO.hs view
@@ -6,7 +6,7 @@ -- "Data.Random.Source.DevRandom". module Data.Random.Source.IO () where -import Data.Random.Source.Internal.Source+import Data.Random.Internal.Source  #ifndef windows 
− src/Data/Random/Source/Internal/Source.hs
@@ -1,142 +0,0 @@-{-# LANGUAGE-    TemplateHaskell, GADTs, RankNTypes,-    MultiParamTypeClasses, FlexibleInstances, FlexibleContexts-  #-}--module Data.Random.Source.Internal.Source-    ( Prim(..)-    , MonadRandom(..)-    , RandomSource(..)-    , GetPrim(..)-    ) where--import Data.Random.Source.Internal.Prim-import Data.Word---- |A typeclass for monads with a chosen source of entropy.  For example,--- 'RVar' is such a monad - the source from which it is (eventually) sampled--- is the only source from which a random variable is permitted to draw, so--- when directly requesting entropy for a random variable these functions--- are used.------ Minimum implementation is either the internal 'getRandomPrim' or all--- other functions.  Additionally, this class's interface is subject to --- extension at any time, so it is very, very strongly recommended that--- the 'monadRandom' Template Haskell function be used to implement this --- function rather than directly implementing it.  That function takes care--- of choosing default implementations for any missing functions; as long as--- at least one function is implemented, it will derive sensible --- implementations of all others.--- --- To use 'monadRandom', just wrap your instance declaration as follows (and--- enable the TemplateHaskell and GADTs language extensions):------ > $(monadRandom [d|--- >         instance MonadRandom FooM where--- >             getRandomDouble = return pi--- >             getRandomWord16 = return 4--- >             {- etc... -}--- >     |])-class Monad m => MonadRandom m where-    -- |Generate a random value corresponding to the specified primitive.-    -- -    -- This is an internal interface; use at your own risk.  It may change or-    -- disappear at any time.-    getRandomPrim :: Prim t -> m t-    getRandomPrim PrimWord8             = getRandomWord8-    getRandomPrim PrimWord16            = getRandomWord16-    getRandomPrim PrimWord32            = getRandomWord32-    getRandomPrim PrimWord64            = getRandomWord64-    getRandomPrim PrimDouble            = getRandomDouble-    getRandomPrim (PrimNByteInteger n)  = getRandomNByteInteger n-    -    -- |Generate a uniformly distributed random 'Word8'-    getRandomWord8 :: m Word8-    getRandomWord8 = getRandomPrim PrimWord8-    -    -- |Generate a uniformly distributed random 'Word16'-    getRandomWord16 :: m Word16-    getRandomWord16 = getRandomPrim PrimWord16-    -    -- |Generate a uniformly distributed random 'Word32'-    getRandomWord32 :: m Word32-    getRandomWord32 = getRandomPrim PrimWord32-    -    -- |Generate a uniformly distributed random 'Word64'-    getRandomWord64 :: m Word64-    getRandomWord64 = getRandomPrim PrimWord64-    -    -- |Generate a uniformly distributed random 'Double' in the range 0 <= U < 1-    getRandomDouble :: m Double-    getRandomDouble = getRandomPrim PrimDouble-    -    -- |Generate a uniformly distributed random 'Integer' in the range 0 <= U < 256^n-    getRandomNByteInteger :: MonadRandom m => Int -> m Integer-    getRandomNByteInteger n = getRandomPrim (PrimNByteInteger n)----- |A source of entropy which can be used in the given monad.--- --- See also 'MonadRandom'.--- --- Minimum implementation is either the internal 'getRandomPrimFrom' or all--- other functions.  Additionally, this class's interface is subject to --- extension at any time, so it is very, very strongly recommended that--- the 'randomSource' Template Haskell function be used to implement this --- function rather than directly implementing it.  That function takes care--- of choosing default implementations for any missing functions; as long as--- at least one function is implemented, it will derive sensible --- implementations of all others.--- --- To use 'randomSource', just wrap your instance declaration as follows (and--- enable the TemplateHaskell, MultiParamTypeClasses and GADTs language--- extensions, as well as any others required by your instances, such as--- FlexibleInstances):------ > $(randomSource [d|--- >         instance RandomSource FooM Bar where--- >             {- at least one RandomSource function... -}--- >     |])-class Monad m => RandomSource m s where-    -- |Generate a random value corresponding to the specified primitive.-    -- -    -- This is an internal interface; use at your own risk.  It may change or-    -- disappear at any time.-    getRandomPrimFrom :: s -> Prim t -> m t-    getRandomPrimFrom src PrimWord8             = getRandomWord8From  src-    getRandomPrimFrom src PrimWord16            = getRandomWord16From src-    getRandomPrimFrom src PrimWord32            = getRandomWord32From src-    getRandomPrimFrom src PrimWord64            = getRandomWord64From src-    getRandomPrimFrom src PrimDouble            = getRandomDoubleFrom src-    getRandomPrimFrom src (PrimNByteInteger n)  = getRandomNByteIntegerFrom src n-    -    -    -- |Generate a uniformly distributed random 'Word8'-    getRandomWord8From :: s -> m Word8-    getRandomWord8From src = getRandomPrimFrom src PrimWord8-    -    -- |Generate a uniformly distributed random 'Word16'-    getRandomWord16From :: s -> m Word16-    getRandomWord16From src = getRandomPrimFrom src PrimWord16-    -    -- |Generate a uniformly distributed random 'Word32'-    getRandomWord32From :: s -> m Word32-    getRandomWord32From src = getRandomPrimFrom src PrimWord32-    -    -- |Generate a uniformly distributed random 'Word64'-    getRandomWord64From :: s -> m Word64-    getRandomWord64From src = getRandomPrimFrom src PrimWord64-    -    -- |Generate a uniformly distributed random 'Double' in the range 0 <= U < 1-    getRandomDoubleFrom :: s -> m Double-    getRandomDoubleFrom src = getRandomPrimFrom src PrimDouble-    -    -- |Generate a uniformly distributed random 'Integer' in the range 0 <= U < 256^n-    getRandomNByteIntegerFrom :: s -> Int -> m Integer-    getRandomNByteIntegerFrom src n = getRandomPrimFrom src (PrimNByteInteger n)---- |This type provides a way to define a 'RandomSource' for a monad without actually--- having to declare an instance.-newtype GetPrim m = GetPrim (forall t. Prim t -> m t)-instance Monad m => RandomSource m (GetPrim m) where-    getRandomPrimFrom (GetPrim f) = f
src/Data/Random/Source/Internal/TH.hs view
@@ -7,8 +7,8 @@ import Data.List import Data.Maybe import Data.Monoid-import Data.Random.Source.Internal.Source (Prim(..), MonadRandom(..), RandomSource(..))-import Data.Random.Source.Internal.Words+import Data.Random.Internal.Source (Prim(..), MonadRandom(..), RandomSource(..))+import Data.Random.Internal.Words import Language.Haskell.TH import Language.Haskell.TH.Extras import qualified Language.Haskell.TH.FlexibleDefaults as FD
− src/Data/Random/Source/Internal/Words.hs
@@ -1,129 +0,0 @@--- |A few little functions I found myself writing inline over and over again.-module Data.Random.Source.Internal.Words where--import Data.Bits-import Data.Word-import Foreign.Marshal  (allocaBytes)-import Foreign.Ptr      (castPtr)-import Foreign.Storable (peek, pokeByteOff)-import System.IO.Unsafe (unsafePerformIO)---- TODO: add a build flag for endianness-invariance, or just find a way--- to make sure these operations all do the right thing without costing --- anything extra at runtime--{-# INLINE buildWord16 #-}--- |Build a word out of 2 bytes.  No promises are made regarding the order--- in which the bytes are stuffed.  Note that this means that a 'RandomSource'--- or 'MonadRandom' making use of the default definition of 'getRandomWord', etc.,--- may return different random values on different platforms when started --- with the same seed, depending on the platform's endianness.-buildWord16 :: Word8 -> Word8 -> Word16-buildWord16 b0 b1-    = unsafePerformIO . allocaBytes 2 $ \p -> do-        pokeByteOff p 0 b0-        pokeByteOff p 1 b1-        peek (castPtr p)--{-# INLINE buildWord32 #-}--- |Build a word out of 4 bytes.  No promises are made regarding the order--- in which the bytes are stuffed.  Note that this means that a 'RandomSource'--- or 'MonadRandom' making use of the default definition of 'getRandomWord', etc.,--- may return different random values on different platforms when started --- with the same seed, depending on the platform's endianness.-buildWord32 :: Word8 -> Word8 -> Word8 -> Word8 -> Word32-buildWord32 b0 b1 b2 b3-    = unsafePerformIO . allocaBytes 4 $ \p -> do-        pokeByteOff p 0 b0-        pokeByteOff p 1 b1-        pokeByteOff p 2 b2-        pokeByteOff p 3 b3-        peek (castPtr p)--{-# INLINE buildWord32' #-}-buildWord32' :: Word16 -> Word16 -> Word32-buildWord32' w0 w1-    = unsafePerformIO . allocaBytes 4 $ \p -> do-        pokeByteOff p 0 w0-        pokeByteOff p 2 w1-        peek (castPtr p)--{-# INLINE buildWord64 #-}--- |Build a word out of 8 bytes.  No promises are made regarding the order--- in which the bytes are stuffed.  Note that this means that a 'RandomSource'--- or 'MonadRandom' making use of the default definition of 'getRandomWord', etc.,--- may return different random values on different platforms when started --- with the same seed, depending on the platform's endianness.-buildWord64 :: Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word64-buildWord64 b0 b1 b2 b3 b4 b5 b6 b7-    = unsafePerformIO . allocaBytes 8 $ \p -> do-        pokeByteOff p 0 b0-        pokeByteOff p 1 b1-        pokeByteOff p 2 b2-        pokeByteOff p 3 b3-        pokeByteOff p 4 b4-        pokeByteOff p 5 b5-        pokeByteOff p 6 b6-        pokeByteOff p 7 b7-        peek (castPtr p)--{-# INLINE buildWord64' #-}-buildWord64' :: Word16 -> Word16 -> Word16 -> Word16 -> Word64-buildWord64' w0 w1 w2 w3-    = unsafePerformIO . allocaBytes 8 $ \p -> do-        pokeByteOff p 0 w0-        pokeByteOff p 2 w1-        pokeByteOff p 4 w2-        pokeByteOff p 6 w3-        peek (castPtr p)--{-# INLINE buildWord64'' #-}-buildWord64'' :: Word32 -> Word32 -> Word64-buildWord64'' w0 w1-    = unsafePerformIO . allocaBytes 8 $ \p -> do-        pokeByteOff p 0 w0-        pokeByteOff p 4 w1-        peek (castPtr p)--{-# INLINE word32ToFloat #-}--- |Pack the low 23 bits from a 'Word32' into a 'Float' in the range [0,1).--- Used to convert a 'stdUniform' 'Word32' to a 'stdUniform' 'Double'.-word32ToFloat :: Word32 -> Float-word32ToFloat x = (encodeFloat $! toInteger (x .&. 0x007fffff {- 2^23-1 -} )) $ (-23)--{-# INLINE word32ToFloatWithExcess #-}--- |Same as word32ToFloat, but also return the unused bits (as the 9--- least significant bits of a 'Word32')-word32ToFloatWithExcess :: Word32 -> (Float, Word32)-word32ToFloatWithExcess x = (word32ToFloat x, x `shiftR` 23)--{-# INLINE wordToFloat #-}--- |Pack the low 23 bits from a 'Word64' into a 'Float' in the range [0,1).--- Used to convert a 'stdUniform' 'Word64' to a 'stdUniform' 'Double'.-wordToFloat :: Word64 -> Float-wordToFloat x = (encodeFloat $! toInteger (x .&. 0x007fffff {- 2^23-1 -} )) $ (-23)--{-# INLINE wordToFloatWithExcess #-}--- |Same as wordToFloat, but also return the unused bits (as the 41--- least significant bits of a 'Word64')-wordToFloatWithExcess :: Word64 -> (Float, Word64)-wordToFloatWithExcess x = (wordToFloat x, x `shiftR` 23)--{-# INLINE wordToDouble #-}--- |Pack the low 52 bits from a 'Word64' into a 'Double' in the range [0,1).--- Used to convert a 'stdUniform' 'Word64' to a 'stdUniform' 'Double'.-wordToDouble :: Word64 -> Double-wordToDouble x = (encodeFloat $! toInteger (x .&. 0x000fffffffffffff {- 2^52-1 -})) $ (-52)--{-# INLINE word32ToDouble #-}--- |Pack a 'Word32' into a 'Double' in the range [0,1).  Note that a Double's --- mantissa is 52 bits, so this does not fill all of them.-word32ToDouble :: Word32 -> Double-word32ToDouble x = (encodeFloat $! toInteger x) $ (-32)--{-# INLINE wordToDoubleWithExcess #-}--- |Same as wordToDouble, but also return the unused bits (as the 12--- least significant bits of a 'Word64')-wordToDoubleWithExcess :: Word64 -> (Double, Word64)-wordToDoubleWithExcess x = (wordToDouble x, x `shiftR` 52)-
src/Data/Random/Source/MWC.hs view
@@ -15,7 +15,7 @@     , save, restore     ) where -import Data.Random.Source.Internal.Words+import Data.Random.Internal.Words import Data.Random.Source import System.Random.MWC import Control.Monad.ST
src/Data/Random/Source/PureMT.hs view
@@ -26,7 +26,7 @@  import Control.Monad.State import qualified Control.Monad.State.Strict as S-import Data.Random.Source.Internal.Source+import Data.Random.Internal.Source import Data.Random.Source.Internal.TH import Data.StateRef import System.Random.Mersenne.Pure64
src/Data/Random/Source/Std.hs view
@@ -7,7 +7,7 @@  module Data.Random.Source.Std where -import Data.Random.Source.Internal.Source+import Data.Random.Internal.Source  -- |A token representing the \"standard\" entropy source in a 'MonadRandom' -- monad.  Its sole purpose is to make the following true (when the types check):
src/Data/Random/Source/StdGen.hs view
@@ -21,7 +21,7 @@     , getRandomPrimFromRandomGenState     ) where -import Data.Random.Source.Internal.Source+import Data.Random.Internal.Source import System.Random import Control.Monad.State import qualified Control.Monad.ST.Strict as S