| Stability | experimental |
|---|---|
| Maintainer | georg@schaathun.net |
| Safe Haskell | None |
AI.Random
Description
Artifical intelligence depends on random numbers. Other libraries provide pseudo-random numbers, but we require a couple of definitions to make them easier to use in the rest of the library.
We recommend to use the tf-random package, which provides the only
known implementation of a splittable PRNG with satisfactory distribution.
The key definitions TFGen and newTFGen are reexported from this
package.
- class RandomGen g where
- newTFGen :: IO TFGen
- data TFGen
- class Monad m => RandMonad m where
- getRandom :: Random a => (a, a) -> m a
- class (RandMonad m, NFRandomGen g) => SplittableRandMonad g m where
- getGenerator :: m g
- class (NFData g, RandomGen g) => NFRandomGen g
- withProbability :: RandMonad m => Double -> (a -> m a) -> a -> m a
- selectDistribution :: RandMonad m => [(a, Double)] -> m a
Reexported definitions
class RandomGen g where
Derive a new generator instance from the global RNG using split.
This is the default way of obtaining a new RNG instance.
Initial generator is seeded using mkSeedUnix on UNIX,
and mkSeedTime otherwise. This should be eventually
replaced with proper seeding.
data TFGen
The generator type
New definitions
class Monad m => RandMonad m where
RandMonad is a very generic monad to support pseudo-random
processes. It provides only one function, getRandom to return a
random number from a range.
class (RandMonad m, NFRandomGen g) => SplittableRandMonad g m where
A SplittableRandMonad provides a monad supporting pseudo-random
numbers. In addition to the methods of RandMonad, it includes
a function to get a new generator directly from the monad.
To provide this feature the underlying PRNG must be splittable.
Methods
getGenerator :: m g
Get a new pseudo-random generator from the monad. This generator is obtained by splitting the existing generator state, and should be independent of any previous or subsequent generators.
Instances
| SplittableRandMonad StdGen IO | |
| (Monoid w, SplittableRandMonad g m) => SplittableRandMonad g (WriterT w m) | |
| SplittableRandMonad g m => SplittableRandMonad g (ReaderT r m) | |
| (NFRandomGen g, Monad m) => SplittableRandMonad g (StateT g m) |
class (NFData g, RandomGen g) => NFRandomGen g
To allow parallel processing with the Repa library, we need to
be able to force generators into head normal form. This is done
by making the generator an instance of the NFData class.
For convenience the NFRandomGen class is introduced for generators
with this property.
Instances
| NFRandomGen TFGen | |
| NFRandomGen StdGen |
withProbability :: RandMonad m => Double -> (a -> m a) -> a -> m a
The withProbability applies a given
function with a given probability.
selectDistribution :: RandMonad m => [(a, Double)] -> m a
The selectDistribution function returns a random
element from a discrete probability distribution given as a list of
element/probality pairs.