mrg-random-0.1.0.0: Pseudo Random Number Generator with MRG (Multiple Recursive Generator)

Copyright(c) 2020 Naoyuki MORITA
LicenseBSD3
Maintainernaoyuki.morita@gmail.com
Stabilityexperimental
Portabilityportable
Safe HaskellNone
LanguageHaskell2010

System.Random.MRG63k3a.Stateful

Contents

Description

Pseudo-random number generation with MRG63k3a [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 1234567
  > replicateM 10 (uniformM gen) :: IO [Word32]
  [2246106302,1563963788,2439712072,3737154441,2667077669,767817191,747111673,2638409746,3331088863,4075662417]
  > replicateM 10 (uniformM gen) :: IO [Word32]
  [1456421684,2935764772,936846699,649810874,4215441082,311517124,1039486180,751453058,3053799799,1547236802]
Synopsis

Gen: Pseudo-Random Number Generators

data Gen s Source #

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

initialize :: PrimMonad m => Int64 -> m (Gen (PrimState m)) Source #

Create a generator using given seed.

Type helpers

type GenIO = Gen (PrimState IO) Source #

A shorter name for PRNG state in the IO monad.

type GenST s = Gen (PrimState (ST s)) Source #

A shorter name for PRNG state in the ST monad.

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 1234567
  > replicateM 10 (uniform01M gen)
  [0.9964374245717021,0.9073161749933566,0.308369875218738,0.2928356495081096,0.6407970127747293,0.8444224582886195,0.1358954027173811,0.4542392932876494,0.8016794723344877,0.8370627714083252]
  > seed <- freezeGen gen
  > show $ fromSeed seed
  "(8956691725955036650,4881994573324246905,544766949767175019,4767073730205058520,6711178582528615333,2047597627722241317)"
  > replicateM 10 (uniform01M gen)
  [0.43854909685398463,0.3675952030734795,0.9681374152275398,0.7952475446049576,0.645021516355446,0.3490345515648514,0.13967842526828145,0.6463610214064653,0.3197503428491851,0.40268376160424424]

(in another context,)

  > let seed = Seed $ read "(8956691725955036650,4881994573324246905,544766949767175019,4767073730205058520,6711178582528615333,2047597627722241317)"
  > gen <- thawGen seed
  > replicateM 10 (uniform01M gen)
  [0.43854909685398463,0.3675952030734795,0.9681374152275398,0.7952475446049576,0.645021516355446,0.3490345515648514,0.13967842526828145,0.6463610214064653,0.3197503428491851,0.40268376160424424]

data Seed Source #

An immutable snapshot of the state of a Gen.

Instances
Eq Seed Source # 
Instance details

Defined in System.Random.MRG63k3a.Stateful

Methods

(==) :: Seed -> Seed -> Bool #

(/=) :: Seed -> Seed -> Bool #

Show Seed Source # 
Instance details

Defined in System.Random.MRG63k3a.Stateful

Methods

showsPrec :: Int -> Seed -> ShowS #

show :: Seed -> String #

showList :: [Seed] -> ShowS #

PrimMonad m => FrozenGen Seed m Source # 
Instance details

Defined in System.Random.MRG63k3a.Stateful

Associated Types

type MutableGen Seed m = (g :: Type) #

Methods

freezeGen :: MutableGen Seed m -> m Seed #

thawGen :: Seed -> m (MutableGen Seed m) #

type MutableGen Seed m Source # 
Instance details

Defined in System.Random.MRG63k3a.Stateful

fromSeed :: Seed -> (Word64, Word64, Word64, Word64, Word64, Word64) Source #

Convert seed into a 6-tuple of Word64.

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