random-source-0.3.0.10: Generic basis for random number generators

Safe HaskellNone
LanguageHaskell2010

Data.Random.Source.MWC

Contents

Description

This module defines the following instances:

instance RandomSource (ST s) (Gen s)
instance RandomSource IO (Gen RealWorld)
Synopsis

Documentation

data Gen s #

State of the pseudo-random number generator. It uses mutable state so same generator shouldn't be used from the different threads simultaneously.

Instances
RandomSource IO (Gen RealWorld) Source # 
Instance details

Defined in Data.Random.Source.MWC

RandomSource (ST s) (Gen s) Source # 
Instance details

Defined in Data.Random.Source.MWC

(PrimMonad m, s ~ PrimState m) => MonadRandom (ReaderT (Gen s) m) Source # 
Instance details

Defined in Data.Random.Source.MWC

data RealWorld :: Type #

RealWorld is deeply magical. It is primitive, but it is not unlifted (hence ptrArg). We never manipulate values of type RealWorld; it's only used in the type system, to parameterise State#.

create :: PrimMonad m => m (Gen (PrimState m)) #

Create a generator for variates using a fixed seed.

initialize :: (PrimMonad m, Vector v Word32) => v Word32 -> m (Gen (PrimState m)) #

Create a generator for variates using the given seed, of which up to 256 elements will be used. For arrays of less than 256 elements, part of the default seed will be used to finish initializing the generator's state.

Examples:

initialize (singleton 42)
initialize (fromList [4, 8, 15, 16, 23, 42])

If a seed contains fewer than 256 elements, it is first used verbatim, then its elements are xored against elements of the default seed until 256 elements are reached.

If a seed contains exactly 258 elements, then the last two elements are used to set the generator's initial state. This allows for complete generator reproducibility, so that e.g. gen' == gen in the following example:

gen' <- initialize . fromSeed =<< save

In the MWC algorithm, the carry value must be strictly smaller than the multiplicator (see https://en.wikipedia.org/wiki/Multiply-with-carry). Hence, if a seed contains exactly 258 elements, the carry value, which is the last of the 258 values, is moduloed by the multiplicator.

Note that if the first carry value is strictly smaller than the multiplicator, all subsequent carry values are also strictly smaller than the multiplicator (a proof of this is in the comments of the code of uniformWord32), hence when restoring a saved state, we have the guarantee that moduloing the saved carry won't modify its value.

save :: PrimMonad m => Gen (PrimState m) -> m Seed #

Save the state of a Gen, for later use by restore.

restore :: PrimMonad m => Seed -> m (Gen (PrimState m)) #

Create a new Gen that mirrors the state of a saved Seed.

Orphan instances