Copyright | (c) 2020 Naoyuki MORITA |
---|---|
License | BSD3 |
Maintainer | naoyuki.morita@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Pseudo-random number generation with MRG32k3a [1] (monadic interface).
The generator type Gen
is an instance of StatefulGen
type class, so
it can be used through StatefulGen
intreface functions such like,
> gen <-initialize
12345 > replicateM 10 (uniformM
gen) :: IO [Word32] [3320887301,884645991,913733706,3752754456,1435703647,511136538,160968264,1722383799,2355335811,690051551] > replicateM 10 (uniformM
gen) :: IO [Word32] [2969339749,1695849133,1356079654,2439569495,4195450174,664833117,1860624843,3302720735,962971476,547363651]
Notice: MRG32k3a is originally designed to generate random numbers following U(0,1). It DOES NOT generate exactly 32-bit information at a time.
If you need U(0,1) random numbers, use uniform01M
that generates a
random value efficiently by original MRG32k3a algorithm.
Gen: Pseudo-Random Number Generators
State of the pseudo-random number generator. It uses mutable state so same generator shouldn't be used from the different threads simultaneously.
Instances
(s ~ PrimState m, PrimMonad m) => StatefulGen (Gen s) m Source # | |
Defined in System.Random.MRG32k3a.Stateful uniformWord32R :: Word32 -> Gen s -> m Word32 # uniformWord64R :: Word64 -> Gen s -> m Word64 # uniformWord8 :: Gen s -> m Word8 # uniformWord16 :: Gen s -> m Word16 # uniformWord32 :: Gen s -> m Word32 # uniformWord64 :: Gen s -> m Word64 # uniformShortByteString :: Int -> Gen s -> m ShortByteString # |
initialize :: PrimMonad m => Word32 -> m (Gen (PrimState m)) Source #
Create a generator using given seed.
Type helpers
Unitility functions
uniform01M :: PrimMonad m => Gen (PrimState m) -> m Double Source #
Get a random value following U(0,1).
Seed: state management
You can get the current PRNG state by freezeGen
as an immutable data
that has type Seed
. You may save the state into persistent store and
restore the state by thawGen
later.
> gen <-initialize
12345 > replicateM 10 (uniform01M
gen) [0.12701112204657714,0.3185275653967945,0.3091860155832701,0.8258468629271136,0.2216299157820229,0.5333953879182788,0.4807742033156181,0.3555598794381262,0.13598841039594017,0.7558522371615436] > seed <-freezeGen
gen > show $fromSeed
seed "(2989318136,3378525425,1773647758,1462200156,2794459678,2822254363)" > replicateM 10 (uniform01M
gen) [0.5755553189002691,0.4100640936040626,0.3263296794324586,0.24037805455705044,0.6100629823964789,0.9041809183707534,0.2989749433907653,3.415449711124772e-2,0.9664250719399228,0.1434954073855292]
(in another context,)
> let seed =Seed
$ read "(2989318136,3378525425,1773647758,1462200156,2794459678,2822254363)" > gen <-thawGen
seed > replicateM 10 (uniform01M
gen) [0.5755553189002691,0.4100640936040626,0.3263296794324586,0.24037805455705044,0.6100629823964789,0.9041809183707534,0.2989749433907653,3.415449711124772e-2,0.9664250719399228,0.1434954073855292]
An immutable snapshot of the state of a Gen
.
Instances
Eq Seed Source # | |
Show Seed Source # | |
PrimMonad m => FrozenGen Seed m Source # | |
Defined in System.Random.MRG32k3a.Stateful type MutableGen Seed m = (g :: Type) # freezeGen :: MutableGen Seed m -> m Seed # thawGen :: Seed -> m (MutableGen Seed m) # | |
type MutableGen Seed m Source # | |
Defined in System.Random.MRG32k3a.Stateful |
fromSeed :: Seed -> (Word32, Word32, Word32, Word32, Word32, Word32) Source #
Convert seed into a 6-tuple of Word32
.
References
[1] Pierre L'Ecuyer, (1999) Good Parameters and Implementations for Combined Multiple Recursive Random Number Generators.Operations Research 47(1):159-164. https://doi.org/10.1287/opre.47.1.159