Skip to main content

Fast random number generation in Python

Project description

fastrand

Fast random number generation in an interval in Python using PCG: Up to 10x faster than random.randint.

Blog post: Ranged random-number generation is slow in Python

Usage... (don't forget to type the above lines in your shell!)

import fastrand

print("generate an integer in [0,1001)")
fastrand.pcg32bounded(1001) 
print("generate an integer in [100,1000]")
fastrand.pcg32randint(100,1000) # requires Python 3.7 or better
print("Generate a random 32-bit integer.")
fastrand.pcg32()

if fastrand.SIXTYFOUR: # support for xorshift128+ is limited to some 64-bit platforms (linux, macos, etc.)
    print("generate an integer in [0,1001)")
    fastrand.xorshift128plusbounded(1001) 
    print("generate an integer in [100,1000]")
    fastrand.xorshift128plusrandint(100,1000) # requires Python 3.7 or better
    print("Generate a random 64-bit integer.")
    fastrand.xorshift128plus()

It is nearly an order of magnitude faster than the alternatives:


python3 -m timeit -s 'import fastrand' 'fastrand.pcg32bounded(1001)'
10000000 loops, best of 5: 23.6 nsec per loop

python3 -m timeit -s 'import fastrand' 'fastrand.pcg32randint(100,1000)'
10000000 loops, best of 5: 24.6 nsec per loop

python3 -m timeit -s 'import random' 'random.randint(0,1000)'
1000000 loops, best of 5: 216 nsec per loop

python3 -m timeit -s 'import numpy' 'numpy.random.randint(0, 1000)'
500000 loops, best of 5: 955 nsec per loop

The pcg32 generator is a 32-bit generator so it generates values in the interval from 0 to 2**32-1. The xorshift128+ generator is a 64-bit generator so that it can generate values in a 64-bit range (up to 2**64-1).

If you have Linux, macOS or Windows, you should be able to do just pip install...

pip install fastrand

You may need root access (sudo on macOS and Linux).

It is sometimes useful to install a specific version, you can do so as follows;

pip install fastrand==1.2.4

Generally, you can build the library as follows (if you have root):

python setup.py build
python setup.py install 

or

python setup.py build
python setup.py install --home=$HOME
export PYTHONPATH=$PYTHONPATH:~/lib/python

Changing the seed and multiple streams

  • You can change the seed with a function like pcg32_seed. The seed determines the random values you get. Be mindful that naive seeds (e.g., int(time.time())) can deliver poor initial randomness. A few calls to pcg32() may help to boost the improve the randomness. Or else you may try a better seed.
  • If you need to produce multiple streams of random numbers, merely changing the seed is not enough. You are better off using different increments by calling the pcg32inc. The increments should all be distinct. Note that the least significant bit of the increment is always set to 1 no matter which value you pass: so make sure your increments are distinct 31-bit values (ignoring the least significant bit).
  • You may also initialize xorshift128+ with xorshift128plus_seed1 and xorshift128plus_seed2.

Reference

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastrand-2.0.1.tar.gz (8.6 kB view hashes)

Uploaded Source

Built Distributions

fastrand-2.0.1-pp39-pypy39_pp73-win_amd64.whl (13.0 kB view hashes)

Uploaded PyPy Windows x86-64

fastrand-2.0.1-pp39-pypy39_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fastrand-2.0.1-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (11.6 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

fastrand-2.0.1-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (9.2 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

fastrand-2.0.1-pp38-pypy38_pp73-win_amd64.whl (13.0 kB view hashes)

Uploaded PyPy Windows x86-64

fastrand-2.0.1-pp38-pypy38_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fastrand-2.0.1-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (11.6 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

fastrand-2.0.1-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (9.2 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

fastrand-2.0.1-pp37-pypy37_pp73-win_amd64.whl (13.0 kB view hashes)

Uploaded PyPy Windows x86-64

fastrand-2.0.1-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fastrand-2.0.1-pp37-pypy37_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (11.7 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

fastrand-2.0.1-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (9.2 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

fastrand-2.0.1-cp311-cp311-win_amd64.whl (12.9 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

fastrand-2.0.1-cp311-cp311-win32.whl (12.6 kB view hashes)

Uploaded CPython 3.11 Windows x86

fastrand-2.0.1-cp311-cp311-musllinux_1_1_x86_64.whl (24.0 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

fastrand-2.0.1-cp311-cp311-musllinux_1_1_i686.whl (24.6 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

fastrand-2.0.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.7 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fastrand-2.0.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (19.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

fastrand-2.0.1-cp311-cp311-macosx_11_0_arm64.whl (9.9 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

fastrand-2.0.1-cp311-cp311-macosx_10_9_x86_64.whl (9.4 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

fastrand-2.0.1-cp311-cp311-macosx_10_9_universal2.whl (12.0 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

fastrand-2.0.1-cp310-cp310-win_amd64.whl (13.0 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

fastrand-2.0.1-cp310-cp310-win32.whl (12.6 kB view hashes)

Uploaded CPython 3.10 Windows x86

fastrand-2.0.1-cp310-cp310-musllinux_1_1_x86_64.whl (23.2 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

fastrand-2.0.1-cp310-cp310-musllinux_1_1_i686.whl (23.8 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

fastrand-2.0.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fastrand-2.0.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (19.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

fastrand-2.0.1-cp310-cp310-macosx_11_0_arm64.whl (9.9 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

fastrand-2.0.1-cp310-cp310-macosx_10_9_x86_64.whl (9.4 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

fastrand-2.0.1-cp310-cp310-macosx_10_9_universal2.whl (12.0 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

fastrand-2.0.1-cp39-cp39-win_amd64.whl (13.0 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

fastrand-2.0.1-cp39-cp39-win32.whl (12.6 kB view hashes)

Uploaded CPython 3.9 Windows x86

fastrand-2.0.1-cp39-cp39-musllinux_1_1_x86_64.whl (22.9 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

fastrand-2.0.1-cp39-cp39-musllinux_1_1_i686.whl (23.4 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

fastrand-2.0.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fastrand-2.0.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (18.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

fastrand-2.0.1-cp39-cp39-macosx_11_0_arm64.whl (9.9 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

fastrand-2.0.1-cp39-cp39-macosx_10_9_x86_64.whl (9.4 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

fastrand-2.0.1-cp39-cp39-macosx_10_9_universal2.whl (12.0 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

fastrand-2.0.1-cp38-cp38-win_amd64.whl (13.0 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

fastrand-2.0.1-cp38-cp38-win32.whl (12.6 kB view hashes)

Uploaded CPython 3.8 Windows x86

fastrand-2.0.1-cp38-cp38-musllinux_1_1_x86_64.whl (23.1 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

fastrand-2.0.1-cp38-cp38-musllinux_1_1_i686.whl (23.7 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

fastrand-2.0.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fastrand-2.0.1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (19.5 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

fastrand-2.0.1-cp38-cp38-macosx_11_0_arm64.whl (9.9 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

fastrand-2.0.1-cp38-cp38-macosx_10_9_x86_64.whl (9.4 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

fastrand-2.0.1-cp38-cp38-macosx_10_9_universal2.whl (12.0 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

fastrand-2.0.1-cp37-cp37m-win_amd64.whl (13.0 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

fastrand-2.0.1-cp37-cp37m-win32.whl (12.6 kB view hashes)

Uploaded CPython 3.7m Windows x86

fastrand-2.0.1-cp37-cp37m-musllinux_1_1_x86_64.whl (24.0 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

fastrand-2.0.1-cp37-cp37m-musllinux_1_1_i686.whl (24.4 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

fastrand-2.0.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.7 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fastrand-2.0.1-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (19.2 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

fastrand-2.0.1-cp37-cp37m-macosx_10_9_x86_64.whl (9.4 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

fastrand-2.0.1-cp36-cp36m-win_amd64.whl (13.0 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

fastrand-2.0.1-cp36-cp36m-win32.whl (12.7 kB view hashes)

Uploaded CPython 3.6m Windows x86

fastrand-2.0.1-cp36-cp36m-musllinux_1_1_x86_64.whl (22.0 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

fastrand-2.0.1-cp36-cp36m-musllinux_1_1_i686.whl (22.3 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ i686

fastrand-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.8 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fastrand-2.0.1-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (18.0 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

fastrand-2.0.1-cp36-cp36m-macosx_10_9_x86_64.whl (9.1 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

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