Skip to main content

Next-gen RandomState supporting multiple PRNGs

Project description

Travis Build Status Appveyor Build Status PyPI version

This is a library and generic interface for alternative random generators in Python and NumPy.

Features

  • Immediate drop in replacement for NumPy’s RandomState

# import numpy.random as rnd
import randomstate as rnd
x = rnd.standard_normal(100)
y = rnd.random_sample(100)
z = rnd.randn(10,10)
  • Default random generator is identical to NumPy’s RandomState (i.e., same seed, same random numbers).

  • Support for random number generators that support independent streams and jumping ahead so that substreams can be generated

  • Faster random number generation, especially for Normals using the Ziggurat method

import randomstate as rnd
w = rnd.standard_normal(10000, method='zig')
  • Support for 32-bit floating randoms for core generators. Currently supported:

    • Uniforms (random_sample)

    • Exponentials (standard_exponential)

    • Normals (standard_normal, both Box-Muller and Ziggurat)

    • Standard Gammas (via standard_gamma)

WARNING: The 32-bit generators are experimental and subjust to change.

Note: There are no plans to extend the alternative precision generation to all random number types.

  • Support for filling existing arrays using out keyword argument. Currently supported in (both 32- and 64-bit outputs)

    • Uniforms (random_sample)

    • Exponentials (standard_exponential)

    • Normals (standard_normal)

    • Standard Gammas (via standard_gamma)

Included Pseudo Random Number Generators

This modules includes a number of alternative random number generators in addition to the MT19937 that is included in NumPy. The RNGs include:

Differences from numpy.random.RandomState

New Features

  • standard_normal, normal, randn and multivariate_normal all support an additional method keyword argument which can be bm or zig where bm corresponds to the current method using the Box-Muller transformation and zig uses the much faster (100%+) ziggurat method.

  • Core random number generators can produce either single precision (np.float32) or double precision (np.float64, the default) using an the optional keyword argument dtype

  • Core random number generators can fill existin arrays using the out keyword argument

New Functions

  • random_entropy - Read from the system entropy provider, which is commonly used in cryptographic applications

  • random_raw - Direct access to the values produced by the underlying PRNG. The range of the values returned depends on the specifics of the PRNG implementation.

  • jump - Jumps RNGs that support it. jump moves the state a great distance. Only available if supported by the RNG.

  • advance - Advanced the core RNG ‘as-if’ a number of draws were made, without actually drawing the numbers. Only available if supported by the RNG.

Status

  • Complete drop-in replacement for numpy.random.RandomState. The mt19937 generator is identical to numpy.random.RandomState, and will produce an identical sequence of random numbers for a given seed.

  • Builds and passes all tests on:

  • Linux 32/64 bit, Python 2.7, 3.4, 3.5, 3.6 (probably works on 2.6 and 3.3)

  • PC-BSD (FreeBSD) 64-bit, Python 2.7

  • OSX 64-bit, Python 2.7

  • Windows 32/64 bit (only tested on Python 2.7 and 3.5, but should work on 3.3/3.4)

Version

The version matched the latest version of NumPy where randomstate.prng.mt19937 passes all NumPy test.

Documentation

An occasionally updated build of the documentation is available on my github pages.

Plans

This module is essentially complete. There are a few rough edges that need to be smoothed.

  • Stream support for MLFG

  • Creation of additional streams from a RandomState where supported (i.e. a next_stream() method)

Requirements

Building requires:

  • Python (2.7, 3.4, 3.5, 3.6)

  • NumPy (1.9, 1.10, 1.11, 1.12)

  • Cython (0.22, 0.23, 0.24, 0.25)

  • tempita (0.5+), if not provided by Cython

Testing requires nose (1.3+).

Note: it might work with other versions but only tested with these versions.

Development and Testing

All development has been on 64-bit Linux, and it is regularly tested on Travis-CI. The library is occasionally tested on Linux 32-bit,
OSX 10.10, PC-BSD 10.2 (should also work on Free BSD) and Windows (Python 2.7/3.5, both 32 and 64-bit).

Basic tests are in place for all RNGs. The MT19937 is tested against NumPy’s implementation for identical results. It also passes NumPy’s test suite.

Installing

python setup.py install

SSE2

dSFTM makes use of SSE2 by default. If you have a very old computer or are building on non-x86, you can install using:

python setup.py install --no-sse2

Windows

Either use a binary installer, or if building from scratch, use Python 3.5 with Visual Studio 2015 Community Edition. It can also be build using Microsoft Visual C++ Compiler for Python 2.7 and Python 2.7, although some modifications may be needed to distutils to find the compiler.

Using

The separate generators are importable from randomstate.prng.

import randomstate
rs = randomstate.prng.xorshift128.RandomState()
rs.random_sample(100)

rs = randomstate.prng.pcg64.RandomState()
rs.random_sample(100)

# Identical to NumPy
rs = randomstate.prng.mt19937.RandomState()
rs.random_sample(100)

Like NumPy, randomstate also exposes a single instance of the mt19937 generator directly at the module level so that commands like

import randomstate
randomstate.standard_normal()
randomstate.exponential(1.0, 1.0, size=10)

will work.

License

Standard NCSA, plus sub licenses for components.

Performance

Performance is promising, and even the mt19937 seems to be faster than NumPy’s mt19937.

Speed-up relative to NumPy (Uniform Doubles)
************************************************************
randomstate.prng-dsfmt-random_sample               313.5%
randomstate.prng-mlfg_1279_861-random_sample       459.4%
randomstate.prng-mrg32k3a-random_sample            -57.6%
randomstate.prng-mt19937-random_sample              72.5%
randomstate.prng-pcg32-random_sample               232.8%
randomstate.prng-pcg64-random_sample               330.6%
randomstate.prng-xoroshiro128plus-random_sample    609.9%
randomstate.prng-xorshift1024-random_sample        348.8%
randomstate.prng-xorshift128-random_sample         489.7%

Speed-up relative to NumPy (Normals using Box-Muller)
************************************************************
randomstate.prng-dsfmt-standard_normal                26.8%
randomstate.prng-mlfg_1279_861-standard_normal        30.9%
randomstate.prng-mrg32k3a-standard_normal            -14.8%
randomstate.prng-mt19937-standard_normal              17.7%
randomstate.prng-pcg32-standard_normal                24.5%
randomstate.prng-pcg64-standard_normal                26.2%
randomstate.prng-xoroshiro128plus-standard_normal     31.4%
randomstate.prng-xorshift1024-standard_normal         27.4%
randomstate.prng-xorshift128-standard_normal          30.3%

Speed-up relative to NumPy (Normals using Ziggurat)
************************************************************
randomstate.prng-dsfmt-standard_normal               491.7%
randomstate.prng-mlfg_1279_861-standard_normal       439.6%
randomstate.prng-mrg32k3a-standard_normal            101.2%
randomstate.prng-mt19937-standard_normal             354.4%
randomstate.prng-pcg32-standard_normal               531.0%
randomstate.prng-pcg64-standard_normal               517.9%
randomstate.prng-xoroshiro128plus-standard_normal    674.0%
randomstate.prng-xorshift1024-standard_normal        486.7%
randomstate.prng-xorshift128-standard_normal         617.0%

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

randomstate-1.12.zip (3.5 MB view details)

Uploaded Source

Built Distributions

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

randomstate-1.12-cp36-cp36m-win_amd64.whl (6.2 MB view details)

Uploaded CPython 3.6mWindows x86-64

randomstate-1.12-cp35-cp35m-win_amd64.whl (9.3 MB view details)

Uploaded CPython 3.5mWindows x86-64

randomstate-1.12-cp35-cp35m-macosx_10_6_x86_64.whl (6.8 MB view details)

Uploaded CPython 3.5mmacOS 10.6+ x86-64

randomstate-1.12-cp27-cp27m-win_amd64.whl (6.2 MB view details)

Uploaded CPython 2.7mWindows x86-64

randomstate-1.12-cp27-cp27m-macosx_10_7_x86_64.whl (7.1 MB view details)

Uploaded CPython 2.7mmacOS 10.7+ x86-64

File details

Details for the file randomstate-1.12.zip.

File metadata

  • Download URL: randomstate-1.12.zip
  • Upload date:
  • Size: 3.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for randomstate-1.12.zip
Algorithm Hash digest
SHA256 62251c13775d390436348ea7348d252d11e67f32cc5f2d9206d4a334da057603
MD5 0e26138df23b0644e4e2e9448f8d1076
BLAKE2b-256 b89ddeb5cee915f1120931371b27f449b113e4bd3f34173703ef3f0e09bc2a7a

See more details on using hashes here.

File details

Details for the file randomstate-1.12-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for randomstate-1.12-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 bc5a42777d291d08872d67e925d9e8570fb340d3ab2e2ff7ad50716ecfb828e4
MD5 ee845e76420beaec43504a8d510424a9
BLAKE2b-256 898eb404f793884fd515eaf271485f438d00f93a558e217be483ae7149286af5

See more details on using hashes here.

File details

Details for the file randomstate-1.12-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for randomstate-1.12-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 3bfb3cd95fee98fda0c2014621c39857ff139e8ac3374349c87b7cb2e27a0480
MD5 cc71fc5e0ae48e1b00e6043cf5b9aa51
BLAKE2b-256 5e4a0a54066115794ca60ed11ed2e09102ee4700d8385f0fd3d25bdd7d5c197c

See more details on using hashes here.

File details

Details for the file randomstate-1.12-cp35-cp35m-macosx_10_6_x86_64.whl.

File metadata

File hashes

Hashes for randomstate-1.12-cp35-cp35m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 ea6fd3b50f5300a9bbe58e78fb0d85b08d28bc10337048d5bb866d35ff65cd2e
MD5 3bb61715a616f33581f252a0e322032a
BLAKE2b-256 60c6b4780b675741e1b8fbb2e1ae6057c856dfe02d370784b6cacbdb81ba53f8

See more details on using hashes here.

File details

Details for the file randomstate-1.12-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for randomstate-1.12-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 127e46cd53bdc62bbbf7a8220c6c16f67b210d9b1b6c8389851debcdabb79d2b
MD5 51fed6b791fc002e1f64eb628c7dde87
BLAKE2b-256 3c9931fffe88a3797373d30d42e9263c5d51f94468b2df9f245da8bcd459c238

See more details on using hashes here.

File details

Details for the file randomstate-1.12-cp27-cp27m-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for randomstate-1.12-cp27-cp27m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 65b7d2acb4dbe0e83e0fbeee1126f6437bbdfe6baf645a9d66540c3d978c9997
MD5 c89dbbb40dc060d14cded8e78d5ad416
BLAKE2b-256 fe9f6f7301f7d21d21d9a085269ad8181492f9dd60dabdecba52a52dd77b64ee

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