diff --git a/System/Random.hs b/System/Random.hs index 3741ce0d3..8a93e5468 100644 --- a/System/Random.hs +++ b/System/Random.hs @@ -250,7 +250,7 @@ getStdGen :: MonadIO m => m StdGen getStdGen = liftIO $ readIORef theStdGen theStdGen :: IORef StdGen -theStdGen = unsafePerformIO $ SM.initSMGen >>= newIORef +theStdGen = unsafePerformIO $ SM.initSMGen >>= newIORef . StdGen {-# NOINLINE theStdGen #-} -- |Applies 'split' to the current global pseudo-random generator, diff --git a/System/Random/Internal.hs b/System/Random/Internal.hs index 76e101660..42682d395 100644 --- a/System/Random/Internal.hs +++ b/System/Random/Internal.hs @@ -32,7 +32,7 @@ module System.Random.Internal , Frozen(..) -- ** Standard pseudo-random number generator - , StdGen + , StdGen(..) , mkStdGen -- * Monadic adapters for pure pseudo-random number generators @@ -402,13 +402,16 @@ runPureGenST g action = runST $ runGenStateT g $ action -- | The standard pseudo-random number generator. -type StdGen = SM.SMGen +newtype StdGen = StdGen { unStdGen :: SM.SMGen } + deriving (Show) instance RandomGen StdGen where - next = SM.nextInt - genWord32 = SM.nextWord32 - genWord64 = SM.nextWord64 - split = SM.splitSMGen + next = second StdGen . SM.nextInt . unStdGen + genWord32 = second StdGen . SM.nextWord32 . unStdGen + genWord64 = second StdGen . SM.nextWord64 . unStdGen + split g = (StdGen g1, StdGen g2) + where + (g1, g2) = SM.splitSMGen $ unStdGen g instance RandomGen SM32.SMGen where next = SM32.nextInt @@ -418,7 +421,7 @@ instance RandomGen SM32.SMGen where -- | Constructs a 'StdGen' deterministically. mkStdGen :: Int -> StdGen -mkStdGen s = SM.mkSMGen $ fromIntegral s +mkStdGen = StdGen . SM.mkSMGen . fromIntegral -- | The class of types for which a uniformly distributed value can be drawn -- from all possible values of the type. diff --git a/System/Random/Monad.hs b/System/Random/Monad.hs index 309e8bfad..f5250e346 100644 --- a/System/Random/Monad.hs +++ b/System/Random/Monad.hs @@ -216,7 +216,7 @@ instance RandomGen r => RandomGenM STGen r s (ST s) where -- -- >>> import Data.Int (Int8) -- >>> runGenM (IOGen (mkStdGen 217)) (`uniformListM` 5) :: IO ([Int8], Frozen (IOGen StdGen)) --- ([-74,37,-50,-2,3],IOGen {unIOGen = SMGen 4273268533320920145 15251669095119325999}) +-- ([-74,37,-50,-2,3],IOGen {unIOGen = StdGen {unStdGen = SMGen 4273268533320920145 15251669095119325999}}) -- -- @since 1.2 runGenM :: MonadRandom g s m => Frozen g -> (g s -> m a) -> m (a, Frozen g)