distribution-1.1.1.0: Finite discrete probability distributions.

Safe HaskellSafe
LanguageHaskell98

Data.Distribution.Sample

Contents

Description

This modules provides ways to randomly and efficiently sample values from distributions.

Walker's alias method is used internally, so that values can be sampled in constant time.

Synopsis

Generator

data Generator a Source #

Generator of random values of type a.

Can be created in linear time from distributions and sampled in constant time.

Instances

Functor Generator Source # 

Methods

fmap :: (a -> b) -> Generator a -> Generator b #

(<$) :: a -> Generator b -> Generator a #

Building

fromDistribution :: Distribution a -> Generator a Source #

Creates a generator from the given non-empty distribution.

Runs in O(n) time where n is the size of the distribution.

safeFromDistribution :: Distribution a -> Maybe (Generator a) Source #

Safe version of fromDistribution. Returns Nothing when the given distribution is empty.

Sampling

sample :: RandomGen g => Generator a -> g -> (a, g) Source #

Picks a random value from the generator.

Runs in constant O(1) time.

getSample :: MonadRandom m => Generator a -> m a Source #

Picks a random value from the generator.

Runs in constant O(1) time.