Control.Monad.Random

MonadRandom-0.4.2.3: Random-number generation monad.

Copyright | 2006-2007 Cale Gibbard, Russell O'Connor, Dan Doel, Remi Turk, Eric Kidd.
License | OtherLicense
Stability | experimental
Portability | non-portable (multi-parameter type classes, undecidable instances)
Safe Haskell | Trustworthy
Language | Haskell2010

Control.Monad.Random

Contents

- Special lift functions
- Example
- Orphan instances

Description

A random number generation monad. See http://www.haskell.org/haskellwiki/NewMonads/MonadRandom for the original version of this code.

The actual interface is defined by `MonadRandom`.

Computation type:
  Computations which consume random values.
Binding strategy:
  The computation proceeds in the same fashion as the identity monad, but it carries a random number generator that may be queried to generate random values.
Useful for:
  Monte Carlo algorithms and simulating random processes.

Synopsis

- module System.Random
- module Control.Monad.Random.Class
- evalRandT :: Monad m => RandT g m a -> g -> m a
- runRandT :: RandT g m a -> g -> m (a, g)
- evalRand :: Rand g a -> g -> a
- runRand :: Rand g a -> g -> (a, g)
- evalRandIO :: Rand StdGen a -> IO a
- fromList :: MonadRandom m => [(a, Rational)] -> m a
- uniform :: MonadRandom m => [a] -> m a
- type Rand g = RandT g Identity
- data RandT g m a
- liftRand :: (g -> (a, g)) -> Rand g a
- liftRandT :: (g -> m (a, g)) -> RandT g m a

# Documentation

module System.Random

module Control.Monad.Random.Class

evalRandT :: Monad m => RandT g m a -> g -> m a #

Evaluate a RandT computation using the generator `g`. Note that the generator `g` is not returned, so there's no way to recover the updated version of `g`.

runRandT :: RandT g m a -> g -> m (a, g) #

Run a RandT computation using the generator `g`, returning the result and the updated generator.

evalRand :: Rand g a -> g -> a #

Evaluate a random computation using the generator `g`. Note that the generator `g` is not returned, so there's no way to recover the updated version of `g`.

runRand :: Rand g a -> g -> (a, g) #

Run a random computation using the generator `g`, returning the result and the updated generator.

evalRandIO :: Rand StdGen a -> IO a #

Evaluate a random computation in the IO monad, splitting the global standard generator to get a new one for the computation.

fromList :: MonadRandom m => [(a, Rational)] -> m a #

Sample a random value from a weighted list. The total weight of all elements must not be 0.

uniform :: MonadRandom m => [a] -> m a #

Sample a value from a uniform distribution of a list of elements.

type Rand g = RandT g Identity #

A basic random monad.

data RandT g m a #

A monad transformer which adds a random number generator to an existing monad.

Instances

MonadReader r m => MonadReader r (RandT g m) # |
Methods ask :: RandT g m r # local :: (r -> r) -> RandT g m a -> RandT g m a # reader :: (r -> a) -> RandT g m a #
MonadState s m => MonadState s (RandT g m) # |
Methods get :: RandT g m s # put :: s -> RandT g m () # state :: (s -> (a, s)) -> RandT g m a #
MonadWriter w m => MonadWriter w (RandT g m) # |
Methods writer :: (a, w) -> RandT g m a # tell :: w -> RandT g m () # listen :: RandT g m a -> RandT g m (a, w) # pass :: RandT g m (a, w -> w) -> RandT g m a #
(Monad m, RandomGen g) => MonadSplit g (RandT g m) # |
Methods getSplit :: RandT g m g #
MonadTrans (RandT g) # |
Methods lift :: Monad m => m a -> RandT g m a #
Monad m => Monad (RandT g m) # |
Methods (>>=) :: RandT g m a -> (a -> RandT g m b) -> RandT g m b # (>>) :: RandT g m a -> RandT g m b -> RandT g m b # return :: a -> RandT g m a # fail :: String -> RandT g m a #
Functor m => Functor (RandT g m) # |
Methods fmap :: (a -> b) -> RandT g m a -> RandT g m b # (<$) :: a -> RandT g m b -> RandT g m a #
MonadFix m => MonadFix (RandT g m) # |
Methods mfix :: (a -> RandT g m a) -> RandT g m a #
(Functor m, Monad m) => Applicative (RandT g m) # |
Methods pure :: a -> RandT g m a # (<*>) :: RandT g m (a -> b) -> RandT g m a -> RandT g m b # (*>) :: RandT g m a -> RandT g m b -> RandT g m b # (<*) :: RandT g m a -> RandT g m b -> RandT g m a #
MonadIO m => MonadIO (RandT g m) # |
Methods liftIO :: IO a -> RandT g m a #
(Functor m, MonadPlus m) => Alternative (RandT g m) # |
Methods empty :: RandT g m a # (<|>) :: RandT g m a -> RandT g m a -> RandT g m a # some :: RandT g m a -> RandT g m [a] # many :: RandT g m a -> RandT g m [a] #
MonadPlus m => MonadPlus (RandT g m) # |
Methods mzero :: RandT g m a # mplus :: RandT g m a -> RandT g m a -> RandT g m a #
(Monad m, RandomGen g) => MonadRandom (RandT g m) # |
Methods getRandom :: Random a => RandT g m a # getRandoms :: Random a => RandT g m [a] # getRandomR :: Random a => (a, a) -> RandT g m a # getRandomRs :: Random a => (a, a) -> RandT g m [a] #

# Special lift functions

liftRand #

Arguments

:: (g -> (a, g)) | action returning value and new generator state
-> Rand g a |

Lift arbitrary action to Rand

liftRandT #

Arguments

:: (g -> m (a, g)) | action returning value and new generator state
-> RandT g m a |

Lift arbitrary action to RandT

# Example

The `die` function simulates the roll of a die, picking a number between 1 and 6, inclusive, and returning it in the `Rand` monad. Notice that this code will work with any source of random numbers `g`.

    die :: (RandomGen g) => Rand g Int
    die = getRandomR (1,6)

The `dice` function uses `replicate` and `sequence` to simulate the roll of `n` dice.

    dice :: (RandomGen g) => Int -> Rand g [Int]
    dice n = sequence (replicate n die)

To extract a value from the `Rand` monad, we can can use `evalRandIO`.

    main = do
      values <- evalRandIO (dice 2)
      putStrLn (show values)

# Orphan instances

MonadRandom IO # |
Methods getRandom :: Random a => IO a # getRandoms :: Random a => IO [a] # getRandomR :: Random a => (a, a) -> IO a # getRandomRs :: Random a => (a, a) -> IO [a] #
MonadSplit StdGen IO # |
Methods getSplit :: IO StdGen #
MonadSplit g m => MonadSplit g (MaybeT m) # |
Methods getSplit :: MaybeT m g #
(Error e, MonadSplit g m) => MonadSplit g (ErrorT e m) # |
Methods getSplit :: ErrorT e m g #
MonadSplit g m => MonadSplit g (ExceptT e m) # |
Methods getSplit :: ExceptT e m g #
(MonadSplit g m, Monoid w) => MonadSplit g (WriterT w m) # |
Methods getSplit :: WriterT w m g #
(MonadSplit g m, Monoid w) => MonadSplit g (WriterT w m) # |
Methods getSplit :: WriterT w m g #
MonadSplit g m => MonadSplit g (StateT s m) # |
Methods getSplit :: StateT s m g #
MonadSplit g m => MonadSplit g (StateT s m) # |
Methods getSplit :: StateT s m g #
MonadSplit g m => MonadSplit g (IdentityT * m) # |
Methods getSplit :: IdentityT * m g #
MonadSplit g m => MonadSplit g (ContT * r m) # |
Methods getSplit :: ContT * r m g #
MonadSplit g m => MonadSplit g (ReaderT * r m) # |
Methods getSplit :: ReaderT * r m g #
(MonadSplit g m, Monoid w) => MonadSplit g (RWST r w s m) # |
Methods getSplit :: RWST r w s m g #
(MonadSplit g m, Monoid w) => MonadSplit g (RWST r w s m) # |
Methods getSplit :: RWST r w s m g #
MonadRandom m => MonadRandom (MaybeT m) # |
Methods getRandom :: Random a => MaybeT m a # getRandoms :: Random a => MaybeT m [a] # getRandomR :: Random a => (a, a) -> MaybeT m a # getRandomRs :: Random a => (a, a) -> MaybeT m [a] #
MonadRandom m => MonadRandom (ExceptT e m) # |
Methods getRandom :: Random a => ExceptT e m a # getRandoms :: Random a => ExceptT e m [a] # getRandomR :: Random a => (a, a) -> ExceptT e m a # getRandomRs :: Random a => (a, a) -> ExceptT e m [a] #
(Error e, MonadRandom m) => MonadRandom (ErrorT e m) # |
Methods getRandom :: Random a => ErrorT e m a # getRandoms :: Random a => ErrorT e m [a] # getRandomR :: Random a => (a, a) -> ErrorT e m a # getRandomRs :: Random a => (a, a) -> ErrorT e m [a] #
MonadRandom m => MonadRandom (StateT s m) # |
Methods getRandom :: Random a => StateT s m a # getRandoms :: Random a => StateT s m [a] # getRandomR :: Random a => (a, a) -> StateT s m a # getRandomRs :: Random a => (a, a) -> StateT s m [a] #
MonadRandom m => MonadRandom (StateT s m) # |
Methods getRandom :: Random a => StateT s m a # getRandoms :: Random a => StateT s m [a] # getRandomR :: Random a => (a, a) -> StateT s m a # getRandomRs :: Random a => (a, a) -> StateT s m [a] #
(MonadRandom m, Monoid w) => MonadRandom (WriterT w m) # |
Methods getRandom :: Random a => WriterT w m a # getRandoms :: Random a => WriterT w m [a] # getRandomR :: Random a => (a, a) -> WriterT w m a # getRandomRs :: Random a => (a, a) -> WriterT w m [a] #
(MonadRandom m, Monoid w) => MonadRandom (WriterT w m) # |
Methods getRandom :: Random a => WriterT w m a # getRandoms :: Random a => WriterT w m [a] # getRandomR :: Random a => (a, a) -> WriterT w m a # getRandomRs :: Random a => (a, a) -> WriterT w m [a] #
MonadRandom m => MonadRandom (IdentityT * m) # |
Methods getRandom :: Random a => IdentityT * m a # getRandoms :: Random a => IdentityT * m [a] # getRandomR :: Random a => (a, a) -> IdentityT * m a # getRandomRs :: Random a => (a, a) -> IdentityT * m [a] #
MonadRandom m => MonadRandom (ContT * r m) # |
Methods getRandom :: Random a => ContT * r m a # getRandoms :: Random a => ContT * r m [a] # getRandomR :: Random a => (a, a) -> ContT * r m a # getRandomRs :: Random a => (a, a) -> ContT * r m [a] #
MonadRandom m => MonadRandom (ReaderT * r m) # |
Methods getRandom :: Random a => ReaderT * r m a # getRandoms :: Random a => ReaderT * r m [a] # getRandomR :: Random a => (a, a) -> ReaderT * r m a # getRandomRs :: Random a => (a, a) -> ReaderT * r m [a] #
(MonadRandom m, Monoid w) => MonadRandom (RWST r w s m) # |
Methods getRandom :: Random a => RWST r w s m a # getRandoms :: Random a => RWST r w s m [a] # getRandomR :: Random a => (a, a) -> RWST r w s m a # getRandomRs :: Random a => (a, a) -> RWST r w s m [a] #
(MonadRandom m, Monoid w) => MonadRandom (RWST r w s m) # |
Methods getRandom :: Random a => RWST r w s m a # getRandoms :: Random a => RWST r w s m [a] # getRandomR :: Random a => (a, a) -> RWST r w s m a # getRandomRs :: Random a => (a, a) -> RWST r w s m [a] #