GithubHelp home page GithubHelp logo

kristjaneerik / xorshift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ihaque/xorshift

0.0 2.0 0.0 55 KB

Vectorized xorshift and xoroshiro uniform/binomial RNGs

License: MIT License

Python 57.61% C 42.39%

xorshift's Introduction

xorshift: Fast vectorized RNGs for Python

C/Python implementation of Sebastiano Vigna's xoroshiro128+ and xorshift128+ random number generators. These are fast, high quality random bit generators with shorter periods (2^128 - 1) than the usual Mersenne Twister, but also fewer failures on TestU01.

This implementation vectorizes the 64-bit implementations of the xoroshiro128+ and xorshift128+ generators to produce 4 single precision floats per iteration. As currently implemented, they have periods of 2^64, but that is not a fundamental limitation. They're fast: about 5 instructions per uniform float in the inner loop:

(Benchmark results from a 3.1GHz Core i7 13" Retina MacBook Pro, Early 2015)

Benchmarking generation of 131072 Bin(50, 0.25) RVs, 10 iterations
------------------------------------------------------------------
numpy took 15.90 ms/iter,           121.30 ns per float
xoroshiro took 3.88 ms/iter,        29.61 ns per float
xoroshift128plus took 3.46 ms/iter, 26.41 ns per float

Benchmarking generation of 131072 Uniform(0,1) RVs, 10 iterations
-----------------------------------------------------------------
numpy took 1.71 ms/iter,            13.07 ns per float
xoroshiro took 0.11 ms/iter,        0.82 ns per float
xoroshift128plus took 0.10 ms/iter, 0.76 ns per float

Currently, a subset of the numpy.random.RandomState interface is implemented: Xoroshiro or Xorshift128plus generators can be created with a seed (or no seed, seeding from os.urandom(), and they can generate single-precision uniform or binomial variates, of arbitrary shape.

Additionally, the generators can be used in a "no-copy" mode, in which the returned random variates are a window into an internal buffer. This can be useful if you will immediately use the numbers or will directly copy them into another buffer, to avoid an intermediate memory allocation and copy.

API

xorshift.Xoroshiro and xorshift.Xorshift128plus implement the following Generator interface:

Methods

__init__(self, seed=None, copy=True):

Initialize the generator with the given seed. seed may be None or any object defining the __hash__ magic method. If None, the generator will seed itself from os.urandom().

If copy is False, the returned random numbers will be a window into an internally allocated memory buffer; if copy is False, the returned random numbers will be a freshly allocated ndarray. copy=False is faster, but can lead to errors:

rng_nocopy = xorgen.Xoroshiro(copy=False)

nums_1 = rng_nocopy.uniform(size=4)
nums_2 = rng_nocopy.uniform(size=4)
# nums_1 and nums_2 are now windows into the same array, not independent arrays

copy=False can be useful if you will immediately use the results of the RNG or if you will copy them into your own destination:

scatter_array[indices] = rng_nocopy(size=len(indices))

uniform(low=0.0, high=1.0, size=None)

See numpy.random.uniform

binomial(N, p, size=None)

See numpy.random.binomial

xorshift's People

Contributors

ihaque avatar

Watchers

James Cloos avatar (Kristjan) Eerik Kaseniit avatar

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.