Skip to main content

A Python implementation of the mrg32k3a pseudo-random number generator.

Project description

mrg32k3a

This package provides a Python implementation of the mrg32k3a pseudo-random number generator of L'Ecuyer (1999) and L'Ecuyer et al. (2002). It extends the implementation used in PyMOSO to handle streams, substreams, and subsubstreams. The generator's period of ~2191 is split into ~250 streams of length 2141, each containing 247 substreams of length 294, each containing 247 subsubstreams of length 247.

Details

The mrg32k3a module includes the MRG32k3a class and several useful functions for controlling the generators.

  • The MRG32k3a class is a subclass of Python's random.Random class and therefore inherits easy-to-use methods for generating random variates. E.g., if rng is an instance of the MRG32k3a class, the command rng.normalvariate(mu=2, sigma=5) generates a normal random variate with mean 2 and standard deviation 5. Normal random variates are generated via inversion using the Beasley-Springer-Moro algorithm.
  • The MRG32k3a class expands the suite of functions for random-variate generation available in random.Random to include lognormalvariate, mvnormalvariate, poissonvariate, gumbelvariate, binomialvariate. Additionally, the methods integer_random_vector_from_simplex and continuous_random_vector_from_simplex generate discrete and continuous vectors from a symmetric non-negative simplex.
  • The advance_stream, advance_substream, and advance_subsubstream functions advance the generator to the start of the next stream, substream, or subsubstream, respectively. They make use of techniques for efficiently "jumping ahead," as outlined by L'Ecuyer (1990).
  • The reset_stream, reset_substream, and reset_subsubstream functions reset the generator to the start of the current stream, substream, or subsubstream, respectively.

Installation

The mrg32k3a package is available to download through the Python Packaging Index (PyPI) and can be installed from the terminal with the following command:

python -m pip install mrg32k3a

Basic Example

After installing mrg32k3a, the package's main class (MRG32k3a) can be imported from the Python console (or in code):

from mrg32k3a.mrg32k3a import MRG32k3a

One can instantiate a random number generator set at a given stream, substream, and subsubstream triplet or seed. For example, the command

rng = MRG32k3a(s_ss_sss_index=[1, 2, 3])

creates a object of the MRG32k3a class called rng that it initialized at the start of subsubstream 3 of substream 2 of stream 1. If the argument s_ss_sss_index is not provided, the random number generator is initialized at stream-substream-subsubstream 0-0-0. (We adopt the Python convention of indexing from 0.) Alternatively, the command

rng = MRG32k3a(ref_seed=(12345, 12345, 12345, 12345, 12345, 12345))

initializes the random number generator at the state described by the length-6 tuple (12345, 12345, 12345, 12345, 12345, 12345). Streams, substreams, and subsubstreams are indexed using ref_seed as a point of reference.

After instantiating a random number generator, its methods can be invoked to generate (scalar or vector) random variates from a particular probability distribution. For example,

x = rng.normalvariate(mu=2, sigma=5)

returns a normally distributed random variate x with mean 2 and standard deviation 5.

Similarly,

x = rng.poissonvariate(lmdba=50)

returns a Poisson distributed random variate x with rate parameter (mean) 50.

Finally,

v = rng.integer_random_vector_from_simplex(n_elements=3, summation=10, with_zero=False))

returns a random length-3 vector v of positive integers summing to 10. The vector v is uniformly distributed over the set of such vectors.

Documentation

Full documentation for the mrg32k3a source code can be found here.

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mrg32k3a-2.0.1.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mrg32k3a-2.0.1-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file mrg32k3a-2.0.1.tar.gz.

File metadata

  • Download URL: mrg32k3a-2.0.1.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mrg32k3a-2.0.1.tar.gz
Algorithm Hash digest
SHA256 9de9ba5a6a08a44c8404cf67e9840cbff98c6e8d115db68cdb3d45d159ead150
MD5 5b23c105f227bc721be3acd52f152b05
BLAKE2b-256 8e9ae79ed565e3e100a8743eeeaa32ff8d3b55cfad4b4f7712dd43d680cef724

See more details on using hashes here.

File details

Details for the file mrg32k3a-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: mrg32k3a-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mrg32k3a-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f5fb07237d4da92b36553f93bf1a1f6e84b3d738331911c06dcb81165316c588
MD5 5167b2aa13836c89253b4ce2807dcaaf
BLAKE2b-256 339a5652f25e92111462a6ac0d2c5bf0df63adc9e10ea3ee1cdda70aace1cda8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page