Simple random number generators
Project description
- Contact:
- http://craig.mcqueen.id.au/
- Copyright:
- 2010 Craig McQueen
Simple pseudo-random number generators, from George Marsaglia.
Intro
The simplerandom package is provided, which contains modules containing classes for various simple pseudo-random number generators.
The algorithms were obtained from a newsgroup post by George Marsaglia [1]. The algorithms were specified in C in the newsgroup post. For the purpose of prototyping software in a high-level language such as Python, before writing it in faster C, it is useful to have identical algorithms implemented in both Python and C.
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.
References
Modules Provided
Module |
Description |
---|---|
simplerandom.iterators |
Iterator classes, which generate unsigned 32-bit integers. |
simplerandom.random |
Classes that conform to standard Python random.Random API. |
Random Number Generators Provided
In simplerandom.iterators, the following pseudo-random number generators are provided:
Generator |
Notes |
---|---|
RandomCongIterator |
|
RandomCong2Iterator |
Very similar to Cong, but different added constant and default seed. |
RandomSHR3Iterator |
|
RandomSHR3_2Iterator |
Similar to SHR3, but different shift values and default seed. |
RandomMWCIterator |
|
RandomMWC64Iterator |
A single 64-bit multiply-with-carry calculation. |
RandomKISSIterator |
Combination of MWC, Cong and SHR3 |
RandomKISS2Iterator |
Combination of MWC64, Cong2 and SHR3_2 |
RandomLFIB4Iterator |
|
RandomSWBIterator |
|
_RandomFibIterator |
Not useful on its own, but can be used in a combination with other generators. |
In simplerandom.random, the following pseudo-random number generators are provided:
Generator |
Notes |
---|---|
RandomCong |
|
RandomSHR3 |
|
RandomMWC |
|
RandomKISS |
Combination of MWC, Cong and SHR3 |
RandomLFIB4 |
|
RandomSWB |
Usage
Iterators
>>> import simplerandom.iterators as sri >>> rng = sri.RandomKISSIterator(123958, 34987243, 3495825239, 2398172431) >>> next(rng) 21111917L >>> next(rng) 1327965872L >>> next(rng) 2128842716L
Random class API
>>> import simplerandom.random as srr >>> rng = srr.RandomKISS(258725234) >>> rng.random() 0.30631872435766117 >>> rng.random() 0.43903576112750442 >>> rng.random() 0.69756297733927486
Supported Python Versions
Currently this has had basic testing on Ubuntu 10.04 32-bit and Windows XP 32-bit. It passes the basic 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.6 and 3.1 and passes.
In Windows, it has been tested on Python 2.4, 2.5, 2.6, 2.7 and 3.1. It passes under these versions.
With Cython 0.14, one unit test shows an error with a certain seed value of the MWC64 iterator.
The pure Python code is expected to work on 64-bit platforms, but has not been tested. The Cython version of simplerandom.iterators should work on 64-bit platforms, but has not been tested.
Use of Cython
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.
If Cython is installed, the setup.py file will build the code using the Cython .pyx file (updating the .c file as necessary). If Cython is not installed, the setup.py file will build the code from the included .c file.
Installation
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
Basic unit testing of the iterators is in simplerandom.iterators.test. It duplicates the tests of the C algorithms given in the original newsgroup post [1], as well as other unit tests.
To run it on Python >=2.5:
python -m simplerandom.iterators.test
Alternatively, in the test directory run:
python test_iterators.py
A more thorough unit test suite is needed.
License
The code is released under the MIT license. See LICENSE.txt for details.
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 Distribution
Built Distributions
Hashes for simplerandom-0.8.0.win32-py3.2.msi
Algorithm | Hash digest | |
---|---|---|
SHA256 | a03a77c71434185982b98d74b682229ccf301311a3c02bf2d0bec8e46575ca65 |
|
MD5 | 380fcf9ed3aace94bddffd5770983a2e |
|
BLAKE2b-256 | 17d8be200754dc6291b1ce218719a43bc27041b04a01109f1b21a6d724bdc7fe |
Hashes for simplerandom-0.8.0.win32-py3.1.msi
Algorithm | Hash digest | |
---|---|---|
SHA256 | 606af379f8d4c6b269d47df9dd4dd11601676c91a8f502cd65bbbd3a14921afc |
|
MD5 | 57e5aaa07bbd66f85d4f1d60eee6ab52 |
|
BLAKE2b-256 | 7597e639d2bcae597e57acd643eecfff34ea2e272c392b65b33478a4b10faeff |
Hashes for simplerandom-0.8.0.win32-py2.7.msi
Algorithm | Hash digest | |
---|---|---|
SHA256 | 270abf70e79af575976a406fbfcd52c17c87dcb10421e122737d70f1755a0089 |
|
MD5 | 18775da85d5f154e26693730b6dc783a |
|
BLAKE2b-256 | 4b5fa2fc0c5eb4235ac557aae9c266d7d94ddbd19f126c44f2e396f79fcaa633 |
Hashes for simplerandom-0.8.0.win32-py2.6.msi
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17e6cf1c91033f30129b2e91f9ab5a6977908a0b7b6b4b19cc7141b4318b0743 |
|
MD5 | aba02edde1502cbc5c936a2cf236f69a |
|
BLAKE2b-256 | bc829b129b7e14709b31de73a03a2f4634a2f00159eb02a24d5fd37043f8abd5 |
Hashes for simplerandom-0.8.0.win32-py2.5.msi
Algorithm | Hash digest | |
---|---|---|
SHA256 | 750966ac54d4cdcb5d11b2d6cfcd01cbfa40ca0ec392cbc1270b6eade2d468cb |
|
MD5 | 9fbc507695699e312915cb68124a95c0 |
|
BLAKE2b-256 | 8acb90fb08912d82aff50d52e90c5167e5667579c225cf204d7423dcf14b8260 |