This module provides functions useful for implementing new MonadRandom
and RandomSource
instances for state-abstractions containing PureMT
values (the pure pseudorandom generator provided by the
mersenne-random-pure64 package), as well as instances for some common
cases.
- getRandomWordFromMTRef :: ModifyRef sr m PureMT => sr -> m Word64
- getRandomByteFromMTRef :: (Monad m, ModifyRef sr m PureMT) => sr -> m Word8
- getRandomDoubleFromMTRef :: (Monad m, ModifyRef sr m PureMT) => sr -> m Double
- getRandomWordFromMTState :: MonadState PureMT m => m Word64
- getRandomByteFromMTState :: MonadState PureMT m => m Word8
- getRandomDoubleFromMTState :: MonadState PureMT m => m Double
Documentation
getRandomWordFromMTRef :: ModifyRef sr m PureMT => sr -> m Word64Source
Given a mutable reference to a PureMT
generator, we can make a
RandomSource
usable in any monad in which the reference can be modified.
For example, if x :: TVar PureMT
, getRandomWordFromMTRef x
can be
used as a RandomSource
in IO
, STM
, or any monad which is an instance
of MonadIO
. These functions can also be used to implement additional
RandomSource
instances for mutable references to PureMT
states.
getRandomWordFromMTState :: MonadState PureMT m => m Word64Source
Similarly, getRandomWordFromMTState x
can be used in any "state"
monad in the mtl sense whose state is a PureMT
generator.
Additionally, the standard mtl state monads have MonadRandom
instances
which do precisely that, allowing an easy conversion of RVar
s and
other Distribution
instances to "pure" random variables (e.g., by
runState . sample :: Distribution d t => d t -> PureMT -> (t, PureMT)
.
PureMT
in the type there can be replaced by StdGen
or anything else
satisfying MonadRandom (State s) => s
).
getRandomByteFromMTState :: MonadState PureMT m => m Word8Source