Sci.PRNG: Pseudo RNGs

This module implements a number of pseudo random number generators (prngs) selected according to their efficiency and their satisfactory statistical properties:

NamePeriodResolution
lfib42^2872^32
kiss992^1232^32
mrg32k3a2^1912^53

Period indicates how many samples the prng is guaranteed to produce before starting to repeat the same sequence again. All the prng here implemented obtain uniform real numbers starting from integer numbers. Resolution indicates how many bits are used by the prng for these integer numbers. A higher resolution corresponds to a finer grid of possible real numbers on (0,1). In standard use cases a resolution higher than 2^32 is not needed.

Ecuyer07 provides an overview of many prngs including the ones provided by this module. The TestU01 library has been used to assess the statistical properties of the prngs here implemented. More in detail, they all pass the most stringent battery of tests provided by the TestU01 library (big crush).

API

In the following rng represents a generic prng.

u = rng:sample()

Returns independent and identically distributed random numbers which are uniformly distributed over the interval (0,1) extremes excluded.

rngcopy = rng:copy()

Returns an independent copy of the prng which is initialized with the same state of rng. This means that both rng and rngcopy generate the same sequence of random numbers from this point onward. It allows for the re-use of the same sequence of random numbers multiple times without having to store them.

str = tostring(rng)

Returns a string representing a serialization of the rng state. It allows to persist the rng between multiple runs of a simulation by for instance saving the string state to a file.

rng = prng.restore(str)

Takes as input a string returned from a call to tostring(rng) and returns a rng initialized with such state.

rng = prng.std()

Default choice for a prng (chosen among the ones listed below) based on its efficiency and statistical properties. This should be the default choice for users who do not have a strong preference for any specific pseudo random number generators.

rng = prng.lfib4()

Returns a prng based on the lfib4 algorithm according to Marsaglia99.

rng = prng.kiss99()

Returns a prng based on the kiss99 algorithm according to Marsaglia99.

rng = prng.mrg32k3a()

Returns a prng based on the mrg32k3a parametrization according to Ecuyer99.