pcg-random 0.1.2.0 → 0.1.3.0
raw patch · 7 files changed
+36/−47 lines, 7 filesdep +bytestringdep +entropy
Dependencies added: bytestring, entropy
Files
- CHANGELOG.md +5/−0
- pcg-random.cabal +7/−4
- src/System/Random/PCG.hs +3/−1
- src/System/Random/PCG/Class.hs +15/−42
- src/System/Random/PCG/Fast.hs +2/−0
- src/System/Random/PCG/Single.hs +2/−0
- src/System/Random/PCG/Unique.hs +2/−0
CHANGELOG.md view
@@ -1,3 +1,8 @@+### 0.1.3.0++* Use [entropy](http://hackage.haskell.org/package/entropy) package for+ system random generator.+ ### 0.1.2.0 * Allow primitive-0.6.
pcg-random.cabal view
@@ -1,5 +1,5 @@ name: pcg-random-version: 0.1.2.0+version: 0.1.3.0 synopsis: Haskell bindings to the PCG random number generator. description: PCG is a family of simple fast space-efficient statistically good@@ -41,11 +41,14 @@ System.Random.PCG.Unique System.Random.PCG.Single hs-source-dirs: src+ ghc-options: -Wall other-extensions: BangPatterns, CPP, ForeignFunctionInterface build-depends:- base >=4.3 && < 5,- primitive >=0.4 && < 0.7,- random >=1.0 && < 2.0,+ base >=4.3 && < 5,+ primitive >=0.4 && < 0.7,+ random >=1.0 && < 2.0,+ bytestring,+ entropy, time default-language: Haskell2010 cc-options:
src/System/Random/PCG.hs view
@@ -63,9 +63,11 @@ , uniformBF, uniformBD, uniformBBool ) where -import Data.Data+#if __GLASGOW_HASKELL__ < 710 import Control.Applicative+#endif import Control.Monad.Primitive+import Data.Data import Foreign import GHC.Generics import System.IO.Unsafe
src/System/Random/PCG/Class.hs view
@@ -50,19 +50,13 @@ #include "MachDeps.h" #endif -import Control.Monad-import Data.Bits-import Data.Int-import Data.Word-import Foreign (allocaBytes, peek)-import System.IO--import qualified Control.Exception as E-import Data.IORef (atomicModifyIORef, newIORef)-import Data.Ratio (numerator, (%))-import Data.Time.Clock.POSIX (getPOSIXTime)-import System.CPUTime (cpuTimePrecision, getCPUTime)-import System.IO.Unsafe (unsafePerformIO)+import Control.Monad+import Data.Bits+import Data.ByteString (useAsCString)+import Data.Int+import Data.Word+import Foreign (castPtr, peek)+import System.Entropy class Monad m => Generator g m where uniform1 :: (Word32 -> a) -> g -> m a@@ -422,36 +416,15 @@ -- IO randoms -devRandom :: IO Word64-devRandom =- allocaBytes 8 $ \buf -> do- nread <- withBinaryFile "/dev/urandom" ReadMode $ \h -> hGetBuf h buf 8- when (nread /= 8) $ error "unable to read from /dev/urandom"- peek buf---- Acquire seed from current time. This is horrible fall-back for--- Windows system.-acquireSeedTime :: IO Word64-acquireSeedTime = do- c <- (numerator . (%cpuTimePrecision)) `liftM` getCPUTime- t <- toRational `liftM` getPOSIXTime- let n = fromIntegral (numerator t) :: Word64- return $ wordsTo64Bit (fromIntegral c) (fromIntegral n)---- | Get a random number from system source. If \"@\/dev\/urandom@\" is--- not found return inferior random number from time.+-- | Generate a random number using 'System.Entropy'.+--+-- Use RDRAND if available and XOR with @\/dev\/urandom@ on Unix and+-- CryptAPI on Windows. This entropy is considered cryptographically+-- secure but not true entropy. sysRandom :: IO Word64-sysRandom =- devRandom `E.catch` \(_ :: E.IOException) -> do- seen <- atomicModifyIORef warned ((,) True)- unless seen $ E.handle (\(_::E.IOException) -> return ()) $ do- hPutStrLn stderr ("Warning: Couldn't open /dev/urandom")- hPutStrLn stderr ("Warning: using system clock for seed instead " ++- "(quality will be lower)")- acquireSeedTime- where- warned = unsafePerformIO $ newIORef False- {-# NOINLINE warned #-}+sysRandom = do+ bs <- getEntropy 8+ useAsCString bs $ peek . castPtr uniformRange :: ( Generator g m , Integral a, Bounded a, Variate a
src/System/Random/PCG/Fast.hs view
@@ -64,7 +64,9 @@ , uniformBF, uniformBD, uniformBBool ) where +#if __GLASGOW_HASKELL__ < 710 import Control.Applicative+#endif import Control.Monad.Primitive import Data.Data import Foreign
src/System/Random/PCG/Single.hs view
@@ -63,7 +63,9 @@ , uniformBF, uniformBD, uniformBBool ) where +#if __GLASGOW_HASKELL__ < 710 import Control.Applicative+#endif import Control.Monad.Primitive import Data.Data import Foreign
src/System/Random/PCG/Unique.hs view
@@ -58,7 +58,9 @@ , uniformBF, uniformBD, uniformBBool ) where +#if __GLASGOW_HASKELL__ < 710 import Data.Functor+#endif import Foreign import System.Random.PCG.Class