Next-gen RandomState supporting multiple PRNGs
Project description
This is a library and generic interface for alternative random generators in Python and Numpy.
Features
Immediate drop in replacement for Numy’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 ranomd number generations, especially for Normals using the Ziggurat method
import randomstate as rnd
w = rnd.standard_normal(10000, method='zig')
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
stanard_normal, normal, randn and multivariate_normal all support an additional method keyword argument which can be inv or zig where inv corresponds to the current method and zig uses tha much faster (100%+) ziggurat method.
New Functions
random_entropy - Read from the system entropy provider, which is commonly used in cryptographic applications
random_uintegers - unsigned integers [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.6, 2.7, 3.3, 3.4, 3.5
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 verion of NumPy where randomstate.prng.mt19937 passes all NumPy test.
Documentation
A 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 and MRG32K3A
Creation of additional streams from a RandomState where supported (i.e. a next_stream() method)
Requirements
Building requires:
Numpy (1.9, 1.10)
Cython (0.22, 0.23)
Python (2.6, 2.7, 3.3, 3.4, 3.5)
Note: it might work with other versions but only tested with these versions.
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 using Python 3.5 and the free 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 are 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 (Slow Normals) ************************************************************ randomstate.prng-dsfmt-standard_normal 107.2% randomstate.prng-mlfg_1279_861-standard_normal 51.2% randomstate.prng-mrg32k3a-standard_normal -11.8% randomstate.prng-mt19937-standard_normal 44.0% randomstate.prng-pcg32-standard_normal 51.2% randomstate.prng-pcg64-standard_normal 51.1% randomstate.prng-xorshift1024-standard_normal 50.5% randomstate.prng-xorshift128-standard_normal 52.1% Speed-up relative to NumPy (Ziggural Normals) ************************************************************ randomstate.prng-dsfmt-standard_normal 283.7% randomstate.prng-mlfg_1279_861-standard_normal 217.4% randomstate.prng-mrg32k3a-standard_normal 16.6% randomstate.prng-mt19937-standard_normal 201.3% randomstate.prng-pcg32-standard_normal 274.9% randomstate.prng-pcg64-standard_normal 310.8% randomstate.prng-xorshift1024-standard_normal 336.3% randomstate.prng-xorshift128-standard_normal 425.1%
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for randomstate-1.10-cp35-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2f310b793360b24b07814e5204d7d973fd9602ec53ab1b15343598287a3e498 |
|
MD5 | f9b494f0436a46ff87dac468f31f6ee8 |
|
BLAKE2b-256 | ffb940296b65223cad02e0bc77d78a0f77972d264e29bbfcf670d63c1f9b26bf |
Hashes for randomstate-1.10-cp35-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf19081bcb2ff0dad6a8b34da71216155adce310516e0e9d66ef6803a1ee8ec1 |
|
MD5 | 3c904de8ac27ee7e081ae065d488e75a |
|
BLAKE2b-256 | dcc704d626fb5ccaef97e4949d82285028c996ea3c9245f1a8f7693a1d3722ef |
Hashes for randomstate-1.10-cp35-cp35m-macosx_10_5_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06f0a1b334cc130153fb07715bacbef633ff2ec959beab54600a250560da7a27 |
|
MD5 | 42c82ba2cd71841949df35256a1b223b |
|
BLAKE2b-256 | 27fc9f0e22043d7b8efcf4ed8bc7e4af9ec47ffa50c08567bee4714bd51a3d36 |
Hashes for randomstate-1.10-cp27-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6847cd0a404487f937ec1461254b00313ae2d9863d842ea416d50d00d4d409d5 |
|
MD5 | 06123096ebb1a8e9d94f65f1f5c2b033 |
|
BLAKE2b-256 | e89aa9cd24dd6f8e048e196a91209bd088a3911af79736d0ba9590dbfe4b1faf |
Hashes for randomstate-1.10-cp27-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9decfc01e61ef1f635a9fded2e684cd07242a561bda8bfaf6cc860b078ce140 |
|
MD5 | ac0e0fd40435541ffb26cf90f330e27b |
|
BLAKE2b-256 | 06c4409e054922183d61739d93d1890bec0d02ab6538173dd75083f1a9feb7fe |
Hashes for randomstate-1.10-cp27-none-macosx_10_5_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28d6c581f84708d9da25c01bf168b922b75b88c4301cab647fbf2a5806912e06 |
|
MD5 | 09a5df0d96cb2cbc2057c23c9359584a |
|
BLAKE2b-256 | 2bd8284e7ce0667261c0b87ff72ed131bae8d5013f74ee62effb3119a863ccc4 |