From 207fe49c757acc45b51a8a1343c3f344ff1bbd2d Mon Sep 17 00:00:00 2001 From: Alexey Kuleshevich Date: Sun, 26 Nov 2023 23:56:59 +0100 Subject: [PATCH 1/2] Add `mkStdGen64`. Fixes #141 --- CHANGELOG.md | 4 +++- src/System/Random.hs | 1 + src/System/Random/Internal.hs | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ee59069..bf717705 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # 1.3.0 -* Add `uniformListRM`, `uniformList`, `uniformListR`, `uniforms` and `uniformRs`: [#154](https://github.com/haskell/random/pull/154) +* Add `mkStdGen64`: [#155](https://github.com/haskell/random/pull/155) +* Add `uniformListRM`, `uniformList`, `uniformListR`, `uniforms` and `uniformRs`: + [#154](https://github.com/haskell/random/pull/154) * Add compatibility with recently added `ByteArray` to `base`: [#153](https://github.com/haskell/random/pull/153) * Switch to using `ByteArray` for type class implementation instead of diff --git a/src/System/Random.hs b/src/System/Random.hs index a21a27c8..d074c62b 100644 --- a/src/System/Random.hs +++ b/src/System/Random.hs @@ -52,6 +52,7 @@ module System.Random -- ** Standard pseudo-random number generator , StdGen , mkStdGen + , mkStdGen64 , initStdGen -- ** Global standard pseudo-random number generator diff --git a/src/System/Random/Internal.hs b/src/System/Random/Internal.hs index 20b5b47d..e7c6354f 100644 --- a/src/System/Random/Internal.hs +++ b/src/System/Random/Internal.hs @@ -37,6 +37,7 @@ module System.Random.Internal -- ** Standard pseudo-random number generator , StdGen(..) , mkStdGen + , mkStdGen64 , theStdGen -- * Monadic adapters for pure pseudo-random number generators @@ -882,6 +883,16 @@ instance RandomGen SM32.SMGen where mkStdGen :: Int -> StdGen mkStdGen = StdGen . SM.mkSMGen . fromIntegral +-- | Constructs a 'StdGen' deterministically from a `Word64` seed. +-- +-- The difference between `mkStdGen` is that `mkStdGen64` will work the same on 64-bit and +-- 32-bit architectures, while the former can only use 32-bit of information for +-- initializing the psuedo-random number generator on 32-bit operating systems +-- +-- @since 1.3.0 +mkStdGen64 :: Word64 -> StdGen +mkStdGen64 = StdGen . SM.mkSMGen + -- | Global mutable veriable with `StdGen` theStdGen :: IORef StdGen theStdGen = unsafePerformIO $ SM.initSMGen >>= newIORef . StdGen From d60ef3dd49ffd58ba96c7cbd3b70d22f2d6e9d80 Mon Sep 17 00:00:00 2001 From: Alexey Kuleshevich Date: Mon, 27 Nov 2023 00:15:46 +0100 Subject: [PATCH 2/2] Fixup haddock for changes from #154 --- src/System/Random.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System/Random.hs b/src/System/Random.hs index d074c62b..9bbc2422 100644 --- a/src/System/Random.hs +++ b/src/System/Random.hs @@ -114,7 +114,7 @@ import qualified System.Random.SplitMix as SM -- -- >>> :{ -- let rolls :: RandomGen g => Int -> g -> [Word] --- rolls n = take n . unfoldr (Just . uniformR (1, 6)) +-- rolls n = fst . uniformListR n (1, 6) -- pureGen = mkStdGen 137 -- in -- rolls 10 pureGen :: [Word] @@ -126,7 +126,7 @@ import qualified System.Random.SplitMix as SM -- -- >>> :{ -- let rollsM :: StatefulGen g m => Int -> g -> m [Word] --- rollsM n = replicateM n . uniformRM (1, 6) +-- rollsM n = uniformListRM n (1, 6) -- pureGen = mkStdGen 137 -- in -- runStateGen_ pureGen (rollsM 10) :: [Word]