mwc-random 0.13.3.2 → 0.13.4.0
raw patch · 5 files changed
+75/−81 lines, 5 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- System.Random.MWC: instance (Variate a, Variate b) => Variate (a, b)
- System.Random.MWC: instance (Variate a, Variate b, Variate c) => Variate (a, b, c)
- System.Random.MWC: instance (Variate a, Variate b, Variate c, Variate d) => Variate (a, b, c, d)
- System.Random.MWC: instance Eq Seed
- System.Random.MWC: instance Show Seed
- System.Random.MWC: instance Typeable Seed
- System.Random.MWC: instance Variate Bool
- System.Random.MWC: instance Variate Double
- System.Random.MWC: instance Variate Float
- System.Random.MWC: instance Variate Int
- System.Random.MWC: instance Variate Int16
- System.Random.MWC: instance Variate Int32
- System.Random.MWC: instance Variate Int64
- System.Random.MWC: instance Variate Int8
- System.Random.MWC: instance Variate Word
- System.Random.MWC: instance Variate Word16
- System.Random.MWC: instance Variate Word32
- System.Random.MWC: instance Variate Word64
- System.Random.MWC: instance Variate Word8
+ System.Random.MWC: instance (System.Random.MWC.Variate a, System.Random.MWC.Variate b) => System.Random.MWC.Variate (a, b)
+ System.Random.MWC: instance (System.Random.MWC.Variate a, System.Random.MWC.Variate b, System.Random.MWC.Variate c) => System.Random.MWC.Variate (a, b, c)
+ System.Random.MWC: instance (System.Random.MWC.Variate a, System.Random.MWC.Variate b, System.Random.MWC.Variate c, System.Random.MWC.Variate d) => System.Random.MWC.Variate (a, b, c, d)
+ System.Random.MWC: instance GHC.Classes.Eq System.Random.MWC.Seed
+ System.Random.MWC: instance GHC.Show.Show System.Random.MWC.Seed
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Int.Int16
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Int.Int32
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Int.Int64
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Int.Int8
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Types.Bool
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Types.Double
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Types.Float
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Types.Int
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Types.Word
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Word.Word16
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Word.Word32
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Word.Word64
+ System.Random.MWC: instance System.Random.MWC.Variate GHC.Word.Word8
- System.Random.MWC: toSeed :: Vector v Word32 => v Word32 -> Seed
+ System.Random.MWC: toSeed :: (Vector v Word32) => v Word32 -> Seed
Files
- ChangeLog +5/−0
- System/Random/MWC.hs +63/−12
- System/Random/MWC/Distributions.hs +5/−2
- benchmarks/tsts.hs +0/−65
- mwc-random.cabal +2/−2
ChangeLog view
@@ -1,3 +1,8 @@+Changes in 0.13.4.0++ * withSystemRandom uses RtlGenRandom for seeding generator on windows++ Changes in 0.13.3.1 * primitive-0.6 compatibility
System/Random/MWC.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE BangPatterns, CPP, DeriveDataTypeable, FlexibleContexts,- MagicHash, Rank2Types, ScopedTypeVariables, TypeFamilies, UnboxedTuples #-}+ MagicHash, Rank2Types, ScopedTypeVariables, TypeFamilies, UnboxedTuples,+ ForeignFunctionInterface #-} -- | -- Module : System.Random.MWC -- Copyright : (c) 2009-2012 Bryan O'Sullivan@@ -103,14 +104,17 @@ import Control.Monad.Primitive (PrimBase) #endif import Control.Monad.ST (ST)-import Data.Bits (Bits, (.&.), (.|.), shiftL, shiftR, xor)+import Data.Bits ((.&.), (.|.), shiftL, shiftR, xor) import Data.Int (Int8, Int16, Int32, Int64) import Data.IORef (atomicModifyIORef, newIORef) import Data.Ratio ((%), numerator) import Data.Time.Clock.POSIX (getPOSIXTime) import Data.Typeable (Typeable) import Data.Vector.Generic (Vector)-import Data.Word (Word, Word8, Word16, Word32, Word64)+import Data.Word (Word8, Word16, Word32, Word64)+#if !MIN_VERSION_base(4,8,0)+import Data.Word (Word)+#endif import Foreign.Marshal.Alloc (allocaBytes) import Foreign.Marshal.Array (peekArray) import qualified Data.Vector.Generic as G@@ -120,7 +124,10 @@ import System.IO (IOMode(..), hGetBuf, hPutStrLn, stderr, withBinaryFile) import System.IO.Unsafe (unsafePerformIO) import qualified Control.Exception as E-+#if defined(mingw32_HOST_OS)+import Foreign.Ptr+import Foreign.C.Types+#endif -- | The class of types for which we can generate uniformly@@ -409,28 +416,68 @@ let n = fromIntegral (numerator t) :: Word64 return [fromIntegral c, fromIntegral n, fromIntegral (n `shiftR` 32)] --- | Acquire seed from /dev/urandom+-- | Acquire seed from the system entropy source. On Unix machines,+-- this will attempt to use @/dev/urandom@. On Windows, it will internally+-- use @RtlGenRandom@. acquireSeedSystem :: IO [Word32] acquireSeedSystem = do+#if !defined(mingw32_HOST_OS)+ -- Read 256 random Word32s from /dev/urandom let nbytes = 1024 random = "/dev/urandom" allocaBytes nbytes $ \buf -> do nread <- withBinaryFile random ReadMode $ \h -> hGetBuf h buf nbytes peekArray (nread `div` 4) buf+#else+ let nbytes = 1024+ -- Generate 256 random Word32s from RtlGenRandom+ allocaBytes nbytes $ \buf -> do+ ok <- c_RtlGenRandom buf (fromIntegral nbytes)+ if ok then return () else fail "Couldn't use RtlGenRandom"+ peekArray (nbytes `div` 4) buf +-- Note: on 64-bit Windows, the 'stdcall' calling convention+-- isn't supported, so we use 'ccall' instead.+#if defined(i386_HOST_ARCH)+# define WINDOWS_CCONV stdcall+#elif defined(x86_64_HOST_ARCH)+# define WINDOWS_CCONV ccall+#else+# error Unknown mingw32 architecture!+#endif++-- Note: On Windows, the typical convention would be to use+-- the CryptoGenRandom API in order to generate random data.+-- However, here we use 'SystemFunction036', AKA RtlGenRandom.+--+-- This is a commonly used API for this purpose; one bonus is+-- that it avoids having to bring in the CryptoAPI library,+-- and completely sidesteps the initialization cost of CryptoAPI.+--+-- While this function is technically "subject to change" that is+-- extremely unlikely in practice: rand_s in the Microsoft CRT uses+-- this, and they can't change it easily without also breaking+-- backwards compatibility with e.g. statically linked applications.+--+-- The name 'SystemFunction036' is the actual link-time name; the+-- display name is just for giggles, I guess.+--+-- See also:+-- - http://blogs.msdn.com/b/michael_howard/archive/2005/01/14/353379.aspx+-- - https://bugzilla.mozilla.org/show_bug.cgi?id=504270+--+foreign import WINDOWS_CCONV unsafe "SystemFunction036"+ c_RtlGenRandom :: Ptr a -> CULong -> IO Bool+#endif+ -- | Seed a PRNG with data from the system's fast source of--- pseudo-random numbers (\"@\/dev\/urandom@\" on Unix-like systems),--- then run the given action.+-- pseudo-random numbers (\"@\/dev\/urandom@\" on Unix-like systems or+-- @RtlGenRandom@ on Windows), then run the given action. -- -- This is a somewhat expensive function, and is intended to be called -- only occasionally (e.g. once per thread). You should use the `Gen` -- it creates to generate many random numbers.------ /Note/: on Windows, this code does not yet use the native--- Cryptographic API as a source of random numbers (it uses the system--- clock instead). As a result, the sequences it generates may not be--- highly independent. withSystemRandom :: #if MIN_VERSION_primitive(0,6,0) PrimBase m@@ -442,7 +489,11 @@ seed <- acquireSeedSystem `E.catch` \(_::E.IOException) -> do seen <- atomicModifyIORef warned ((,) True) unless seen $ E.handle (\(_::E.IOException) -> return ()) $ do+#if !defined(mingw32_HOST_OS) hPutStrLn stderr ("Warning: Couldn't open /dev/urandom")+#else+ hPutStrLn stderr ("Warning: Couldn't use RtlGenRandom")+#endif hPutStrLn stderr ("Warning: using system clock for seed instead " ++ "(quality will be lower)") acquireSeedTime
System/Random/MWC/Distributions.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE BangPatterns, GADTs, FlexibleContexts, ScopedTypeVariables #-}+{-# LANGUAGE BangPatterns, CPP, GADTs, FlexibleContexts, ScopedTypeVariables #-} -- | -- Module : System.Random.MWC.Distributions -- Copyright : (c) 2012 Bryan O'Sullivan@@ -41,7 +41,10 @@ import Control.Monad.Primitive (PrimMonad, PrimState) import Data.Bits ((.&.)) import Data.Foldable (foldl')-import Data.Traversable (Traversable,mapM)+#if !MIN_VERSION_base(4,8,0)+import Data.Traversable (Traversable)+#endif+import Data.Traversable (mapM) import Data.Word (Word32) import System.Random.MWC (Gen, uniform, uniformR) import qualified Data.Vector.Unboxed as I
− benchmarks/tsts.hs
@@ -1,65 +0,0 @@-{-# LANGUAGE BangPatterns #-}-import Control.Monad-import System.Random.MWC-import System.Random.MWC.Distributions--main = do- withSystemRandom $ \g -> replicateM_ (300*1000) $ do- --- !n <- normal 0 1 g- !n <- normal 0 2 g- !n <- normal 3 3 g- !n <- normal 2 4 g- !n <- normal 2 5 g- !n <- normal 1 6 g- !n <- normal 3 7 g- !n <- normal 3 8 g- !n <- normal 3 9 g- !n <- normal 3 10 g- --- !n <- normal 0 1 g- !n <- normal 0 2 g- !n <- normal 3 3 g- !n <- normal 2 4 g- !n <- normal 2 5 g- !n <- normal 1 6 g- !n <- normal 3 7 g- !n <- normal 3 8 g- !n <- normal 3 9 g- !n <- normal 3 10 g- --- !n <- normal 0 1 g- !n <- normal 0 2 g- !n <- normal 3 3 g- !n <- normal 2 4 g- !n <- normal 2 5 g- !n <- normal 1 6 g- !n <- normal 3 7 g- !n <- normal 3 8 g- !n <- normal 3 9 g- !n <- normal 3 10 g- --- !n <- normal 0 1 g- !n <- normal 0 2 g- !n <- normal 3 3 g- !n <- normal 2 4 g- !n <- normal 2 5 g- !n <- normal 1 6 g- !n <- normal 3 7 g- !n <- normal 3 8 g- !n <- normal 3 9 g- !n <- normal 3 10 g- --- !n <- normal 0 1 g- !n <- normal 0 2 g- !n <- normal 3 3 g- !n <- normal 2 4 g- !n <- normal 2 5 g- !n <- normal 1 6 g- !n <- normal 3 7 g- !n <- normal 3 8 g- !n <- normal 3 9 g- !n <- normal 3 10 g- --- return () :: IO ()-
mwc-random.cabal view
@@ -1,5 +1,5 @@ name: mwc-random-version: 0.13.3.2+version: 0.13.4.0 synopsis: Fast, high quality pseudo random number generation description: This package contains code for generating high quality random@@ -24,7 +24,7 @@ copyright: 2009, 2010, 2011 Bryan O'Sullivan category: Math, Statistics build-type: Simple-cabal-version: >= 1.8+cabal-version: >= 1.8.0.4 extra-source-files: ChangeLog README.markdown