{-# LANGUAGE CPP #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE Trustworthy #-}
module System.Random
(
RandomGen(..)
, uniform
, uniformR
, genByteString
, Random(..)
, Uniform
, UniformRange
, Finite
, StdGen
, mkStdGen
, initStdGen
, getStdRandom
, getStdGen
, setStdGen
, newStdGen
, randomIO
, randomRIO
) where
import Control.Arrow
import Control.Monad.IO.Class
import Control.Monad.State.Strict
import Data.ByteString (ByteString)
import Data.Int
import Data.IORef
import Data.Word
import Foreign.C.Types
import GHC.Exts
import System.Random.GFinite (Finite)
import System.Random.Internal
import qualified System.Random.SplitMix as SM
uniform :: (RandomGen g, Uniform a) => g -> (a, g)
uniform :: forall g a. (RandomGen g, Uniform a) => g -> (a, g)
uniform g
g = forall g a.
RandomGen g =>
g -> (StateGenM g -> State g a) -> (a, g)
runStateGen g
g forall a g (m :: * -> *). (Uniform a, StatefulGen g m) => g -> m a
uniformM
{-# INLINE uniform #-}
uniformR :: (RandomGen g, UniformRange a) => (a, a) -> g -> (a, g)
uniformR :: forall g a. (RandomGen g, UniformRange a) => (a, a) -> g -> (a, g)
uniformR (a, a)
r g
g = forall g a.
RandomGen g =>
g -> (StateGenM g -> State g a) -> (a, g)
runStateGen g
g (forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
uniformRM (a, a)
r)
{-# INLINE uniformR #-}
genByteString :: RandomGen g => Int -> g -> (ByteString, g)
genByteString :: forall g. RandomGen g => Int -> g -> (ByteString, g)
genByteString Int
n g
g = forall g a.
RandomGen g =>
g -> (forall s. StateGenM g -> StateT g (ST s) a) -> (a, g)
runStateGenST g
g (forall g (m :: * -> *). StatefulGen g m => Int -> g -> m ByteString
uniformByteStringM Int
n)
{-# INLINE genByteString #-}
class Random a where
{-# INLINE randomR #-}
randomR :: RandomGen g => (a, a) -> g -> (a, g)
default randomR :: (RandomGen g, UniformRange a) => (a, a) -> g -> (a, g)
randomR (a, a)
r g
g = forall g a.
RandomGen g =>
g -> (StateGenM g -> State g a) -> (a, g)
runStateGen g
g (forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
uniformRM (a, a)
r)
{-# INLINE random #-}
random :: RandomGen g => g -> (a, g)
default random :: (RandomGen g, Uniform a) => g -> (a, g)
random g
g = forall g a.
RandomGen g =>
g -> (StateGenM g -> State g a) -> (a, g)
runStateGen g
g forall a g (m :: * -> *). (Uniform a, StatefulGen g m) => g -> m a
uniformM
{-# INLINE randomRs #-}
randomRs :: RandomGen g => (a,a) -> g -> [a]
randomRs (a, a)
ival g
g = forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\a -> b -> b
cons b
_nil -> forall g a as.
RandomGen g =>
(a -> as -> as) -> (g -> (a, g)) -> g -> as
buildRandoms a -> b -> b
cons (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a, a)
ival) g
g)
{-# INLINE randoms #-}
randoms :: RandomGen g => g -> [a]
randoms g
g = forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\a -> b -> b
cons b
_nil -> forall g a as.
RandomGen g =>
(a -> as -> as) -> (g -> (a, g)) -> g -> as
buildRandoms a -> b -> b
cons forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
g)
{-# INLINE buildRandoms #-}
buildRandoms :: RandomGen g
=> (a -> as -> as)
-> (g -> (a,g))
-> g
-> as
buildRandoms :: forall g a as.
RandomGen g =>
(a -> as -> as) -> (g -> (a, g)) -> g -> as
buildRandoms a -> as -> as
cons g -> (a, g)
rand = g -> as
go
where
go :: g -> as
go g
g = a
x seq :: forall a b. a -> b -> b
`seq` (a
x a -> as -> as
`cons` g -> as
go g
g') where (a
x,g
g') = g -> (a, g)
rand g
g
instance Random Integer where
random :: forall g. RandomGen g => g -> (Integer, g)
random = forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first (forall a. Integral a => a -> Integer
toInteger :: Int -> Integer) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a g. (Random a, RandomGen g) => g -> (a, g)
random
{-# INLINE random #-}
instance Random Int8
instance Random Int16
instance Random Int32
instance Random Int64
instance Random Int
instance Random Word
instance Random Word8
instance Random Word16
instance Random Word32
instance Random Word64
#if __GLASGOW_HASKELL__ >= 802
instance Random CBool
#endif
instance Random CChar
instance Random CSChar
instance Random CUChar
instance Random CShort
instance Random CUShort
instance Random CInt
instance Random CUInt
instance Random CLong
instance Random CULong
instance Random CPtrdiff
instance Random CSize
instance Random CWchar
instance Random CSigAtomic
instance Random CLLong
instance Random CULLong
instance Random CIntPtr
instance Random CUIntPtr
instance Random CIntMax
instance Random CUIntMax
instance Random CFloat where
randomR :: forall g. RandomGen g => (CFloat, CFloat) -> g -> (CFloat, g)
randomR (CFloat, CFloat)
r = coerce :: forall a b. Coercible a b => a -> b
coerce forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (coerce :: forall a b. Coercible a b => a -> b
coerce (CFloat, CFloat)
r :: (Float, Float))
{-# INLINE randomR #-}
random :: forall g. RandomGen g => g -> (CFloat, g)
random = forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Float -> CFloat
CFloat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a g. (Random a, RandomGen g) => g -> (a, g)
random
{-# INLINE random #-}
instance Random CDouble where
randomR :: forall g. RandomGen g => (CDouble, CDouble) -> g -> (CDouble, g)
randomR (CDouble, CDouble)
r = coerce :: forall a b. Coercible a b => a -> b
coerce forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (coerce :: forall a b. Coercible a b => a -> b
coerce (CDouble, CDouble)
r :: (Double, Double))
{-# INLINE randomR #-}
random :: forall g. RandomGen g => g -> (CDouble, g)
random = forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Double -> CDouble
CDouble forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a g. (Random a, RandomGen g) => g -> (a, g)
random
{-# INLINE random #-}
instance Random Char
instance Random Bool
instance Random Double where
randomR :: forall g. RandomGen g => (Double, Double) -> g -> (Double, g)
randomR (Double, Double)
r g
g = forall g a.
RandomGen g =>
g -> (StateGenM g -> State g a) -> (a, g)
runStateGen g
g (forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
uniformRM (Double, Double)
r)
{-# INLINE randomR #-}
random :: forall g. RandomGen g => g -> (Double, g)
random g
g = forall g a.
RandomGen g =>
g -> (StateGenM g -> State g a) -> (a, g)
runStateGen g
g (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Double
1 forall a. Num a => a -> a -> a
-) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall g (m :: * -> *). StatefulGen g m => g -> m Double
uniformDouble01M)
{-# INLINE random #-}
instance Random Float where
randomR :: forall g. RandomGen g => (Float, Float) -> g -> (Float, g)
randomR (Float, Float)
r g
g = forall g a.
RandomGen g =>
g -> (StateGenM g -> State g a) -> (a, g)
runStateGen g
g (forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
uniformRM (Float, Float)
r)
{-# INLINE randomR #-}
random :: forall g. RandomGen g => g -> (Float, g)
random g
g = forall g a.
RandomGen g =>
g -> (StateGenM g -> State g a) -> (a, g)
runStateGen g
g (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Float
1 forall a. Num a => a -> a -> a
-) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall g (m :: * -> *). StatefulGen g m => g -> m Float
uniformFloat01M)
{-# INLINE random #-}
initStdGen :: MonadIO m => m StdGen
initStdGen :: forall (m :: * -> *). MonadIO m => m StdGen
initStdGen = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (SMGen -> StdGen
StdGen forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO SMGen
SM.initSMGen)
instance (Random a, Random b) => Random (a, b) where
randomR :: forall g. RandomGen g => ((a, b), (a, b)) -> g -> ((a, b), g)
randomR ((a
al, b
bl), (a
ah, b
bh)) = forall s a. State s a -> s -> (a, s)
runState forall a b. (a -> b) -> a -> b
$
(,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a
al, a
ah)) forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (b
bl, b
bh))
{-# INLINE randomR #-}
random :: forall g. RandomGen g => g -> ((a, b), g)
random = forall s a. State s a -> s -> (a, s)
runState forall a b. (a -> b) -> a -> b
$ (,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
{-# INLINE random #-}
instance (Random a, Random b, Random c) => Random (a, b, c) where
randomR :: forall g.
RandomGen g =>
((a, b, c), (a, b, c)) -> g -> ((a, b, c), g)
randomR ((a
al, b
bl, c
cl), (a
ah, b
bh, c
ch)) = forall s a. State s a -> s -> (a, s)
runState forall a b. (a -> b) -> a -> b
$
(,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a
al, a
ah))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (b
bl, b
bh))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (c
cl, c
ch))
{-# INLINE randomR #-}
random :: forall g. RandomGen g => g -> ((a, b, c), g)
random = forall s a. State s a -> s -> (a, s)
runState forall a b. (a -> b) -> a -> b
$ (,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
{-# INLINE random #-}
instance (Random a, Random b, Random c, Random d) => Random (a, b, c, d) where
randomR :: forall g.
RandomGen g =>
((a, b, c, d), (a, b, c, d)) -> g -> ((a, b, c, d), g)
randomR ((a
al, b
bl, c
cl, d
dl), (a
ah, b
bh, c
ch, d
dh)) = forall s a. State s a -> s -> (a, s)
runState forall a b. (a -> b) -> a -> b
$
(,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a
al, a
ah))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (b
bl, b
bh))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (c
cl, c
ch))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (d
dl, d
dh))
{-# INLINE randomR #-}
random :: forall g. RandomGen g => g -> ((a, b, c, d), g)
random = forall s a. State s a -> s -> (a, s)
runState forall a b. (a -> b) -> a -> b
$
(,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
{-# INLINE random #-}
instance (Random a, Random b, Random c, Random d, Random e) => Random (a, b, c, d, e) where
randomR :: forall g.
RandomGen g =>
((a, b, c, d, e), (a, b, c, d, e)) -> g -> ((a, b, c, d, e), g)
randomR ((a
al, b
bl, c
cl, d
dl, e
el), (a
ah, b
bh, c
ch, d
dh, e
eh)) = forall s a. State s a -> s -> (a, s)
runState forall a b. (a -> b) -> a -> b
$
(,,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a
al, a
ah))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (b
bl, b
bh))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (c
cl, c
ch))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (d
dl, d
dh))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (e
el, e
eh))
{-# INLINE randomR #-}
random :: forall g. RandomGen g => g -> ((a, b, c, d, e), g)
random = forall s a. State s a -> s -> (a, s)
runState forall a b. (a -> b) -> a -> b
$
(,,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
{-# INLINE random #-}
instance (Random a, Random b, Random c, Random d, Random e, Random f) =>
Random (a, b, c, d, e, f) where
randomR :: forall g.
RandomGen g =>
((a, b, c, d, e, f), (a, b, c, d, e, f))
-> g -> ((a, b, c, d, e, f), g)
randomR ((a
al, b
bl, c
cl, d
dl, e
el, f
fl), (a
ah, b
bh, c
ch, d
dh, e
eh, f
fh)) = forall s a. State s a -> s -> (a, s)
runState forall a b. (a -> b) -> a -> b
$
(,,,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a
al, a
ah))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (b
bl, b
bh))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (c
cl, c
ch))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (d
dl, d
dh))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (e
el, e
eh))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (f
fl, f
fh))
{-# INLINE randomR #-}
random :: forall g. RandomGen g => g -> ((a, b, c, d, e, f), g)
random = forall s a. State s a -> s -> (a, s)
runState forall a b. (a -> b) -> a -> b
$
(,,,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
{-# INLINE random #-}
instance (Random a, Random b, Random c, Random d, Random e, Random f, Random g) =>
Random (a, b, c, d, e, f, g) where
randomR :: forall g.
RandomGen g =>
((a, b, c, d, e, f, g), (a, b, c, d, e, f, g))
-> g -> ((a, b, c, d, e, f, g), g)
randomR ((a
al, b
bl, c
cl, d
dl, e
el, f
fl, g
gl), (a
ah, b
bh, c
ch, d
dh, e
eh, f
fh, g
gh)) = forall s a. State s a -> s -> (a, s)
runState forall a b. (a -> b) -> a -> b
$
(,,,,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a
al, a
ah))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (b
bl, b
bh))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (c
cl, c
ch))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (d
dl, d
dh))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (e
el, e
eh))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (f
fl, f
fh))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (g
gl, g
gh))
{-# INLINE randomR #-}
random :: forall g. RandomGen g => g -> ((a, b, c, d, e, f, g), g)
random = forall s a. State s a -> s -> (a, s)
runState forall a b. (a -> b) -> a -> b
$
(,,,,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state forall a g. (Random a, RandomGen g) => g -> (a, g)
random
{-# INLINE random #-}
setStdGen :: MonadIO m => StdGen -> m ()
setStdGen :: forall (m :: * -> *). MonadIO m => StdGen -> m ()
setStdGen = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IORef a -> a -> IO ()
writeIORef IORef StdGen
theStdGen
getStdGen :: MonadIO m => m StdGen
getStdGen :: forall (m :: * -> *). MonadIO m => m StdGen
getStdGen = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> IO a
readIORef IORef StdGen
theStdGen
newStdGen :: MonadIO m => m StdGen
newStdGen :: forall (m :: * -> *). MonadIO m => m StdGen
newStdGen = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef StdGen
theStdGen forall g. RandomGen g => g -> (g, g)
split
getStdRandom :: MonadIO m => (StdGen -> (a, StdGen)) -> m a
getStdRandom :: forall (m :: * -> *) a. MonadIO m => (StdGen -> (a, StdGen)) -> m a
getStdRandom StdGen -> (a, StdGen)
f = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef StdGen
theStdGen (forall {b} {a}. (b, a) -> (a, b)
swap forall b c a. (b -> c) -> (a -> b) -> a -> c
. StdGen -> (a, StdGen)
f)
where swap :: (b, a) -> (a, b)
swap (b
v, a
g) = (a
g, b
v)
randomRIO :: (Random a, MonadIO m) => (a, a) -> m a
randomRIO :: forall a (m :: * -> *). (Random a, MonadIO m) => (a, a) -> m a
randomRIO (a, a)
range = forall (m :: * -> *) a. MonadIO m => (StdGen -> (a, StdGen)) -> m a
getStdRandom (forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a, a)
range)
randomIO :: (Random a, MonadIO m) => m a
randomIO :: forall a (m :: * -> *). (Random a, MonadIO m) => m a
randomIO = forall (m :: * -> *) a. MonadIO m => (StdGen -> (a, StdGen)) -> m a
getStdRandom forall a g. (Random a, RandomGen g) => g -> (a, g)
random