Commit 49a969da authored by haskell@brecknell.org's avatar haskell@brecknell.org
Browse files

thread-safe getStdRandom and newStdGen

parent 15c65fa2
......@@ -403,11 +403,7 @@ theStdGen = unsafePerformIO $ do
-- |Applies 'split' to the current global random generator,
-- updates it with one of the results, and returns the other.
newStdGen :: IO StdGen
newStdGen = do
rng <- getStdGen
let (a,b) = split rng
setStdGen a
return b
newStdGen = atomicModifyIORef theStdGen split
{- |Uses the supplied function to get a value from the current global
random generator, and updates the global generator with the new generator
......@@ -420,11 +416,8 @@ between 1 and 6:
-}
getStdRandom :: (StdGen -> (a,StdGen)) -> IO a
getStdRandom f = do
rng <- getStdGen
let (v, new_rng) = f rng
setStdGen new_rng
return v
getStdRandom f = atomicModifyIORef theStdGen (swap . f)
where swap (v,g) = (g,v)
{- $references
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment