Fast random number generation in Python
Project description
fastrand
Fast random number generation 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()
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
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 topcg32()
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).
future work
Also look at https://github.com/rkern/line_profiler
Reference
- http://www.pcg-random.org
- Daniel Lemire, Fast Random Integer Generation in an Interval, ACM Transactions on Modeling and Computer Simulation, Volume 29 Issue 1, February 2019
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
File details
Details for the file fastrand-1.8.0.tar.gz
.
File metadata
- Download URL: fastrand-1.8.0.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f24d880d7bc65853aa1a69223dc065d855fecdecfe3fbcaa2ac90c6a5df27453 |
|
MD5 | 31cf132e267d581d81f78cf0b527a060 |
|
BLAKE2b-256 | 89365c807bac8843ffbdda472c49c62a41eb3a1e174cb6c60cd36d47cc1e6120 |
File details
Details for the file fastrand-1.8.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
.
File metadata
- Download URL: fastrand-1.8.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 18.2 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ i686, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0db5172ca2f05b0cdd901492726be3ff8e492efc93d1381033583e0a8b1efb47 |
|
MD5 | 70dec442fdd9989b51978408f31d1bbc |
|
BLAKE2b-256 | fddde89fa5424884711a3f9f7ab3724cd05e92c942c03636fc989630b61ad901 |
File details
Details for the file fastrand-1.8.0-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: fastrand-1.8.0-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 9.4 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3925b3f6475cbac24e16aef004067e041643d9fc50ae65b1896775b9a5eef486 |
|
MD5 | 6c8a73ef33f97d7fd70f4e0b664bcab0 |
|
BLAKE2b-256 | edd1b85ae1a188e8731052af32c966c715a06c67426937ac266d4686254b3ec0 |
File details
Details for the file fastrand-1.8.0-cp310-cp310-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: fastrand-1.8.0-cp310-cp310-macosx_10_9_x86_64.whl
- Upload date:
- Size: 9.1 kB
- Tags: CPython 3.10, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec9f8f238232feec26958fec935794ea24210d72c32645a1e21439392b2f9719 |
|
MD5 | ac1d7a07aafaae66557d418f38802ad7 |
|
BLAKE2b-256 | 955af9a5a4f91af76d637741a49ad1e9cae40ef98477c88a82ab9132778e3d9c |
File details
Details for the file fastrand-1.8.0-cp310-cp310-macosx_10_9_universal2.whl
.
File metadata
- Download URL: fastrand-1.8.0-cp310-cp310-macosx_10_9_universal2.whl
- Upload date:
- Size: 11.9 kB
- Tags: CPython 3.10, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44f38d081522b09376c5b7f93c7d3be5d299f10dfa6d8b7e96b7c8236509c74e |
|
MD5 | 3d32afbb70ba1ee915f3e35848d6dd46 |
|
BLAKE2b-256 | c66965ee55a2ff6ff1076201f18d62112350bf99f1481f1094cf1b54af88b2a5 |