-- 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. -- --
-- >>> 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 f m = modify f >> m
withRandT f m = modify f >> -- m
-- >>> 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 f m = modify f >> m
withRandT f m = modify f >> -- m
-- >>> 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 f m = modify f >> m
withRandT f m = modify f >> -- m
-- >>> 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. -- --
-- >>> 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:
--
-- -- >>> :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 f m = modify f >> m
withRandT f m = modify f >> -- m
-- >>> 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. -- --
-- >>> 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:
--
-- -- >>> :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