Skip to content

Commit

Permalink
Add ability to split PrimGen
Browse files Browse the repository at this point in the history
  • Loading branch information
lehins committed Mar 13, 2020
1 parent 9564db4 commit b918908
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions System/Random.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module System.Random
, PrimGen
, runPrimGenST
, runPrimGenIO
, splitPrimGen
, atomicPrimGen

-- ** The global random number generator
Expand Down Expand Up @@ -360,6 +361,17 @@ atomicPrimGen op (PrimGen gVar) =
case op g of
(a, g') -> (g', a)


-- | Split `PrimGen` into atomically updated current generator and a newly created that is
-- returned.
--
-- @since 1.2
splitPrimGen ::
(RandomGen g, PrimMonad m)
=> PrimGen (PrimState m) g
-> m (PrimGen (PrimState m) g)
splitPrimGen = atomicPrimGen split >=> restore

runPrimGenST :: RandomGen g => g -> (forall s . PrimGen s g -> ST s a) -> (a, g)
runPrimGenST g action = runST $ do
primGen :: PrimGen s g <- restore g
Expand Down

0 comments on commit b918908

Please sign in to comment.