Simple random number generators
Simple pseudo-random number generators.
The simplerandom package is provided, which contains modules containing classes for various simple pseudo-random number generators.
One module provides Python iterators, which generate simple unsigned 32-bit integers identical to their C counterparts.
Another module provides random classes that are sub-classed from the class Random in the random module of the standard Python library.
Why use this package? These random number generators are very simple, which has two main advantages:
An equivalent C implementation (of the Python simplerandom.iterators module) has been created. See:
Most algorithms were obtained from two newsgroup posts by George Marsaglia  . However, some modifications have been made. From , it seems that the SHR3 algorithm defined in  is flawed and should not be used. It doesn’t actually have a period of 2**32-1 as expected, but has 64 different cycles, some with very short periods. The SHR3 in the 2003 post is very similar, but with two shift values swapped. It has a period of 2**32-1 as expected.
We still find KISS from  useful mainly because it uses 32-bit calculations for MWC, which can be more suitable for small embedded systems. So we define KISS that uses a MWC based on , but the Cong and SHR3 from .
|||(1, 2, 3, 4, 5, 6, 7) |
Newsgroup post, sci.stat.math and others, Thu, 21 Jan 1999
|||(1, 2, 3, 4, 5, 6, 7) |
Newsgroup post, sci.math, 26 Feb 2003
|||(1, 2) |
Mathematics of Computation, 68, 225 (1999), 261–269.
|||(1, 2) |
|||(1, 2) |
Mathematics of Computation, 65, 213 (1996), 203–213.
|simplerandom.iterators||Iterator classes, which generate unsigned 32-bit integers.|
|simplerandom.random||Classes that conform to standard Python random.Random API.|
In simplerandom.iterators, the following pseudo-random number generators are provided:
|MWC1||Two 32-bit MWCs combined. From .|
|MWC2||Very similar to MWC1, but slightly modified to improve its statistical properties.|
|MWC64||A single 64-bit multiply-with-carry calculation. From .|
|KISS||Combination of MWC2, Cong and SHR3. Based on  but using Cong and SHR3 from , and the modified MWC.|
|KISS2||Combination of MWC64, Cong and SHR3. From .|
|LFSR113||Combined LFSR (Tausworthe) random number generator by L’Ecuyer. From  .|
|LFSR88||Combined LFSR (Tausworthe) random number generator by L’Ecuyer. From .|
These generators are Python iterators, of infinite length (they never raise StopIteration). They implement the next() method (__next__() in Python 3.x) to generate the next random integer. All the generators output 32-bit unsigned values, and take one or more 32-bit seed values during initialisation/seeding.
In simplerandom.random, pseudo-random number generators are provided which have the same names as those in simplerandom.iterators, but these generators implement the standard Python random.Random API. Each generator uses the iterator of the same name in simplerandom.iterators to generate the random bits used to produce the random floats. The jumpahead() function (Python 2.x) is implemented in all cases, and is also supported in Python 3.x, even though jumpahead() has officially been removed from the Python 3.x random API.
>>> import simplerandom.iterators as sri >>> rng = sri.KISS(123958, 34987243, 3495825239, 2398172431) >>> next(rng) 702862187L >>> next(rng) 13888114L >>> next(rng) 699722976L
>>> import simplerandom.random as srr >>> rng = srr.KISS(258725234) >>> rng.random() 0.0925917826051541 >>> rng.random() 0.02901686453730415 >>> rng.random() 0.9024972981686489
Currently this has had basic testing on Ubuntu 13.04 64-bit and Windows XP 32-bit. It passes the simplerandom.iterators.test unit tests, as well as basic manual testing of simplerandom.random. A more thorough unit test suite is needed.
In Ubuntu, it has been tested on Python 2.7 and 3.3, and passes.
In Windows, it has been tested on Python 2.6, 2.7, 3.1, 3.2 and 3.3. It passes under these versions.
Cython is used to make a fast implementation of simplerandom.iterators. Cython creates a .c file that can be compiled into a Python binary extension module.
The simplerandom source distribution package includes a .c file that was created with Cython, so it is not necessary to have Cython installed to install simplerandom.
The Cython .pyx file is also included, if you want to modify the Cython source code, in which case you do need to have Cython installed. But by default, setup.py builds the extension from the .c file (to ensure that the build doesn’t fail due to particular Cython version issues). If you wish to build using Cython from the included .pyx file, you must set USE_CYTHON=True in setup.py.
The simplerandom package is installed using distutils. If you have the tools installed to build a Python extension module, run the following command:
python setup.py install
If you cannot build the C extension, you may install just the pure Python implementation, using the following command:
python setup.py build_py install --skip-build
Unit testing of the iterators is in simplerandom.iterators.test. It duplicates the tests of the C algorithms given in the original newsgroup post , as well as other unit tests.
To run unit tests:
python -m simplerandom.iterators.test
A more thorough unit test suite is needed. A unit test suite for simplerandom.random is needed.
The code is released under the MIT license. See LICENSE.txt for details.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|simplerandom-0.12.1.tar.gz (165.5 kB) Copy SHA256 Checksum SHA256||–||Source||Nov 12, 2013|
|simplerandom-0.12.1.win32-py2.6.msi (213.0 kB) Copy SHA256 Checksum SHA256||2.6||Windows MSI Installer||Nov 12, 2013|
|simplerandom-0.12.1.win32-py2.7.msi (229.4 kB) Copy SHA256 Checksum SHA256||2.7||Windows MSI Installer||Nov 12, 2013|
|simplerandom-0.12.1.win32-py3.2.msi (213.0 kB) Copy SHA256 Checksum SHA256||3.2||Windows MSI Installer||Nov 12, 2013|
|simplerandom-0.12.1.win32-py3.3.msi (229.4 kB) Copy SHA256 Checksum SHA256||3.3||Windows MSI Installer||Nov 12, 2013|
|simplerandom-0.12.1.zip (180.3 kB) Copy SHA256 Checksum SHA256||–||Source||Nov 12, 2013|