Skip to main content
Files added late

2 files were added to this release more than 14 days after its initial publication. Inspect the release files before installing.

Author:

Craig McQueen

Contact:
http://craig.mcqueen.id.au/

Simple pseudo-random number generators.

Intro

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:

  • It is easy to port them to a different platform and/or language. It can be useful to be able to implement the identical algorithm on multiple platforms and/or languages.

  • Small and simple generators can be more appropriate for small embedded systems, with limited RAM and ROM.

An equivalent C implementation (of the Python simplerandom.iterators module) has been created. See:

http://github.com/cmcqueen/simplerandom

Algorithms

Most algorithms were obtained from two newsgroup posts by George Marsaglia [1] [2]. However, some modifications have been made. From [3], it seems that the SHR3 algorithm defined in [1] 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 [1] 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 [1], but the Cong and SHR3 from [2].

From Pierre L’Ecuyer [4] [6], the Combined LFSR (Tausworthe) LFSR113 algorithm [5] and LFSR88 (aka Taus88) have been implemented.

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

MWC1

Two 32-bit MWCs combined. From [1].

MWC2

Very similar to MWC1, but slightly modified to improve its statistical properties.

Cong

From [2].

SHR3

From [2].

MWC64

A single 64-bit multiply-with-carry calculation. From [2].

KISS

Combination of MWC2, Cong and SHR3. Based on [1] but using Cong and SHR3 from [2], and the modified MWC.

KISS2

Combination of MWC64, Cong and SHR3. From [2].

LFSR113

Combined LFSR (Tausworthe) random number generator by L’Ecuyer. From [4] [5].

LFSR88

Combined LFSR (Tausworthe) random number generator by L’Ecuyer. From [6].

These generators are Python iterators, of infinite length (they never raise StopIteration). They implement the __next__() function 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 (in the style of the Python 2.x API) is implemented in all cases, even though jumpahead() has officially been removed from the Python 3.x random API.

Usage

Iterators

>>> import simplerandom.iterators as sri
>>> rng = sri.KISS(123958, 34987243, 3495825239, 2398172431)
>>> next(rng)
702862187
>>> next(rng)
13888114
>>> next(rng)
699722976

Random class API

>>> import simplerandom.random as srr
>>> rng = srr.KISS(258725234)
>>> rng.random()
0.0925917826051541
>>> rng.random()
0.02901686453730415
>>> rng.random()
0.9024972981686489

Supported Python Versions

Python >= 3.10 are supported.

Python versions < 3.10 might work, but have 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. 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.

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

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 unit tests:

python -m simplerandom.iterators.test

A more thorough unit test suite is needed. A unit test suite for simplerandom.random is needed.

License

The code is released under the MIT license. See LICENSE.txt for details.

Metadata

Release files for simplerandom 0.13.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Files added late

2 files were uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release files before installing.

Source distribution (sdist)

Source distribution for simplerandom 0.13.8
File Size Uploaded
simplerandom-0.13.8.tar.gz 408.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for simplerandom 0.13.8
File
simplerandom-0.13.8-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
simplerandom-0.13.8-cp314-cp314-win32.whl CPython 3.14 CPython 3.14 Windows x86-32 Details
simplerandom-0.13.8-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
simplerandom-0.13.8-cp313-cp313-win32.whl CPython 3.13 CPython 3.13 Windows x86-32 Details
simplerandom-0.13.8-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
simplerandom-0.13.8-cp312-cp312-win32.whl CPython 3.12 CPython 3.12 Windows x86-32 Details

Total release size: 1.5 MB

Release files / simplerandom-0.13.8.tar.gz

Download URL simplerandom-0.13.8.tar.gz
Size 408.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c7adbc2cc2c778d84f99a6f18d4ff17b27780754962ea6ba9f65a8231b464fd3
BLAKE2b-256 checksum
How to use checksums
6ecc0ea4b1b7973394523eb0711d4b88fedc7e1bbb00224717e6855e1717a980
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.0

Release files / simplerandom-0.13.8-cp314-cp314-win_amd64.whl

File added late

This file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release file before installing.

Download URL simplerandom-0.13.8-cp314-cp314-win_amd64.whl
Size 202.9 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
01138a6be23e9e19bbbceef5acd58a08181ed74eedcc5c796ff25757855629fe
BLAKE2b-256 checksum
How to use checksums
886083403c022c022d58cbe5af67848113234c1c82c6a3071f374f41b215c7fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / simplerandom-0.13.8-cp314-cp314-win32.whl

File added late

This file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release file before installing.

Download URL simplerandom-0.13.8-cp314-cp314-win32.whl
Size 165.6 kB
Tags CPython 3.14 Windows x86-32
SHA-256 checksum
How to use checksums
a49bbeceaff667c749e3b33b49f2260392ac78d9128a4ebc8ebc131334cda180
BLAKE2b-256 checksum
How to use checksums
e10f0c37db7421b4c9f47b54bf24786a7c5beaf26c40763525a5c8a4adea82e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / simplerandom-0.13.8-cp313-cp313-win_amd64.whl

Download URL simplerandom-0.13.8-cp313-cp313-win_amd64.whl
Size 204.5 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
92bc893a323aaa9f3d9a34540495987aee775387f0ac5c57f0883cf323b177eb
BLAKE2b-256 checksum
How to use checksums
f86c5d14d77546338801ee906904155460b568861b46914ec4a76cfd156c43e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.0

Release files / simplerandom-0.13.8-cp313-cp313-win32.whl

Download URL simplerandom-0.13.8-cp313-cp313-win32.whl
Size 166.0 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
277d7434907ba658ec4d01cf4794d7590a45e12b6daa4f99919f29a83fcaf432
BLAKE2b-256 checksum
How to use checksums
55580d105d9ca18f88c3fe294ba77a4828daa3777e18305aa8505f16eb6299e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.0

Release files / simplerandom-0.13.8-cp312-cp312-win_amd64.whl

Download URL simplerandom-0.13.8-cp312-cp312-win_amd64.whl
Size 203.7 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
fdda2baa8f2372f2bd713d4eb60cd573c70e4bf4a0ae17a70bd37b60985aea64
BLAKE2b-256 checksum
How to use checksums
7e3bf145a5426f653f8bb8d39dc06ae9e2f93eb231664dc5224c2268a7fc1541
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.0

Release files / simplerandom-0.13.8-cp312-cp312-win32.whl

Download URL simplerandom-0.13.8-cp312-cp312-win32.whl
Size 165.9 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
bc4b3af288346e9bd48543e2cbded7e27dd4fc8cf11d37611a44da8793e46edc
BLAKE2b-256 checksum
How to use checksums
cee84d6e21bbd79696e1a139512771b9ccb51bfb930167065ca23650fb32f526
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.0

Release history Release notifications | RSS feed

This release

0.13.8 This release

7 release files

0.13.7

5 release files

0.13.6

7 release files

0.13.5

5 release files

0.13.4

7 release files

0.13.3

5 release files

0.13.2

3 release files

0.13.1

1 release file

0.13.0

1 release file

0.12.1

6 release files

0.10.0

7 release files

0.9.0

2 release files

0.8.0

6 release files

0.7.1

7 release files

0.6.2

2 release files

0.5.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page