Skip to content

Commit

Permalink
Merge pull request #155 from haskell/lehins/mkStdGen64
Browse files Browse the repository at this point in the history
Add `mkStdGen64`.
  • Loading branch information
lehins authored Nov 27, 2023
2 parents 5e1d5bb + d60ef3d commit fdcc5d2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/System/Random.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module System.Random
-- ** Standard pseudo-random number generator
, StdGen
, mkStdGen
, mkStdGen64
, initStdGen

-- ** Global standard pseudo-random number generator
Expand Down Expand Up @@ -113,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]
Expand All @@ -125,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]
Expand Down
11 changes: 11 additions & 0 deletions src/System/Random/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fdcc5d2

Please sign in to comment.