Giter Club home page Giter Club logo

fastrand's Introduction

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()

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 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.