-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Random-number generation monad. -- -- Support for computations which consume random values. @package MonadRandom @version 0.6 -- | The MonadRandom, MonadSplit, and MonadInterleave -- classes. -- -- -- -- This module also defines convenience functions for sampling from a -- given collection of values, either uniformly or according to given -- weights. module Control.Monad.Random.Class -- | With a source of random number supply in hand, the MonadRandom -- class allows the programmer to extract random values of a variety of -- types. class (Monad m) => MonadRandom m -- | Takes a range (lo,hi) and a random number generator g, -- and returns a computation that returns a random value uniformly -- distributed in the closed interval [lo,hi], together with a new -- generator. It is unspecified what happens if lo>hi. For -- continuous types there is no requirement that the values lo and -- hi are ever produced, but they may be, depending on the -- implementation and the interval. -- -- See randomR for details. getRandomR :: (MonadRandom m, Random a) => (a, a) -> m a -- | The same as getRandomR, but using a default range determined by -- the type: -- -- -- -- See random for details. getRandom :: (MonadRandom m, Random a) => m a -- | Plural variant of getRandomR, producing an infinite list of -- random values instead of returning a new generator. -- -- See randomRs for details. getRandomRs :: (MonadRandom m, Random a) => (a, a) -> m [a] -- | Plural variant of getRandom, producing an infinite list of -- random values instead of returning a new generator. -- -- See randoms for details. getRandoms :: (MonadRandom m, Random a) => m [a] -- | The class MonadSplit proivides a way to specify a random number -- generator that can be split into two new generators. -- -- This class is not very useful in practice: typically, one cannot -- actually do anything with a generator. It remains here to avoid -- breaking existing code unnecessarily. For a more practically useful -- interface, see MonadInterleave. class (Monad m) => MonadSplit g m | m -> g -- | The getSplit operation allows one to obtain two distinct random -- number generators. -- -- See split for details. getSplit :: MonadSplit g m => m g -- | The class MonadInterleave proivides a convenient interface atop -- a split operation on a random generator. class MonadRandom m => MonadInterleave m -- | If x :: m a is a computation in some random monad, then -- interleave x works by splitting the generator, running -- x using one half, and using the other half as the final -- generator state of interleave x (replacing whatever the final -- generator state otherwise would have been). This means that -- computation needing random values which comes after interleave -- x does not necessarily depend on the computation of x. -- For example: -- --
--   >>> evalRandIO $ snd <$> ((,) <$> undefined <*> getRandom)
--   *** Exception: Prelude.undefined
--   >>> evalRandIO $ snd <$> ((,) <$> interleave undefined <*> getRandom)
--   6192322188769041625
--   
-- -- This can be used, for example, to allow random computations to run in -- parallel, or to create lazy infinite structures of random values. In -- the example below, the infinite tree randTree cannot be -- evaluated lazily: even though it is cut off at two levels deep by -- hew 2, the random value in the right subtree still depends on -- generation of all the random values in the (infinite) left subtree, -- even though they are ultimately unneeded. Inserting a call to -- interleave, as in randTreeI, solves the problem: the -- generator splits at each Node, so random values in the left -- and right subtrees are generated independently. -- --
--   data Tree = Leaf | Node Int Tree Tree deriving Show
--   
--   hew :: Int -> Tree -> Tree
--   hew 0 _    = Leaf
--   hew _ Leaf = Leaf
--   hew n (Node x l r) = Node x (hew (n-1) l) (hew (n-1) r)
--   
--   randTree :: Rand StdGen Tree
--   randTree = Node <$> getRandom <*> randTree <*> randTree
--   
--   randTreeI :: Rand StdGen Tree
--   randTreeI = interleave $ Node <$> getRandom <*> randTreeI <*> randTreeI
--   
-- --
--   >>> hew 2 <$> evalRandIO randTree
--   Node 2168685089479838995 (Node (-1040559818952481847) Leaf Leaf) (Node ^CInterrupted.
--   >>> hew 2 <$> evalRandIO randTreeI
--   Node 8243316398511136358 (Node 4139784028141790719 Leaf Leaf) (Node 4473998613878251948 Leaf Leaf)
--   
interleave :: MonadInterleave m => m a -> m a -- | Sample a random value from a weighted list. The list must be non-empty -- and the total weight must be non-zero. fromList :: MonadRandom m => [(a, Rational)] -> m a -- | Sample a random value from a weighted list. Return Nothing if -- the list is empty or the total weight is nonpositive. fromListMay :: MonadRandom m => [(a, Rational)] -> m (Maybe a) -- | Sample a value uniformly from a nonempty collection of elements. uniform :: (Foldable t, MonadRandom m) => t a -> m a -- | Sample a value uniformly from a collection of elements. Return -- Nothing if the collection is empty. uniformMay :: (Foldable t, MonadRandom m) => t a -> m (Maybe a) -- | Sample a random value from a weighted nonempty collection of elements. -- Crashes with a call to error if the collection is empty or -- the total weight is zero. weighted :: (Foldable t, MonadRandom m) => t (a, Rational) -> m a -- | Sample a random value from a weighted collection of elements. Returns -- Nothing if the collection is empty or the total weight is -- zero. weightedMay :: (Foldable t, MonadRandom m) => t (a, Rational) -> m (Maybe a) instance Control.Monad.Random.Class.MonadInterleave m => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.Cont.ContT r m) instance Control.Monad.Random.Class.MonadInterleave m => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.Except.ExceptT e m) instance Control.Monad.Random.Class.MonadInterleave m => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.Identity.IdentityT m) instance Control.Monad.Random.Class.MonadInterleave m => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.Maybe.MaybeT m) instance (GHC.Base.Monoid w, Control.Monad.Random.Class.MonadInterleave m) => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.RWS.Lazy.RWST r w s m) instance (GHC.Base.Monoid w, Control.Monad.Random.Class.MonadInterleave m) => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.RWS.Strict.RWST r w s m) instance Control.Monad.Random.Class.MonadInterleave m => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.Reader.ReaderT r m) instance Control.Monad.Random.Class.MonadInterleave m => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.State.Lazy.StateT s m) instance Control.Monad.Random.Class.MonadInterleave m => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.State.Strict.StateT s m) instance (GHC.Base.Monoid w, Control.Monad.Random.Class.MonadInterleave m) => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.Writer.Lazy.WriterT w m) instance (GHC.Base.Monoid w, Control.Monad.Random.Class.MonadInterleave m) => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.Writer.Strict.WriterT w m) instance Control.Monad.Random.Class.MonadSplit System.Random.Internal.StdGen GHC.Types.IO instance Control.Monad.Random.Class.MonadSplit g m => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.Cont.ContT r m) instance Control.Monad.Random.Class.MonadSplit g m => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.Except.ExceptT e m) instance Control.Monad.Random.Class.MonadSplit g m => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.Identity.IdentityT m) instance Control.Monad.Random.Class.MonadSplit g m => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.Maybe.MaybeT m) instance (GHC.Base.Monoid w, Control.Monad.Random.Class.MonadSplit g m) => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.RWS.Lazy.RWST r w s m) instance (GHC.Base.Monoid w, Control.Monad.Random.Class.MonadSplit g m) => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.RWS.Strict.RWST r w s m) instance Control.Monad.Random.Class.MonadSplit g m => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.Reader.ReaderT r m) instance Control.Monad.Random.Class.MonadSplit g m => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.State.Lazy.StateT s m) instance Control.Monad.Random.Class.MonadSplit g m => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.State.Strict.StateT s m) instance (GHC.Base.Monoid w, Control.Monad.Random.Class.MonadSplit g m) => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.Writer.Lazy.WriterT w m) instance (GHC.Base.Monoid w, Control.Monad.Random.Class.MonadSplit g m) => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.Writer.Strict.WriterT w m) instance Control.Monad.Random.Class.MonadRandom GHC.Types.IO instance Control.Monad.Random.Class.MonadRandom m => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.Cont.ContT r m) instance Control.Monad.Random.Class.MonadRandom m => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.Except.ExceptT e m) instance Control.Monad.Random.Class.MonadRandom m => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.Identity.IdentityT m) instance Control.Monad.Random.Class.MonadRandom m => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.Maybe.MaybeT m) instance (GHC.Base.Monoid w, Control.Monad.Random.Class.MonadRandom m) => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.RWS.Lazy.RWST r w s m) instance (GHC.Base.Monoid w, Control.Monad.Random.Class.MonadRandom m) => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.RWS.Strict.RWST r w s m) instance Control.Monad.Random.Class.MonadRandom m => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.Reader.ReaderT r m) instance Control.Monad.Random.Class.MonadRandom m => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.State.Lazy.StateT s m) instance Control.Monad.Random.Class.MonadRandom m => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.State.Strict.StateT s m) instance (Control.Monad.Random.Class.MonadRandom m, GHC.Base.Monoid w) => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.Writer.Lazy.WriterT w m) instance (Control.Monad.Random.Class.MonadRandom m, GHC.Base.Monoid w) => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.Writer.Strict.WriterT w m) -- | Strict random monads, passing a random number generator through a -- computation. See below for examples. -- -- In this version, sequencing of computations is strict (but -- computations are not strict in the state unless you force it with seq -- or the like). For a lazy version with the same interface, see -- Control.Monad.Trans.Random.Lazy. module Control.Monad.Trans.Random.Strict -- | A random monad parameterized by the type g of the generator -- to carry. -- -- The return function leaves the generator unchanged, while -- >>= uses the final generator of the first computation as -- the initial generator of the second. type Rand g = RandT g Identity -- | Construct a random monad computation from a function. (The inverse of -- runRand.) liftRand :: (g -> (a, g)) -> Rand g a -- | Unwrap a random monad computation as a function. (The inverse of -- liftRand.) runRand :: Rand g a -> g -> (a, g) -- | Evaluate a random computation with the given initial generator and -- return the final value, discarding the final generator. -- -- evalRand :: Rand g a -> g -> a -- | Evaluate a random computation with the given initial generator and -- return the final generator, discarding the final value. -- -- execRand :: Rand g a -> g -> g -- | Map both the return value and final generator of a computation using -- the given function. -- -- mapRand :: ((a, g) -> (b, g)) -> Rand g a -> Rand g b -- | withRand f m executes action m on a generator -- modified by applying f. -- -- withRand :: (g -> g) -> Rand g a -> Rand g a -- | Evaluate a random computation in the IO monad, splitting the -- global standard generator to get a new one for the computation. evalRandIO :: Rand StdGen a -> IO a -- | A random transformer monad parameterized by: -- -- -- -- The return function leaves the generator unchanged, while -- >>= uses the final generator of the first computation as -- the initial generator of the second. data RandT g m a -- | Construct a random monad computation from an impure function. (The -- inverse of runRandT.) liftRandT :: (g -> m (a, g)) -> RandT g m a -- | Unwrap a random monad computation as an impure function. (The inverse -- of liftRandT.) runRandT :: RandT g m a -> g -> m (a, g) -- | Evaluate a random computation with the given initial generator and -- return the final value, discarding the final generator. -- -- evalRandT :: Monad m => RandT g m a -> g -> m a -- | Evaluate a random computation with the given initial generator and -- return the final generator, discarding the final value. -- -- execRandT :: Monad m => RandT g m a -> g -> m g -- | Map both the return value and final generator of a computation using -- the given function. -- -- mapRandT :: (m (a, g) -> n (b, g)) -> RandT g m a -> RandT g n b -- | withRandT f m executes action m on a -- generator modified by applying f. -- -- withRandT :: (g -> g) -> RandT g m a -> RandT g m a -- | Evaluate a random computation that is embedded in the IO monad, -- splitting the global standard generator to get a new one for the -- computation. evalRandTIO :: MonadIO m => RandT StdGen m a -> m a -- | Uniform lifting of a callCC operation to the new monad. This -- version rolls back to the original state on entering the continuation. liftCallCC :: CallCC m (a, g) (b, g) -> CallCC (RandT g m) a b -- | In-situ lifting of a callCC operation to the new monad. This -- version uses the current state on entering the continuation. It does -- not satisfy the uniformity property (see -- Control.Monad.Signatures). liftCallCC' :: CallCC m (a, g) (b, g) -> CallCC (RandT g m) a b -- | Lift a catchE operation to the new monad. liftCatch :: Catch e m (a, g) -> Catch e (RandT g m) a -- | Lift a listen operation to the new monad. liftListen :: Monad m => Listen w m (a, g) -> Listen w (RandT g m) a -- | Lift a pass operation to the new monad. liftPass :: Monad m => Pass w m (a, g) -> Pass w (RandT g m) a -- | A proxy that carries information about the type of generator to use -- with RandT monad and its StatefulGen instance. data RandGen g RandGen :: RandGen g -- | A RandT runner that allows using it with StatefulGen -- restricted actions. Returns the outcome of random computation and the -- new pseudo-random-number generator -- --
--   >>> withRandGen (mkStdGen 2021) uniformM :: IO (Int, StdGen)
--   (6070831465987696718,StdGen {unStdGen = SMGen 4687568268719557181 4805600293067301895})
--   
withRandGen :: g -> (RandGen g -> RandT g m a) -> m (a, g) -- | Same as withRandGen, but discards the resulting generator. -- --
--   >>> withRandGen_ (mkStdGen 2021) uniformM :: IO Int
--   6070831465987696718
--   
withRandGen_ :: Monad m => g -> (RandGen g -> RandT g m a) -> m a instance Control.Monad.Writer.Class.MonadWriter w m => Control.Monad.Writer.Class.MonadWriter w (Control.Monad.Trans.Random.Strict.RandT g m) instance Control.Monad.Reader.Class.MonadReader r m => Control.Monad.Reader.Class.MonadReader r (Control.Monad.Trans.Random.Strict.RandT g m) instance Control.Monad.Fix.MonadFix m => Control.Monad.Fix.MonadFix (Control.Monad.Trans.Random.Strict.RandT g m) instance Control.Monad.IO.Class.MonadIO m => Control.Monad.IO.Class.MonadIO (Control.Monad.Trans.Random.Strict.RandT g m) instance Control.Monad.Trans.Class.MonadTrans (Control.Monad.Trans.Random.Strict.RandT g) instance GHC.Base.MonadPlus m => GHC.Base.MonadPlus (Control.Monad.Trans.Random.Strict.RandT g m) instance GHC.Base.Monad m => GHC.Base.Monad (Control.Monad.Trans.Random.Strict.RandT g m) instance GHC.Base.MonadPlus m => GHC.Base.Alternative (Control.Monad.Trans.Random.Strict.RandT g m) instance GHC.Base.Monad m => GHC.Base.Applicative (Control.Monad.Trans.Random.Strict.RandT g m) instance GHC.Base.Functor m => GHC.Base.Functor (Control.Monad.Trans.Random.Strict.RandT g m) instance (GHC.Base.Monad m, System.Random.Internal.RandomGen g) => System.Random.Internal.StatefulGen (Control.Monad.Trans.Random.Strict.RandGen g) (Control.Monad.Trans.Random.Strict.RandT g m) instance (GHC.Base.Monad m, System.Random.Internal.RandomGen g) => System.Random.Stateful.RandomGenM (Control.Monad.Trans.Random.Strict.RandGen g) g (Control.Monad.Trans.Random.Strict.RandT g m) instance Control.Monad.Cont.Class.MonadCont m => Control.Monad.Cont.Class.MonadCont (Control.Monad.Trans.Random.Strict.RandT g m) instance Control.Monad.Error.Class.MonadError e m => Control.Monad.Error.Class.MonadError e (Control.Monad.Trans.Random.Strict.RandT g m) instance (Control.Monad.Reader.Class.MonadReader r m, Control.Monad.Writer.Class.MonadWriter w m, Control.Monad.State.Class.MonadState s m) => Control.Monad.RWS.Class.MonadRWS r w s (Control.Monad.Trans.Random.Strict.RandT g m) instance (System.Random.Internal.RandomGen g, GHC.Base.Monad m) => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.Random.Strict.RandT g m) instance (System.Random.Internal.RandomGen g, GHC.Base.Monad m) => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.Random.Strict.RandT g m) instance (GHC.Base.Monad m, System.Random.Internal.RandomGen g) => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.Random.Strict.RandT g m) instance Control.Monad.State.Class.MonadState s m => Control.Monad.State.Class.MonadState s (Control.Monad.Trans.Random.Strict.RandT g m) instance Control.Monad.Primitive.PrimMonad m => Control.Monad.Primitive.PrimMonad (Control.Monad.Trans.Random.Strict.RandT s m) instance Control.Monad.Fail.MonadFail m => Control.Monad.Fail.MonadFail (Control.Monad.Trans.Random.Strict.RandT g m) -- | Lazy random monads, passing a random number generator through a -- computation. See below for examples. -- -- For a strict version with the same interface, see -- Control.Monad.Trans.Random.Strict. module Control.Monad.Trans.Random.Lazy -- | A random monad parameterized by the type g of the generator -- to carry. -- -- The return function leaves the generator unchanged, while -- >>= uses the final generator of the first computation as -- the initial generator of the second. type Rand g = RandT g Identity -- | Construct a random monad computation from a function. (The inverse of -- runRand.) liftRand :: (g -> (a, g)) -> Rand g a -- | Unwrap a random monad computation as a function. (The inverse of -- liftRand.) runRand :: Rand g a -> g -> (a, g) -- | Evaluate a random computation with the given initial generator and -- return the final value, discarding the final generator. -- -- evalRand :: Rand g a -> g -> a -- | Evaluate a random computation with the given initial generator and -- return the final generator, discarding the final value. -- -- execRand :: Rand g a -> g -> g -- | Map both the return value and final generator of a computation using -- the given function. -- -- mapRand :: ((a, g) -> (b, g)) -> Rand g a -> Rand g b -- | withRand f m executes action m on a generator -- modified by applying f. -- -- withRand :: (g -> g) -> Rand g a -> Rand g a -- | Evaluate a random computation in the IO monad, splitting the -- global standard generator to get a new one for the computation. evalRandIO :: Rand StdGen a -> IO a -- | A random transformer monad parameterized by: -- -- -- -- The return function leaves the generator unchanged, while -- >>= uses the final generator of the first computation as -- the initial generator of the second. data RandT g m a -- | Construct a random monad computation from an impure function. (The -- inverse of runRandT.) liftRandT :: (g -> m (a, g)) -> RandT g m a -- | Unwrap a random monad computation as an impure function. (The inverse -- of liftRandT.) runRandT :: RandT g m a -> g -> m (a, g) -- | Evaluate a random computation with the given initial generator and -- return the final value, discarding the final generator. -- -- evalRandT :: Monad m => RandT g m a -> g -> m a -- | Evaluate a random computation with the given initial generator and -- return the final generator, discarding the final value. -- -- execRandT :: Monad m => RandT g m a -> g -> m g -- | Map both the return value and final generator of a computation using -- the given function. -- -- mapRandT :: (m (a, g) -> n (b, g)) -> RandT g m a -> RandT g n b -- | withRandT f m executes action m on a -- generator modified by applying f. -- -- withRandT :: (g -> g) -> RandT g m a -> RandT g m a -- | Uniform lifting of a callCC operation to the new monad. This -- version rolls back to the original state on entering the continuation. liftCallCC :: CallCC m (a, g) (b, g) -> CallCC (RandT g m) a b -- | In-situ lifting of a callCC operation to the new monad. This -- version uses the current state on entering the continuation. It does -- not satisfy the uniformity property (see -- Control.Monad.Signatures). liftCallCC' :: CallCC m (a, g) (b, g) -> CallCC (RandT g m) a b -- | Lift a catchE operation to the new monad. liftCatch :: Catch e m (a, g) -> Catch e (RandT g m) a -- | Lift a listen operation to the new monad. liftListen :: Monad m => Listen w m (a, g) -> Listen w (RandT g m) a -- | Lift a pass operation to the new monad. liftPass :: Monad m => Pass w m (a, g) -> Pass w (RandT g m) a -- | Evaluate a random computation that is embedded in the IO monad, -- splitting the global standard generator to get a new one for the -- computation. evalRandTIO :: MonadIO m => RandT StdGen m a -> m a -- | A proxy that carries information about the type of generator to use -- with RandT monad and its StatefulGen instance. data RandGen g RandGen :: RandGen g -- | A RandT runner that allows using it with StatefulGen -- restricted actions. Returns the outcome of random computation and the -- new pseudo-random-number generator -- --
--   >>> withRandGen (mkStdGen 2021) uniformM :: IO (Int, StdGen)
--   (6070831465987696718,StdGen {unStdGen = SMGen 4687568268719557181 4805600293067301895})
--   
withRandGen :: g -> (RandGen g -> RandT g m a) -> m (a, g) -- | Same as withRandGen, but discards the resulting generator. -- --
--   >>> withRandGen_ (mkStdGen 2021) uniformM :: IO Int
--   6070831465987696718
--   
withRandGen_ :: Monad m => g -> (RandGen g -> RandT g m a) -> m a instance Control.Monad.Writer.Class.MonadWriter w m => Control.Monad.Writer.Class.MonadWriter w (Control.Monad.Trans.Random.Lazy.RandT g m) instance Control.Monad.Reader.Class.MonadReader r m => Control.Monad.Reader.Class.MonadReader r (Control.Monad.Trans.Random.Lazy.RandT g m) instance Control.Monad.Fix.MonadFix m => Control.Monad.Fix.MonadFix (Control.Monad.Trans.Random.Lazy.RandT g m) instance Control.Monad.IO.Class.MonadIO m => Control.Monad.IO.Class.MonadIO (Control.Monad.Trans.Random.Lazy.RandT g m) instance Control.Monad.Trans.Class.MonadTrans (Control.Monad.Trans.Random.Lazy.RandT g) instance GHC.Base.MonadPlus m => GHC.Base.MonadPlus (Control.Monad.Trans.Random.Lazy.RandT g m) instance GHC.Base.Monad m => GHC.Base.Monad (Control.Monad.Trans.Random.Lazy.RandT g m) instance GHC.Base.MonadPlus m => GHC.Base.Alternative (Control.Monad.Trans.Random.Lazy.RandT g m) instance GHC.Base.Monad m => GHC.Base.Applicative (Control.Monad.Trans.Random.Lazy.RandT g m) instance GHC.Base.Functor m => GHC.Base.Functor (Control.Monad.Trans.Random.Lazy.RandT g m) instance Control.Monad.Cont.Class.MonadCont m => Control.Monad.Cont.Class.MonadCont (Control.Monad.Trans.Random.Lazy.RandT g m) instance Control.Monad.Error.Class.MonadError e m => Control.Monad.Error.Class.MonadError e (Control.Monad.Trans.Random.Lazy.RandT g m) instance (Control.Monad.Reader.Class.MonadReader r m, Control.Monad.Writer.Class.MonadWriter w m, Control.Monad.State.Class.MonadState s m) => Control.Monad.RWS.Class.MonadRWS r w s (Control.Monad.Trans.Random.Lazy.RandT g m) instance (System.Random.Internal.RandomGen g, GHC.Base.Monad m) => Control.Monad.Random.Class.MonadRandom (Control.Monad.Trans.Random.Lazy.RandT g m) instance (System.Random.Internal.RandomGen g, GHC.Base.Monad m) => Control.Monad.Random.Class.MonadSplit g (Control.Monad.Trans.Random.Lazy.RandT g m) instance (GHC.Base.Monad m, System.Random.Internal.RandomGen g) => Control.Monad.Random.Class.MonadInterleave (Control.Monad.Trans.Random.Lazy.RandT g m) instance Control.Monad.State.Class.MonadState s m => Control.Monad.State.Class.MonadState s (Control.Monad.Trans.Random.Lazy.RandT g m) instance Control.Monad.Primitive.PrimMonad m => Control.Monad.Primitive.PrimMonad (Control.Monad.Trans.Random.Lazy.RandT s m) instance Control.Monad.Fail.MonadFail m => Control.Monad.Fail.MonadFail (Control.Monad.Trans.Random.Lazy.RandT g m) instance (GHC.Base.Monad m, System.Random.Internal.RandomGen g) => System.Random.Internal.StatefulGen (Control.Monad.Trans.Random.Strict.RandGen g) (Control.Monad.Trans.Random.Lazy.RandT g m) instance (GHC.Base.Monad m, System.Random.Internal.RandomGen g) => System.Random.Stateful.RandomGenM (Control.Monad.Trans.Random.Strict.RandGen g) g (Control.Monad.Trans.Random.Lazy.RandT g m) -- | Random monads, passing a random number generator through a -- computation. -- -- This version is lazy; for a strict version, see -- Control.Monad.Trans.Random.Strict, which has the same -- interface. module Control.Monad.Trans.Random -- | Random monads that are lazy in the generator state. For a strict -- version, see Control.Monad.Random.Strict, which has the same -- interface. module Control.Monad.Random.Lazy -- | A random monad parameterized by the type g of the generator -- to carry. -- -- The return function leaves the generator unchanged, while -- >>= uses the final generator of the first computation as -- the initial generator of the second. type Rand g = RandT g Identity -- | Construct a random monad computation from a function. (The inverse of -- runRand.) liftRand :: (g -> (a, g)) -> Rand g a -- | Unwrap a random monad computation as a function. (The inverse of -- liftRand.) runRand :: Rand g a -> g -> (a, g) -- | Evaluate a random computation with the given initial generator and -- return the final value, discarding the final generator. -- -- evalRand :: Rand g a -> g -> a -- | Evaluate a random computation with the given initial generator and -- return the final generator, discarding the final value. -- -- execRand :: Rand g a -> g -> g -- | Map both the return value and final generator of a computation using -- the given function. -- -- mapRand :: ((a, g) -> (b, g)) -> Rand g a -> Rand g b -- | withRand f m executes action m on a generator -- modified by applying f. -- -- withRand :: (g -> g) -> Rand g a -> Rand g a -- | Evaluate a random computation in the IO monad, splitting the -- global standard generator to get a new one for the computation. evalRandIO :: Rand StdGen a -> IO a -- | A random transformer monad parameterized by: -- -- -- -- The return function leaves the generator unchanged, while -- >>= uses the final generator of the first computation as -- the initial generator of the second. data RandT g m a -- | Construct a random monad computation from an impure function. (The -- inverse of runRandT.) liftRandT :: (g -> m (a, g)) -> RandT g m a -- | Unwrap a random monad computation as an impure function. (The inverse -- of liftRandT.) runRandT :: RandT g m a -> g -> m (a, g) -- | Evaluate a random computation with the given initial generator and -- return the final value, discarding the final generator. -- -- evalRandT :: Monad m => RandT g m a -> g -> m a -- | Evaluate a random computation with the given initial generator and -- return the final generator, discarding the final value. -- -- execRandT :: Monad m => RandT g m a -> g -> m g -- | Map both the return value and final generator of a computation using -- the given function. -- -- mapRandT :: (m (a, g) -> n (b, g)) -> RandT g m a -> RandT g n b -- | withRandT f m executes action m on a -- generator modified by applying f. -- -- withRandT :: (g -> g) -> RandT g m a -> RandT g m a -- | Evaluate a random computation that is embedded in the IO monad, -- splitting the global standard generator to get a new one for the -- computation. evalRandTIO :: MonadIO m => RandT StdGen m a -> m a -- | A variant of randomM that uses the global pseudo-random number -- generator globalStdGen. -- --
--   >>> import Data.Int
--   
--   >>> randomIO :: IO Int32
--   -1580093805
--   
-- -- This function is equivalent to getStdRandom -- random and is included in this interface for historical -- reasons and backwards compatibility. It is recommended to use -- uniformM instead, possibly with the globalStdGen if -- relying on the global state is acceptable. -- --
--   >>> import System.Random.Stateful
--   
--   >>> uniformM globalStdGen :: IO Int32
--   -1649127057
--   
randomIO :: (Random a, MonadIO m) => m a -- | A variant of randomRM that uses the global pseudo-random number -- generator globalStdGen -- --
--   >>> randomRIO (2020, 2100) :: IO Int
--   2040
--   
-- -- Similar to randomIO, this function is equivalent to -- getStdRandom randomR and is included in this -- interface for historical reasons and backwards compatibility. It is -- recommended to use uniformRM instead, possibly with the -- globalStdGen if relying on the global state is acceptable. -- --
--   >>> import System.Random.Stateful
--   
--   >>> uniformRM (2020, 2100) globalStdGen :: IO Int
--   2079
--   
randomRIO :: (Random a, MonadIO m) => (a, a) -> m a -- | Uses the supplied function to get a value from the current global -- random generator, and updates the global generator with the new -- generator returned by the function. For example, rollDice -- produces a pseudo-random integer between 1 and 6: -- --
--   >>> rollDice = getStdRandom (randomR (1, 6))
--   
--   >>> replicateM 10 (rollDice :: IO Int)
--   [5,6,6,1,1,6,4,2,4,1]
--   
-- -- This is an outdated function and it is recommended to switch to its -- equivalent applyAtomicGen instead, possibly with the -- globalStdGen if relying on the global state is acceptable. -- --
--   >>> import System.Random.Stateful
--   
--   >>> rollDice = applyAtomicGen (uniformR (1, 6)) globalStdGen
--   
--   >>> replicateM 10 (rollDice :: IO Int)
--   [4,6,1,1,4,4,3,2,1,2]
--   
getStdRandom :: MonadIO m => (StdGen -> (a, StdGen)) -> m a -- | Applies split to the current global pseudo-random generator -- globalStdGen, updates it with one of the results, and returns -- the other. newStdGen :: MonadIO m => m StdGen -- | Gets the global pseudo-random number generator. Extracts the contents -- of globalStdGen getStdGen :: MonadIO m => m StdGen -- | Sets the global pseudo-random number generator. Overwrites the -- contents of globalStdGen setStdGen :: MonadIO m => StdGen -> m () -- | Initialize StdGen using system entropy (i.e. -- /dev/urandom) when it is available, while falling back on -- using system time as the seed. initStdGen :: MonadIO m => m StdGen -- | Generates a ByteString of the specified size using a pure -- pseudo-random number generator. See uniformByteStringM for the -- monadic version. -- --

Examples

-- --
--   >>> import System.Random
--   
--   >>> import Data.ByteString
--   
--   >>> let pureGen = mkStdGen 137
--   
--   >>> unpack . fst . genByteString 10 $ pureGen
--   [51,123,251,37,49,167,90,109,1,4]
--   
genByteString :: RandomGen g => Int -> g -> (ByteString, g) -- | The class of types for which random values can be generated. Most -- instances of Random will produce values that are uniformly -- distributed on the full range, but for those types without a -- well-defined "full range" some sensible default subrange will be -- selected. -- -- Random exists primarily for backwards compatibility with -- version 1.1 of this library. In new code, use the better specified -- Uniform and UniformRange instead. class Random a -- | Takes a range (lo,hi) and a pseudo-random number generator -- g, and returns a pseudo-random value uniformly distributed over -- the closed interval [lo,hi], together with a new generator. It -- is unspecified what happens if lo>hi, but usually the values -- will simply get swapped. -- --
--   >>> let gen = mkStdGen 2021
--   
--   >>> fst $ randomR ('a', 'z') gen
--   't'
--   
--   >>> fst $ randomR ('z', 'a') gen
--   't'
--   
-- -- For continuous types there is no requirement that the values lo -- and hi are ever produced, but they may be, depending on the -- implementation and the interval. -- -- There is no requirement to follow the Ord instance and the -- concept of range can be defined on per type basis. For example product -- types will treat their values independently: -- --
--   >>> fst $ randomR (('a', 5.0), ('z', 10.0)) $ mkStdGen 2021
--   ('t',6.240232662366563)
--   
-- -- In case when a lawful range is desired uniformR should be used -- instead. randomR :: (Random a, RandomGen g) => (a, a) -> g -> (a, g) -- | The same as randomR, but using a default range determined by -- the type: -- -- random :: (Random a, RandomGen g) => g -> (a, g) -- | Plural variant of randomR, producing an infinite list of -- pseudo-random values instead of returning a new generator. randomRs :: (Random a, RandomGen g) => (a, a) -> g -> [a] -- | Plural variant of random, producing an infinite list of -- pseudo-random values instead of returning a new generator. randoms :: (Random a, RandomGen g) => g -> [a] -- | Constructs a StdGen deterministically. mkStdGen :: Int -> StdGen -- | RandomGen is an interface to pure pseudo-random number -- generators. -- -- StdGen is the standard RandomGen instance provided by -- this library. class RandomGen g -- | Returns an Int that is uniformly distributed over the range -- returned by genRange (including both end points), and a new -- generator. Using next is inefficient as all operations go via -- Integer. See here for more details. It is thus -- deprecated. next :: RandomGen g => g -> (Int, g) -- | Returns a Word8 that is uniformly distributed over the entire -- Word8 range. genWord8 :: RandomGen g => g -> (Word8, g) -- | Returns a Word16 that is uniformly distributed over the entire -- Word16 range. genWord16 :: RandomGen g => g -> (Word16, g) -- | Returns a Word32 that is uniformly distributed over the entire -- Word32 range. genWord32 :: RandomGen g => g -> (Word32, g) -- | Returns a Word64 that is uniformly distributed over the entire -- Word64 range. genWord64 :: RandomGen g => g -> (Word64, g) -- | genWord32R upperBound g returns a Word32 that is -- uniformly distributed over the range [0, upperBound]. genWord32R :: RandomGen g => Word32 -> g -> (Word32, g) -- | genWord64R upperBound g returns a Word64 that is -- uniformly distributed over the range [0, upperBound]. genWord64R :: RandomGen g => Word64 -> g -> (Word64, g) -- | genShortByteString n g returns a ShortByteString of -- length n filled with pseudo-random bytes. genShortByteString :: RandomGen g => Int -> g -> (ShortByteString, g) -- | Yields the range of values returned by next. -- -- It is required that: -- -- -- -- The default definition spans the full range of Int. genRange :: RandomGen g => g -> (Int, Int) -- | Returns two distinct pseudo-random number generators. -- -- Implementations should take care to ensure that the resulting -- generators are not correlated. Some pseudo-random number generators -- are not splittable. In that case, the split implementation -- should fail with a descriptive error message. split :: RandomGen g => g -> (g, g) -- | The standard pseudo-random number generator. data StdGen -- | The class of types for which a uniformly distributed value can be -- drawn from all possible values of the type. class Uniform a -- | The class of types for which a uniformly distributed value can be -- drawn from a range. class UniformRange a -- | A type class for data with a finite number of inhabitants. This type -- class is used in default implementations of Uniform. -- -- Users are not supposed to write instances of Finite manually. -- There is a default implementation in terms of Generic instead. -- --
--   >>> :set -XDeriveGeneric -XDeriveAnyClass
--   
--   >>> import GHC.Generics (Generic)
--   
--   >>> data MyBool = MyTrue | MyFalse deriving (Generic, Finite)
--   
--   >>> data Action = Code MyBool | Eat (Maybe Bool) | Sleep deriving (Generic, Finite)
--   
class Finite a -- | This module is provided for backwards compatibility, and simply -- re-exports Control.Monad.Random.Lazy. module Control.Monad.Random -- | Random monads that are strict in the generator state. For a lazy -- version, see Control.Monad.Random.Lazy, which has the same -- interface. module Control.Monad.Random.Strict -- | A random monad parameterized by the type g of the generator -- to carry. -- -- The return function leaves the generator unchanged, while -- >>= uses the final generator of the first computation as -- the initial generator of the second. type Rand g = RandT g Identity -- | Construct a random monad computation from a function. (The inverse of -- runRand.) liftRand :: (g -> (a, g)) -> Rand g a -- | Unwrap a random monad computation as a function. (The inverse of -- liftRand.) runRand :: Rand g a -> g -> (a, g) -- | Evaluate a random computation with the given initial generator and -- return the final value, discarding the final generator. -- -- evalRand :: Rand g a -> g -> a -- | Evaluate a random computation with the given initial generator and -- return the final generator, discarding the final value. -- -- execRand :: Rand g a -> g -> g -- | Map both the return value and final generator of a computation using -- the given function. -- -- mapRand :: ((a, g) -> (b, g)) -> Rand g a -> Rand g b -- | withRand f m executes action m on a generator -- modified by applying f. -- -- withRand :: (g -> g) -> Rand g a -> Rand g a -- | Evaluate a random computation in the IO monad, splitting the -- global standard generator to get a new one for the computation. evalRandIO :: Rand StdGen a -> IO a -- | A random transformer monad parameterized by: -- -- -- -- The return function leaves the generator unchanged, while -- >>= uses the final generator of the first computation as -- the initial generator of the second. data RandT g m a -- | Construct a random monad computation from an impure function. (The -- inverse of runRandT.) liftRandT :: (g -> m (a, g)) -> RandT g m a -- | Unwrap a random monad computation as an impure function. (The inverse -- of liftRandT.) runRandT :: RandT g m a -> g -> m (a, g) -- | Evaluate a random computation with the given initial generator and -- return the final value, discarding the final generator. -- -- evalRandT :: Monad m => RandT g m a -> g -> m a -- | Evaluate a random computation with the given initial generator and -- return the final generator, discarding the final value. -- -- execRandT :: Monad m => RandT g m a -> g -> m g -- | Map both the return value and final generator of a computation using -- the given function. -- -- mapRandT :: (m (a, g) -> n (b, g)) -> RandT g m a -> RandT g n b -- | withRandT f m executes action m on a -- generator modified by applying f. -- -- withRandT :: (g -> g) -> RandT g m a -> RandT g m a -- | Evaluate a random computation that is embedded in the IO monad, -- splitting the global standard generator to get a new one for the -- computation. evalRandTIO :: MonadIO m => RandT StdGen m a -> m a -- | A variant of randomM that uses the global pseudo-random number -- generator globalStdGen. -- --
--   >>> import Data.Int
--   
--   >>> randomIO :: IO Int32
--   -1580093805
--   
-- -- This function is equivalent to getStdRandom -- random and is included in this interface for historical -- reasons and backwards compatibility. It is recommended to use -- uniformM instead, possibly with the globalStdGen if -- relying on the global state is acceptable. -- --
--   >>> import System.Random.Stateful
--   
--   >>> uniformM globalStdGen :: IO Int32
--   -1649127057
--   
randomIO :: (Random a, MonadIO m) => m a -- | A variant of randomRM that uses the global pseudo-random number -- generator globalStdGen -- --
--   >>> randomRIO (2020, 2100) :: IO Int
--   2040
--   
-- -- Similar to randomIO, this function is equivalent to -- getStdRandom randomR and is included in this -- interface for historical reasons and backwards compatibility. It is -- recommended to use uniformRM instead, possibly with the -- globalStdGen if relying on the global state is acceptable. -- --
--   >>> import System.Random.Stateful
--   
--   >>> uniformRM (2020, 2100) globalStdGen :: IO Int
--   2079
--   
randomRIO :: (Random a, MonadIO m) => (a, a) -> m a -- | Uses the supplied function to get a value from the current global -- random generator, and updates the global generator with the new -- generator returned by the function. For example, rollDice -- produces a pseudo-random integer between 1 and 6: -- --
--   >>> rollDice = getStdRandom (randomR (1, 6))
--   
--   >>> replicateM 10 (rollDice :: IO Int)
--   [5,6,6,1,1,6,4,2,4,1]
--   
-- -- This is an outdated function and it is recommended to switch to its -- equivalent applyAtomicGen instead, possibly with the -- globalStdGen if relying on the global state is acceptable. -- --
--   >>> import System.Random.Stateful
--   
--   >>> rollDice = applyAtomicGen (uniformR (1, 6)) globalStdGen
--   
--   >>> replicateM 10 (rollDice :: IO Int)
--   [4,6,1,1,4,4,3,2,1,2]
--   
getStdRandom :: MonadIO m => (StdGen -> (a, StdGen)) -> m a -- | Applies split to the current global pseudo-random generator -- globalStdGen, updates it with one of the results, and returns -- the other. newStdGen :: MonadIO m => m StdGen -- | Gets the global pseudo-random number generator. Extracts the contents -- of globalStdGen getStdGen :: MonadIO m => m StdGen -- | Sets the global pseudo-random number generator. Overwrites the -- contents of globalStdGen setStdGen :: MonadIO m => StdGen -> m () -- | Initialize StdGen using system entropy (i.e. -- /dev/urandom) when it is available, while falling back on -- using system time as the seed. initStdGen :: MonadIO m => m StdGen -- | Generates a ByteString of the specified size using a pure -- pseudo-random number generator. See uniformByteStringM for the -- monadic version. -- --

Examples

-- --
--   >>> import System.Random
--   
--   >>> import Data.ByteString
--   
--   >>> let pureGen = mkStdGen 137
--   
--   >>> unpack . fst . genByteString 10 $ pureGen
--   [51,123,251,37,49,167,90,109,1,4]
--   
genByteString :: RandomGen g => Int -> g -> (ByteString, g) -- | The class of types for which random values can be generated. Most -- instances of Random will produce values that are uniformly -- distributed on the full range, but for those types without a -- well-defined "full range" some sensible default subrange will be -- selected. -- -- Random exists primarily for backwards compatibility with -- version 1.1 of this library. In new code, use the better specified -- Uniform and UniformRange instead. class Random a -- | Takes a range (lo,hi) and a pseudo-random number generator -- g, and returns a pseudo-random value uniformly distributed over -- the closed interval [lo,hi], together with a new generator. It -- is unspecified what happens if lo>hi, but usually the values -- will simply get swapped. -- --
--   >>> let gen = mkStdGen 2021
--   
--   >>> fst $ randomR ('a', 'z') gen
--   't'
--   
--   >>> fst $ randomR ('z', 'a') gen
--   't'
--   
-- -- For continuous types there is no requirement that the values lo -- and hi are ever produced, but they may be, depending on the -- implementation and the interval. -- -- There is no requirement to follow the Ord instance and the -- concept of range can be defined on per type basis. For example product -- types will treat their values independently: -- --
--   >>> fst $ randomR (('a', 5.0), ('z', 10.0)) $ mkStdGen 2021
--   ('t',6.240232662366563)
--   
-- -- In case when a lawful range is desired uniformR should be used -- instead. randomR :: (Random a, RandomGen g) => (a, a) -> g -> (a, g) -- | The same as randomR, but using a default range determined by -- the type: -- -- random :: (Random a, RandomGen g) => g -> (a, g) -- | Plural variant of randomR, producing an infinite list of -- pseudo-random values instead of returning a new generator. randomRs :: (Random a, RandomGen g) => (a, a) -> g -> [a] -- | Plural variant of random, producing an infinite list of -- pseudo-random values instead of returning a new generator. randoms :: (Random a, RandomGen g) => g -> [a] -- | Constructs a StdGen deterministically. mkStdGen :: Int -> StdGen -- | RandomGen is an interface to pure pseudo-random number -- generators. -- -- StdGen is the standard RandomGen instance provided by -- this library. class RandomGen g -- | Returns an Int that is uniformly distributed over the range -- returned by genRange (including both end points), and a new -- generator. Using next is inefficient as all operations go via -- Integer. See here for more details. It is thus -- deprecated. next :: RandomGen g => g -> (Int, g) -- | Returns a Word8 that is uniformly distributed over the entire -- Word8 range. genWord8 :: RandomGen g => g -> (Word8, g) -- | Returns a Word16 that is uniformly distributed over the entire -- Word16 range. genWord16 :: RandomGen g => g -> (Word16, g) -- | Returns a Word32 that is uniformly distributed over the entire -- Word32 range. genWord32 :: RandomGen g => g -> (Word32, g) -- | Returns a Word64 that is uniformly distributed over the entire -- Word64 range. genWord64 :: RandomGen g => g -> (Word64, g) -- | genWord32R upperBound g returns a Word32 that is -- uniformly distributed over the range [0, upperBound]. genWord32R :: RandomGen g => Word32 -> g -> (Word32, g) -- | genWord64R upperBound g returns a Word64 that is -- uniformly distributed over the range [0, upperBound]. genWord64R :: RandomGen g => Word64 -> g -> (Word64, g) -- | genShortByteString n g returns a ShortByteString of -- length n filled with pseudo-random bytes. genShortByteString :: RandomGen g => Int -> g -> (ShortByteString, g) -- | Yields the range of values returned by next. -- -- It is required that: -- -- -- -- The default definition spans the full range of Int. genRange :: RandomGen g => g -> (Int, Int) -- | Returns two distinct pseudo-random number generators. -- -- Implementations should take care to ensure that the resulting -- generators are not correlated. Some pseudo-random number generators -- are not splittable. In that case, the split implementation -- should fail with a descriptive error message. split :: RandomGen g => g -> (g, g) -- | The standard pseudo-random number generator. data StdGen -- | The class of types for which a uniformly distributed value can be -- drawn from all possible values of the type. class Uniform a -- | The class of types for which a uniformly distributed value can be -- drawn from a range. class UniformRange a -- | A type class for data with a finite number of inhabitants. This type -- class is used in default implementations of Uniform. -- -- Users are not supposed to write instances of Finite manually. -- There is a default implementation in terms of Generic instead. -- --
--   >>> :set -XDeriveGeneric -XDeriveAnyClass
--   
--   >>> import GHC.Generics (Generic)
--   
--   >>> data MyBool = MyTrue | MyFalse deriving (Generic, Finite)
--   
--   >>> data Action = Code MyBool | Eat (Maybe Bool) | Sleep deriving (Generic, Finite)
--   
class Finite a