Skip to main content

Next-gen RandomState supporting multiple PRNGs

Project description

randomstate

Travis Build Status Appveyor Build Status PyPI version

Future development in randomgen

End-of-life notification

This library was designed to bring alternative generators to the NumPy infrastructure. It as been successful in advancing the conversation for a future implementation of a new random number API in NumPy which will allow new algorithms and/or generators. The next step in this process is to separate the basic (or core RNG) from the functions that transform random bits into useful random numbers. This has been implemented in a successor project randomgen available on GitHub or PyPi.

randomgen has a slightly different API, so please see the randomgen documentation.


Introduction

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 sub-streams can be generated

  • Faster random number generation, especially for normal, standard exponential and standard gamma using the Ziggurat method

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

    • Uniforms (random_sample)

    • Exponentials (standard_exponential, both Inverse CDF and Ziggurat)

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

    • Standard Gammas (via standard_gamma, both Inverse CDF and Ziggurat)

WARNING: The 32-bit generators are experimental and subject 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.

  • standard_exponential and standard_gamma both support an additional method keyword argument which can be inv or zig where inv corresponds to the current method using the inverse CDF 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 existing 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.

  • random_uintegers - unsigned integers, either 32- ([0, 2**32-1]) or 64-bit ([0, 2**64-1])

  • 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, 3.5 and 3.6, 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, not 0.23, 0.24, 0.25)

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

Testing requires pytest (3.0+).

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

randomstate-1.14.0-cp36-cp36m-win_amd64.whl (8.2 MB view details)

Uploaded CPython 3.6mWindows x86-64

randomstate-1.14.0-cp36-cp36m-win32.whl (7.8 MB view details)

Uploaded CPython 3.6mWindows x86

randomstate-1.14.0-cp36-cp36m-manylinux1_x86_64.whl (9.8 MB view details)

Uploaded CPython 3.6m

randomstate-1.14.0-cp36-cp36m-manylinux1_i686.whl (9.1 MB view details)

Uploaded CPython 3.6m

randomstate-1.14.0-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (9.2 MB view details)

Uploaded CPython 3.6mmacOS 10.10+ Intel (x86-64, i386)macOS 10.10+ x86-64macOS 10.6+ Intel (x86-64, i386)macOS 10.9+ Intel (x86-64, i386)macOS 10.9+ x86-64

randomstate-1.14.0-cp35-cp35m-win_amd64.whl (8.1 MB view details)

Uploaded CPython 3.5mWindows x86-64

randomstate-1.14.0-cp35-cp35m-win32.whl (7.7 MB view details)

Uploaded CPython 3.5mWindows x86

randomstate-1.14.0-cp35-cp35m-manylinux1_x86_64.whl (9.6 MB view details)

Uploaded CPython 3.5m

randomstate-1.14.0-cp35-cp35m-manylinux1_i686.whl (9.0 MB view details)

Uploaded CPython 3.5m

randomstate-1.14.0-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (9.1 MB view details)

Uploaded CPython 3.5mmacOS 10.10+ Intel (x86-64, i386)macOS 10.10+ x86-64macOS 10.6+ Intel (x86-64, i386)macOS 10.9+ Intel (x86-64, i386)macOS 10.9+ x86-64

randomstate-1.14.0-cp27-cp27mu-manylinux1_x86_64.whl (9.6 MB view details)

Uploaded CPython 2.7mu

randomstate-1.14.0-cp27-cp27mu-manylinux1_i686.whl (9.0 MB view details)

Uploaded CPython 2.7mu

randomstate-1.14.0-cp27-cp27m-win_amd64.whl (8.2 MB view details)

Uploaded CPython 2.7mWindows x86-64

randomstate-1.14.0-cp27-cp27m-win32.whl (7.7 MB view details)

Uploaded CPython 2.7mWindows x86

randomstate-1.14.0-cp27-cp27m-manylinux1_x86_64.whl (9.6 MB view details)

Uploaded CPython 2.7m

randomstate-1.14.0-cp27-cp27m-manylinux1_i686.whl (9.0 MB view details)

Uploaded CPython 2.7m

randomstate-1.14.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (9.5 MB view details)

Uploaded CPython 2.7mmacOS 10.10+ Intel (x86-64, i386)macOS 10.10+ x86-64macOS 10.6+ Intel (x86-64, i386)macOS 10.9+ Intel (x86-64, i386)macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for randomstate-1.14.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 b793e031c2ef8bbe7612305d1668fafba75ac6c3bf4bfc7b0db367650ac62432
MD5 cae781cd581d21a3e18f06845329f8c9
BLAKE2b-256 4d789362df7425c7c62c3c39450e2a3757c54b9c5afc9d352db9e78959a04984

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 9591bed2abe53c4d97c2bf4296c77020ccc6aa47bec5fdf9fbd2a71fd127cf8b
MD5 0e726bd62ec7e353bda3c94d451644c2
BLAKE2b-256 c54cc72a7070763b566c37665f95a9d3ccafad2b031ba69225f7031a1f0955da

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e6a8512bbc85b3244913d60a6371e1859411fa78874e02a63a816e9a64d6de44
MD5 6a868d35a6d4c73717be8eb21e9144e1
BLAKE2b-256 eaeb44990f03e38e155f739e31c229ccb44d38a9114fde03af6e2ecc5590ad37

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 2d0ce13b8f96cbb0de5fe7b8b69e1c661ad079809b74792bda534f10e8a91bb4
MD5 71a0b5f31b9a604c0936ee5e71afe391
BLAKE2b-256 55dc8c3f742a5ff79404a2ac396452743d876983ac15091ef5bf3d359d4f1560

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 75c90cf94a7d421a1cc516d67a3bd49ea253775ceac6012fdd269b7e7f0ee614
MD5 5c9773e44cfeca15a2a13eba51eace12
BLAKE2b-256 8b38e995d794a488924d27e510a0221ae1b85c9bec2e7470940f0b68eb139035

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for randomstate-1.14.0-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 f9e8ec51d6e657e91e341ea23cfe5c7a747534c96296a1728c209088aa397010
MD5 7b79cac237dd5f635c94bc351424505b
BLAKE2b-256 db5aa6cba4e0429400ec86686ffd20c563743b810295e37415381fe8fc70b541

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 91579a39649a975b199ad9fc81a7c387462a99e7294bdae1a0a70ff029a0adc6
MD5 394ffb038fc3419c781c8dd2a9c24abb
BLAKE2b-256 62fbf5b46305316e9b97a145ff2d45bee6374aba0bc256c095efa0f093075fef

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 50d3e9031c91992d2c4a6cd5077bde836dc43263380d4252143aaaa708f483f3
MD5 2acdaca7a14f4674b1e26caadef91148
BLAKE2b-256 613c3cce4e3fd2b6f67fcf0dd250d4e532fb89fb240b6c73edb9b319873514d5

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 d28c628bc0ffee3ce12c94dae79b2b5b053067a8b6f2bdd7dba057d6231aa278
MD5 5ac4bbab6f5a0d700c3563a2b87d8342
BLAKE2b-256 f0d5a4410aa06c4e93dc32cd66cd8d7796a7e952b9e74ef3a2bec019a1192c02

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 378f132327ff7aff63323378d3378d22a246e25859dbf1fe3a367be02320c242
MD5 b9eabe70777229fd4c740dc03b0ca425
BLAKE2b-256 1149448da9919c110007ee30597f7c11b81c7f037363baf626c73c5300d753d4

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 89bd1871420fb051daf74cf440f14d50555c81c98b015a3f2aa6990128ad5107
MD5 a8acac7dc6cd3c02427f926e987ccaa0
BLAKE2b-256 1c58f0e28ca32fc8c36c9506a64a38ec6d13411beabad9b578336dd166307838

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp27-cp27mu-manylinux1_i686.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 7fb4b69a175a5c83501e5c5ad0b95609cbbd1dd0645cdfb69b119d1d50ddfe64
MD5 31136a0ff5bc91a0a612d8bc6ef6603f
BLAKE2b-256 fb57f5e89fce64867069226043e9ccdc25f65fe9ca0e746db52d0c5a6dc40819

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for randomstate-1.14.0-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 1bca180e8664d0e462b76e82bd147fa21569e6a5b78cd644c73b0e7c3028f944
MD5 63c92ab9ad61605fc2006f5c61b549bf
BLAKE2b-256 3ab1af58dee394d9b5d1fa9a480437e2bd802c0acf2962f7183989c11d1f09f5

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 993538e0df87194bb8b672689c8c5f958fa885445667d2f45a17276db1b4afb8
MD5 9444c24ee7f7c32a1f6ee292d84f892c
BLAKE2b-256 313b82ed6a8397e30726d2ef763603dedb3c35c2f6bedd61acc8e683a6a5d4a4

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 355535ab66e92443e5f35a751ef8ea0ac53022964db86f795f273169a62d94f4
MD5 a55de16cd506c7bb69a601afff8abc56
BLAKE2b-256 156d660417d337e79f11b433cbcbd56e1a6ba22f12b98d57a85ec5ea210315c7

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp27-cp27m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 6144ea600cd6e99f4c71780eb6104cd038fd1808508e9380dc01a332e6af61e9
MD5 e3a94f75d171488354c9059348955a69
BLAKE2b-256 d6f249bc93089bcb1fded6229f58c0e797dbccc40dc04f4c845c1a62f46307a8

See more details on using hashes here.

File details

Details for the file randomstate-1.14.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for randomstate-1.14.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 29670fe09a4ebad25a0d3c2d082e019b92e785d6023dd58974bb011104450679
MD5 e742e3acd1c5d2dbac26c3abd3343e49
BLAKE2b-256 72a735143feca6bdf965eb8f7ec02c9fc1a72f15f5637c73afc24b59e2fc5f87

See more details on using hashes here.

Supported by

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