Skip to main content

Fast random number generation in Python

Project description

fastrand

Fast random number generation in Python using PCG

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 a random 32-bit integer.")
fastrand.pcg32()

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

python -m timeit -s 'import fastrand' 'fastrand.pcg32bounded(1001)'
10000000 loops, best of 3: 0.0602 usec per loop
python -m timeit -s 'import random' 'random.randint(0,1000)'
1000000 loops, best of 3: 0.698 usec per loop
python -m timeit -s 'import numpy' 'numpy.random.randint(0, 1000)'
1000000 loops, best of 3: 0.795 usec 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 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).

future work

Also look at https://github.com/rkern/line_profiler

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

fastrand-1.6.0-py3.7-win-amd64.egg (8.8 kB view details)

Uploaded Source

fastrand-1.6.0-py3.6-win-amd64.egg (8.8 kB view details)

Uploaded Source

fastrand-1.6.0-cp37-cp37m-win_amd64.whl (12.2 kB view details)

Uploaded CPython 3.7m Windows x86-64

fastrand-1.6.0-cp36-cp36m-win_amd64.whl (12.2 kB view details)

Uploaded CPython 3.6m Windows x86-64

File details

Details for the file fastrand-1.6.0-py3.7-win-amd64.egg.

File metadata

File hashes

Hashes for fastrand-1.6.0-py3.7-win-amd64.egg
Algorithm Hash digest
SHA256 0f8d3e22a5bb85a0944f0aa840efdbaa9351dcde9b25232f3c63fa6b0b6937c7
MD5 5bd08ae74a05f99c29bdf043bed10811
BLAKE2b-256 4729cc151a2cbaecd8771056c5d0c2fa99bd327098800bf825db30085097bfba

See more details on using hashes here.

File details

Details for the file fastrand-1.6.0-py3.6-win-amd64.egg.

File metadata

  • Download URL: fastrand-1.6.0-py3.6-win-amd64.egg
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.1 urllib3/1.26.14 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.8

File hashes

Hashes for fastrand-1.6.0-py3.6-win-amd64.egg
Algorithm Hash digest
SHA256 48a0df6ac507922559c37741bda75e6e89270d9928fa26648b96a16a35924fb8
MD5 b349cf16ab79eafc0b651908c5e73a83
BLAKE2b-256 c26bf0585b0e3dd999e2541d8e98018bbed0948d867db1b15ee078342c6d6ece

See more details on using hashes here.

File details

Details for the file fastrand-1.6.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: fastrand-1.6.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.5

File hashes

Hashes for fastrand-1.6.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 de7bd20b099732ad679912179898696514451f97ac89896e4681b067a302b27e
MD5 f855513e64a8430c967a5ea46dd9a1c0
BLAKE2b-256 d973f9eacf2bc61bea3aa979c7abed76ddc63685263415dd0a9c69ac8804dc66

See more details on using hashes here.

File details

Details for the file fastrand-1.6.0-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: fastrand-1.6.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.1 urllib3/1.26.14 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.8

File hashes

Hashes for fastrand-1.6.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 2c826680b37c13b30e433afa9a6c92c83a5c18f58cd92a910520ef71127af948
MD5 ca93adc85fd2b22960df0668c750eab9
BLAKE2b-256 4858200bc35de69e1657805ac0094e2d892d50ff720929b41f34f1b72d6d3638

See more details on using hashes here.

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