├── .coveragerc ├── .gitattributes ├── .gitignore ├── .travis.osx.yml ├── .travis.yml ├── LICENSE.md ├── MANIFEST.in ├── README.md ├── README.rst ├── appveyor.yml ├── ci ├── docker-run.sh ├── docker-wheel-build.sh ├── performance.py └── requirements_dev.txt ├── doc ├── Makefile ├── make.bat └── source │ ├── _static │ └── css │ │ └── overrides.css │ ├── _templates │ └── layout.html │ ├── change-log.rst │ ├── conf.py │ ├── dsfmt.rst │ ├── entropy.rst │ ├── index.rst │ ├── mlfg.rst │ ├── mrg32k3a.rst │ ├── mt19937.rst │ ├── multithreading.rst │ ├── parallel.rst │ ├── pcg32.rst │ ├── pcg64.rst │ ├── performance.rst │ ├── sfmt.rst │ ├── xoroshiro128plus.rst │ ├── xorshift1024.rst │ └── xorshift128.rst ├── github_deploy_key.enc ├── licenses ├── NUMPY-LICENSE.rst ├── PCG-LICENSE.txt └── RANDOMKIT-LICENSE.txt ├── papers ├── 136.pdf ├── 17-MODGEN.pdf ├── IIP1.pdf ├── RICP2000_4.pdf ├── RIJP2000_2.pdf ├── combmrg2.pdf ├── generate_consts.py ├── python_ziggurat.py ├── slides.pdf ├── tr_0503.pdf └── ziggurat.pdf ├── pyproject.toml ├── randomstate ├── __init__.py ├── _deprecated.py ├── _version.py ├── aligned_malloc.c ├── aligned_malloc.h ├── aligned_malloc.pxi ├── array_fillers.pxi.in ├── array_utilities.pxi ├── binomial.pxd ├── bounded_integers.pxi.in ├── compat.py ├── config.pxi ├── defaults.pxi ├── distributions.c ├── distributions.h ├── entropy.pyx ├── interface │ ├── dSFMT │ │ ├── dSFMT-poly.h │ │ ├── dSFMT-shim.c │ │ ├── dSFMT-shim.h │ │ └── dSFMT.pxi │ ├── mlfg-1279-861 │ │ ├── mlfg-1279-861-shim.c │ │ ├── mlfg-1279-861-shim.h │ │ └── mlfg-1279-861.pxi │ ├── mrg32k3a │ │ ├── mrg32k3a-shim.c │ │ ├── mrg32k3a-shim.h │ │ └── mrg32k3a.pxi │ ├── pcg-32 │ │ ├── pcg-32-shim.c │ │ ├── pcg-32-shim.h │ │ └── pcg-32.pxi │ ├── pcg-64 │ │ ├── pcg-64-docstring.pxi │ │ ├── pcg-64-emulated.pxi │ │ ├── pcg-64-shim.c │ │ ├── pcg-64-shim.h │ │ └── pcg-64.pxi │ ├── random-kit │ │ ├── random-kit-poly.h │ │ ├── random-kit-shim.c │ │ ├── random-kit-shim.h │ │ └── random-kit.pxi │ ├── sfmt │ │ ├── sfmt-poly.h │ │ ├── sfmt-shim.c │ │ ├── sfmt-shim.h │ │ └── sfmt.pxi │ ├── xoroshiro128plus │ │ ├── xoroshiro128plus-shim.c │ │ ├── xoroshiro128plus-shim.h │ │ └── xoroshiro128plus.pxi │ ├── xorshift1024 │ │ ├── xorshift1024-shim.c │ │ ├── xorshift1024-shim.h │ │ └── xorshift1024.pxi │ └── xorshift128 │ │ ├── xorshift128-shim.c │ │ ├── xorshift128-shim.h │ │ └── xorshift128.pxi ├── performance.py ├── prng │ ├── __init__.py │ ├── dsfmt │ │ └── __init__.py │ ├── mlfg_1279_861 │ │ └── __init__.py │ ├── mrg32k3a │ │ └── __init__.py │ ├── mt19937 │ │ └── __init__.py │ ├── pcg32 │ │ └── __init__.py │ ├── pcg64 │ │ └── __init__.py │ ├── sfmt │ │ └── __init__.py │ ├── xoroshiro128plus │ │ └── __init__.py │ ├── xorshift1024 │ │ └── __init__.py │ └── xorshift128 │ │ └── __init__.py ├── randomstate.pyx ├── setup-single-rng.py ├── src │ ├── common │ │ ├── binomial.h │ │ ├── binomial.pxi │ │ ├── entropy.c │ │ ├── entropy.h │ │ ├── inttypes.h │ │ └── stdint.h │ ├── dSFMT │ │ ├── 128-bit-jump.poly.txt │ │ ├── 96-bit-jump.poly.txt │ │ ├── LICENSE.txt │ │ ├── calc-jump.cpp │ │ ├── dSFMT-calc-jump.hpp │ │ ├── dSFMT-common.h │ │ ├── dSFMT-jump.c │ │ ├── dSFMT-jump.h │ │ ├── dSFMT-params.h │ │ ├── dSFMT-params19937.h │ │ ├── dSFMT-test-gen.c │ │ ├── dSFMT.c │ │ ├── dSFMT.h │ │ ├── dSFMT_wrapper.pyx │ │ └── setup-dsfmt.py │ ├── mlfg-1279-861 │ │ ├── mlfg-1279-861-test-gen.c │ │ ├── mlfg-1279-861.c │ │ └── mlfg-1279-861.h │ ├── mrg32k3a │ │ ├── mrg32k3a-original.c │ │ ├── mrg32k3a-test-gen.c │ │ ├── mrg32k3a.c │ │ └── mrg32k3a.h │ ├── pcg │ │ ├── pcg-32-test-gen.c │ │ ├── pcg-64-test-gen.c │ │ ├── pcg-advance-128.c │ │ ├── pcg-advance-64.c │ │ ├── pcg-output-128.c │ │ ├── pcg-output-32.c │ │ ├── pcg-output-64.c │ │ ├── pcg-rngs-128.c │ │ ├── pcg-rngs-64.c │ │ ├── pcg32.c │ │ ├── pcg32.h │ │ └── pcg_variants.h │ ├── pcg64-compat │ │ ├── pcg64.c │ │ └── pcg64.h │ ├── random-kit │ │ ├── minipoly_mt19937.c │ │ ├── mt19937_polynomial_coefs_2_128.txt │ │ ├── random-kit-jump.c │ │ ├── random-kit-jump.c.txt │ │ ├── random-kit-jump.h │ │ ├── random-kit-jump.h.txt │ │ ├── random-kit-test-gen.c │ │ ├── random-kit.c │ │ └── random-kit.h │ ├── sfmt │ │ ├── SFMT-calc-jump.hpp │ │ ├── calc-jump.cpp │ │ ├── sfmt-alti.h │ │ ├── sfmt-common.h │ │ ├── sfmt-jump.c │ │ ├── sfmt-jump.h │ │ ├── sfmt-neon.h │ │ ├── sfmt-params.h │ │ ├── sfmt-params19937.h │ │ ├── sfmt-sse2-msc.h │ │ ├── sfmt-sse2.h │ │ ├── sfmt-test-gen.c │ │ ├── sfmt.c │ │ └── sfmt.h │ ├── splitmix64 │ │ ├── splitmix64-original.c │ │ ├── splitmix64.c │ │ └── splitmix64.h │ ├── xoroshiro128plus │ │ ├── xoroshiro128plus-original.c │ │ ├── xoroshiro128plus-test.gen.c │ │ ├── xoroshiro128plus.c │ │ └── xoroshiro128plus.h │ ├── xorshift1024 │ │ ├── xorshift1024-original.c │ │ ├── xorshift1024-test-gen.c │ │ ├── xorshift1024.c │ │ └── xorshift1024.h │ └── xorshift128 │ │ ├── xorshift128-original.c │ │ ├── xorshift128-test.gen.c │ │ ├── xorshift128.c │ │ └── xorshift128.h ├── tests │ ├── __init__.py │ ├── data │ │ ├── __init__.py │ │ ├── dSFMT-testset-1.csv │ │ ├── dSFMT-testset-2.csv │ │ ├── mlfg-testset-1.csv │ │ ├── mlfg-testset-2.csv │ │ ├── mrg32k3a-testset-1.csv │ │ ├── mrg32k3a-testset-2.csv │ │ ├── pcg32-testset-1.csv │ │ ├── pcg32-testset-2.csv │ │ ├── pcg64-testset-1.csv │ │ ├── pcg64-testset-2.csv │ │ ├── randomkit-testset-1.csv │ │ ├── randomkit-testset-2.csv │ │ ├── sfmt-testset-1.csv │ │ ├── sfmt-testset-2.csv │ │ ├── xoroshiro128plus-testset-1.csv │ │ ├── xoroshiro128plus-testset-2.csv │ │ ├── xorshift1024-testset-1.csv │ │ ├── xorshift1024-testset-2.csv │ │ ├── xorshift128-testset-1.csv │ │ └── xorshift128-testset-2.csv │ ├── test_against_numpy.py │ ├── test_direct.py │ ├── test_numpy_mt19937.py │ ├── test_numpy_mt19937_regressions.py │ └── test_smoke.py ├── ziggurat.h └── ziggurat_constants.h ├── setup.cfg ├── setup.py └── versioneer.py /.coveragerc: -------------------------------------------------------------------------------- 1 | # .coveragerc to control coverage.py 2 | [run] 3 | source = randomstate 4 | branch = True 5 | include = */randomstate/* 6 | omit = 7 | */_version.py 8 | plugins = Cython.Coverage 9 | 10 | [report] 11 | # Regexes for lines to exclude from consideration 12 | exclude_lines = 13 | # Have to re-enable the standard pragma 14 | pragma: no cover 15 | 16 | # Don't complain if tests don't hit defensive assertion code: 17 | raise NotImplementedError 18 | except NotImplementedError 19 | except AssertionError 20 | # Ignore pass 21 | pass 22 | include = */randomstate/* 23 | omit = 24 | */_version.py 25 | ignore_errors = True 26 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | randomstate/_version.py export-subst 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.xml 2 | *.so 3 | pcg.c 4 | pcg-c-0.94 5 | *.o 6 | pcg/core-rng 7 | build 8 | dist 9 | *.egg-info 10 | .idea/ 11 | 12 | -------------------------------------------------------------------------------- /.travis.osx.yml: -------------------------------------------------------------------------------- 1 | # Travis script that uses miniconda in place of the system installed python 2 | # versions. Allows substantial flexability for choosing versions of 3 | # required packages and is simpler to use to test up-to-date scientific Python 4 | # stack 5 | sudo: false 6 | 7 | language: objective-c 8 | 9 | matrix: 10 | fast_finish: true 11 | include: 12 | - env: PYTHON=2.7 13 | - env: PYTHON=3.5 14 | 15 | before_install: 16 | - which python 17 | - if [ ${TRAVIS_OS_NAME} = "osx" ]; then wget https://repo.continuum.io/miniconda/Miniconda-latest-MacOSX-x86_64.sh -O miniconda.sh; fi 18 | - chmod +x miniconda.sh 19 | - ./miniconda.sh -b 20 | - export PATH=$HOME/miniconda/bin:$PATH 21 | - export PATH=$HOME/miniconda2/bin:$PATH 22 | - conda config --set always_yes yes 23 | - conda update --all --quiet 24 | - PKGS="python=${PYTHON}" 25 | - PKGS="${PKGS} numpy"; if [ ${NUMPY} ]; then PKGS="${PKGS}=${NUMPY}"; fi 26 | - PKGS="${PKGS} Cython"; if [ ${CYTHON} ]; then PKGS="${PKGS}=${CYTHON}"; fi 27 | - PKGS="${PKGS} pandas"; if [ ${PANDAS} ]; then PKGS="${PKGS}=${PANDAS}"; fi 28 | 29 | # Install packages 30 | install: 31 | - conda create -n rng-test ${PKGS} nose pip setuptools --quiet 32 | - source activate rng-test 33 | - export BUILD_DIR=$PWD 34 | - python setup.py install 35 | 36 | script: 37 | - cd $HOME 38 | - nosetests randomstate 39 | - cd $BUILD_DIR/randomstate 40 | - if [ ${PYTHON} = "2.7" ]; then python performance.py; fi 41 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Travis script that uses miniconda in place of the system installed python 2 | # versions. Allows substantial flexability for choosing versions of 3 | # required packages and is simpler to use to test up-to-date scientific Python 4 | # stack 5 | group: edge 6 | dist: trusty 7 | sudo: required 8 | language: python 9 | 10 | env: 11 | global: 12 | - secure: "aupv9PPJZh7WjV2fl8fnxvztT5HHPz0j3pPkaMrxWT5fhWhbL/RYSqkkCh+GZiYFnDToXEdiNLuj2UD3CsRNu1ry9RuI9f9KcBgL9udacu2XC00c8XMl1Puiy02RjHe17DAHmAyoTEcckDoHyoKHM6rfyYCjGEDoqJuW6VfJr0KdNUE9bZSdj+kDQgQT0cilaHywzLnHg0v7K83vp2MG0BELV4vcB/OU/BmFeekjqNWaYHDHP0Ur+WG7FoOpGyG3dqO+Hlutx8tXlwjVHgTjkd6vJOU/3AHjWFfrNd9Z6Pzi2afCBZjLXHOeHm8X4Be4Gd9EJnWpARRev+6bgXVCyVZsaRscXPsdl0ylmYFROiUqIlvnqjPRDTRhq4kA72Fvtsf6RzTHX0CdDyctVMw3RoasrA9yRRg7SQYuTn1yKCpEpSGeDWjHo1/PqyBTvGdAwTse9jitBxyw3lC3dXAQDpQo5Smk5ZFwBS+9T0tm8QR1P63Nv9TeK9yjsJe/cytb5ZP495FKBJ1iBntjhOfhoahKbcFcVZjyZZYQZ5f1pVcZoKI4iTuBZup060ZmDRXjwStR2N63za9DocY6oVIyHaLcrgiej8KqvBeqWCxNvMNdiw/4O47/4T6qsvsjdceSUBkR8yPyuL3NfkZXUzzxdWwkNKHynxYYLprKZqHqfUU=" 13 | 14 | matrix: 15 | fast_finish: true 16 | include: 17 | - env: 18 | - PYTHON=2.7 19 | - NUMPY=1.9 20 | - CYTHON=0.25 21 | - env: 22 | - PYTHON=3.4 23 | - NUMPY=1.10 24 | - CYTHON=0.25 25 | - env: 26 | - PYTHON=3.5 27 | - CYTHON=0.26 28 | - env: 29 | - PYTHON=3.6 30 | 31 | before_install: 32 | - if [ ${TRAVIS_OS_NAME} = "osx" ]; then wget https://repo.continuum.io/miniconda/Miniconda-latest-MacOSX-x86_64.sh -O miniconda.sh; fi 33 | - if [ ${TRAVIS_OS_NAME} = "linux" ]; then wget http://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda3.sh; fi 34 | - chmod +x miniconda3.sh 35 | - ./miniconda3.sh -b 36 | - export PATH=/home/travis/miniconda3/bin:$PATH 37 | - conda config --set always_yes true 38 | # Disable until fixed 39 | # - conda update --all --quiet 40 | - PKGS="python=${PYTHON}" 41 | - PKGS="${PKGS} numpy"; if [ ${NUMPY} ]; then PKGS="${PKGS}=${NUMPY}"; fi 42 | - PKGS="${PKGS} Cython"; if [ ${CYTHON} ]; then PKGS="${PKGS}=${CYTHON}"; fi 43 | - PKGS="${PKGS} pandas"; if [ ${PANDAS} ]; then PKGS="${PKGS}=${PANDAS}"; fi 44 | - export BUILD_DIR=$PWD 45 | - conda create -n randomstate-test ${PKGS} pytest pip setuptools matplotlib pyyaml nose --quiet 46 | - source activate randomstate-test 47 | - pip install tempita sphinx guzzle_sphinx_theme ipython coverage coveralls pytest-cov codecov -q 48 | - export CYTHON_COVERAGE=1 49 | 50 | install: 51 | - python setup.py develop 52 | 53 | script: 54 | - set -e 55 | - pytest --cov-config .coveragerc --cov=randomstate randomstate --cov-report xml --cov-report term 56 | - | 57 | if [ ${PYTHON} = 3.5 ]; then 58 | conda install -c conda-forge doctr 59 | cd ${BUILD_DIR}/doc 60 | make html 61 | cd ${BUILD_DIR} 62 | doctr deploy doc 63 | if [[ ${TRAVIS_TAG}} ]]; then 64 | doctr deploy doc --no-require-master 65 | fi; 66 | fi; 67 | - | 68 | if [ ${PYTHON} = "3.6" ]; then 69 | cd ${BUILD_DIR}/randomstate 70 | python performance.py; 71 | fi 72 | 73 | after_success: 74 | codecov 75 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # License 2 | 3 | Copyright (c) 2016 Kevin Sheppard 4 | All rights reserved. 5 | 6 | Developed by: Kevin Sheppard 7 | https://github.com/bashtage 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a 10 | copy of this software and associated documentation files (the "Software"), to 11 | deal with the Software without restriction, including without limitation the 12 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 13 | sell copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | Redistributions of source code must retain the above copyright notice, 17 | this list of conditions and the following disclaimers. 18 | 19 | Redistributions in binary form must reproduce the above copyright notice, 20 | this list of conditions and the following disclaimers in the documentation 21 | and/or other materials provided with the distribution. 22 | Neither the names of Kevin Sheppard, 23 | nor the names of its contributors may be used to endorse or promote 24 | products derived from this Software without specific prior written 25 | permission. 26 | 27 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 30 | CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 31 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 32 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH 33 | THE SOFTWARE. -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | recursive-include randomstate *.c *.h *.pxi *.pxd *.pyx *.in 2 | recursive-include randomstate/tests *.csv 3 | include *.md 4 | exclude randomstate/entropy.c 5 | exclude randomstate/dsfmt* 6 | exclude randomstate/mt19937* 7 | exclude randomstate/mlfg_1279_861* 8 | exclude randomstate/pcg32* 9 | exclude randomstate/pcg64* 10 | exclude randomstate/xorshift128* 11 | exclude randomstate/xorshift1024* 12 | 13 | include versioneer.py 14 | include randomstate/_version.py 15 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | skip_tags: true 2 | clone_depth: 50 3 | 4 | os: Visual Studio 2015 5 | 6 | environment: 7 | matrix: 8 | - PY_MAJOR_VER: 2 9 | PYTHON_ARCH: "x86" 10 | - PY_MAJOR_VER: 3 11 | PYTHON_ARCH: "x86_64" 12 | - PY_MAJOR_VER: 3 13 | PYTHON_ARCH: "x86" 14 | 15 | platform: 16 | - x64 17 | 18 | build_script: 19 | - ps: Start-FileDownload "https://repo.continuum.io/miniconda/Miniconda$env:PY_MAJOR_VER-latest-Windows-$env:PYTHON_ARCH.exe" C:\Miniconda.exe; echo "Finished downloading miniconda" 20 | - cmd: C:\Miniconda.exe /S /D=C:\Py 21 | - SET PATH=C:\Py;C:\Py\Scripts;C:\Py\Library\bin;%PATH% 22 | - conda config --set always_yes yes 23 | - conda update conda --quiet 24 | - conda install numpy cython nose pandas pytest --quiet 25 | - python setup.py develop 26 | - set "GIT_DIR=%cd%" 27 | 28 | test_script: 29 | - pytest randomstate 30 | 31 | on_success: 32 | - cd %GIT_DIR%\randomstate 33 | - IF %PYTHON_ARCH%==x86_64 python performance.py -------------------------------------------------------------------------------- /ci/docker-run.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Usage: 3 | # 4 | # ci/docker-run.sh 5 | # 6 | # Must be started from repo root 7 | 8 | rm -rf wheelhouse 9 | mkdir wheelhouse 10 | 11 | export DOCKER_IMAGE=quay.io/pypa/manylinux1_x86_64 12 | sudo docker pull $DOCKER_IMAGE 13 | sudo docker run --rm -v `pwd`:/io $DOCKER_IMAGE /io/ci/docker-wheel-build.sh 14 | -------------------------------------------------------------------------------- /ci/docker-wheel-build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e -x 4 | 5 | export SUPPORTED_PYTHONS=(cp27-cp27m cp35-cp35m cp36-cp36m) 6 | 7 | for PYVER in ${SUPPORTED_PYTHONS[@]}; do 8 | echo ${PYVER} 9 | PYBIN=/opt/python/${PYVER}/bin 10 | "${PYBIN}/pip" install -r /io/ci/requirements_dev.txt 11 | "${PYBIN}/pip" wheel /io/ --no-deps -w wheelhouse/ 12 | done 13 | 14 | for whl in wheelhouse/*.whl; do 15 | auditwheel repair $whl -w /io/wheelhouse/ 16 | done 17 | 18 | cd $HOME 19 | for PYVER in ${SUPPORTED_PYTHONS[@]}; do 20 | echo ${PYVER} 21 | PYBIN=/opt/python/${PYVER}/bin 22 | ${PYBIN}/pip install randomstate --no-index -f /io/wheelhouse 23 | ${PYBIN}/pytest --pyargs randomstate 24 | done 25 | -------------------------------------------------------------------------------- /ci/requirements_dev.txt: -------------------------------------------------------------------------------- 1 | numpy>=1.13 2 | cython>=0.26 3 | pytest 4 | nose 5 | -------------------------------------------------------------------------------- /doc/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = python -msphinx 7 | SPHINXPROJ = randomstate 8 | SOURCEDIR = source 9 | BUILDDIR = build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile 16 | 17 | # Catch-all target: route all unknown targets to Sphinx using the new 18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 19 | %: Makefile 20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 21 | -------------------------------------------------------------------------------- /doc/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | pushd %~dp0 4 | 5 | REM Command file for Sphinx documentation 6 | 7 | if "%SPHINXBUILD%" == "" ( 8 | set SPHINXBUILD=python -msphinx 9 | ) 10 | set SOURCEDIR=source 11 | set BUILDDIR=build 12 | set SPHINXPROJ=randomstate 13 | 14 | if "%1" == "" goto help 15 | 16 | %SPHINXBUILD% >NUL 2>NUL 17 | if errorlevel 9009 ( 18 | echo. 19 | echo.The Sphinx module was not found. Make sure you have Sphinx installed, 20 | echo.then set the SPHINXBUILD environment variable to point to the full 21 | echo.path of the 'sphinx-build' executable. Alternatively you may add the 22 | echo.Sphinx directory to PATH. 23 | echo. 24 | echo.If you don't have Sphinx installed, grab it from 25 | echo.http://sphinx-doc.org/ 26 | exit /b 1 27 | ) 28 | 29 | %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 30 | goto end 31 | 32 | :help 33 | %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 34 | 35 | :end 36 | popd 37 | -------------------------------------------------------------------------------- /doc/source/_static/css/overrides.css: -------------------------------------------------------------------------------- 1 | a { 2 | color: #180CD2; 3 | } 4 | table a:not(.btn), 5 | .table a:not(.btn) { 6 | text-decoration: none; 7 | } 8 | body { 9 | font-size: 16px; 10 | } 11 | code { 12 | font-size: 100%; 13 | } 14 | table { 15 | font-size: 16px; 16 | } 17 | .navbar { 18 | font-size: 16px; 19 | } 20 | .navbar .dropdown-menu > li > a, 21 | .navbar .dropdown-menu > li > a:focus { 22 | font-size: 15px; 23 | } 24 | thead tr { 25 | background-color: #337ab7; 26 | color: #ffffff; 27 | } 28 | thead tr th { 29 | padding: 6px 3px; 30 | } 31 | tbody tr:nth-child(even) { 32 | background: #f0f0f0 33 | } 34 | tbody tr:hover td { 35 | background-color: #ddd; 36 | } 37 | tbody tr td { 38 | text-align: right; 39 | padding: 5px; 40 | } 41 | tbody tr td:first-child { 42 | text-align: left; 43 | padding-left: 5px; 44 | } -------------------------------------------------------------------------------- /doc/source/_templates/layout.html: -------------------------------------------------------------------------------- 1 | {# Import the theme's layout. #} 2 | {% extends "!layout.html" %} 3 | 4 | {%- block extrahead %} 5 | {{ super() }} 6 | 7 | {% endblock %} 8 | -------------------------------------------------------------------------------- /doc/source/dsfmt.rst: -------------------------------------------------------------------------------- 1 | dSFMT Randomstate 2 | ***************** 3 | 4 | .. currentmodule:: randomstate.prng.dsfmt 5 | 6 | Random generator 7 | ================ 8 | .. autoclass:: 9 | RandomState 10 | 11 | .. autosummary:: 12 | :toctree: generated/ 13 | 14 | seed 15 | get_state 16 | set_state 17 | 18 | Parallel generation 19 | =================== 20 | .. autosummary:: 21 | :toctree: generated/ 22 | 23 | jump 24 | 25 | Simple random data 26 | ================== 27 | .. autosummary:: 28 | :toctree: generated/ 29 | 30 | rand 31 | randn 32 | randint 33 | random_integers 34 | random_sample 35 | random 36 | ranf 37 | sample 38 | choice 39 | bytes 40 | random_uintegers 41 | random_raw 42 | 43 | Permutations 44 | ============ 45 | .. autosummary:: 46 | :toctree: generated/ 47 | 48 | shuffle 49 | permutation 50 | 51 | Distributions 52 | ============= 53 | .. autosummary:: 54 | :toctree: generated/ 55 | 56 | beta 57 | binomial 58 | chisquare 59 | complex_normal 60 | dirichlet 61 | exponential 62 | f 63 | gamma 64 | geometric 65 | gumbel 66 | hypergeometric 67 | laplace 68 | logistic 69 | lognormal 70 | logseries 71 | multinomial 72 | multivariate_normal 73 | negative_binomial 74 | noncentral_chisquare 75 | noncentral_f 76 | normal 77 | pareto 78 | poisson 79 | power 80 | rayleigh 81 | standard_cauchy 82 | standard_exponential 83 | standard_gamma 84 | standard_normal 85 | standard_t 86 | triangular 87 | uniform 88 | vonmises 89 | wald 90 | weibull 91 | zipf 92 | -------------------------------------------------------------------------------- /doc/source/entropy.rst: -------------------------------------------------------------------------------- 1 | System Entropy 2 | ============== 3 | 4 | .. module:: randomstate.entropy 5 | 6 | .. autofunction:: random_entropy 7 | -------------------------------------------------------------------------------- /doc/source/mlfg.rst: -------------------------------------------------------------------------------- 1 | MLFG(1279, 861, \*) Randomstate 2 | ******************************** 3 | 4 | .. currentmodule:: randomstate.prng.mlfg_1279_861 5 | 6 | Random generator 7 | ================ 8 | .. autoclass:: 9 | RandomState 10 | 11 | .. autosummary:: 12 | :toctree: generated/ 13 | 14 | seed 15 | get_state 16 | set_state 17 | 18 | 19 | Simple random data 20 | ================== 21 | .. autosummary:: 22 | :toctree: generated/ 23 | 24 | rand 25 | randn 26 | randint 27 | random_integers 28 | random_sample 29 | random 30 | ranf 31 | sample 32 | choice 33 | bytes 34 | random_uintegers 35 | random_raw 36 | 37 | Permutations 38 | ============ 39 | .. autosummary:: 40 | :toctree: generated/ 41 | 42 | shuffle 43 | permutation 44 | 45 | Distributions 46 | ============= 47 | .. autosummary:: 48 | :toctree: generated/ 49 | 50 | beta 51 | binomial 52 | chisquare 53 | complex_normal 54 | dirichlet 55 | exponential 56 | f 57 | gamma 58 | geometric 59 | gumbel 60 | hypergeometric 61 | laplace 62 | logistic 63 | lognormal 64 | logseries 65 | multinomial 66 | multivariate_normal 67 | negative_binomial 68 | noncentral_chisquare 69 | noncentral_f 70 | normal 71 | pareto 72 | poisson 73 | power 74 | rayleigh 75 | standard_cauchy 76 | standard_exponential 77 | standard_gamma 78 | standard_normal 79 | standard_t 80 | triangular 81 | uniform 82 | vonmises 83 | wald 84 | weibull 85 | zipf 86 | -------------------------------------------------------------------------------- /doc/source/mrg32k3a.rst: -------------------------------------------------------------------------------- 1 | MRG32K3A Randomstate 2 | ******************** 3 | 4 | .. currentmodule:: randomstate.prng.mrg32k3a 5 | 6 | 7 | Random generator 8 | ================ 9 | .. autoclass:: 10 | RandomState 11 | 12 | .. autosummary:: 13 | :toctree: generated/ 14 | 15 | seed 16 | get_state 17 | set_state 18 | 19 | Parallel generation 20 | =================== 21 | .. autosummary:: 22 | :toctree: generated/ 23 | 24 | jump 25 | 26 | Simple random data 27 | ================== 28 | .. autosummary:: 29 | :toctree: generated/ 30 | 31 | rand 32 | randn 33 | randint 34 | random_integers 35 | random_sample 36 | random 37 | ranf 38 | sample 39 | choice 40 | bytes 41 | random_uintegers 42 | random_raw 43 | 44 | Permutations 45 | ============ 46 | .. autosummary:: 47 | :toctree: generated/ 48 | 49 | shuffle 50 | permutation 51 | 52 | Distributions 53 | ============= 54 | .. autosummary:: 55 | :toctree: generated/ 56 | 57 | beta 58 | binomial 59 | chisquare 60 | complex_normal 61 | dirichlet 62 | exponential 63 | f 64 | gamma 65 | geometric 66 | gumbel 67 | hypergeometric 68 | laplace 69 | logistic 70 | lognormal 71 | logseries 72 | multinomial 73 | multivariate_normal 74 | negative_binomial 75 | noncentral_chisquare 76 | noncentral_f 77 | normal 78 | pareto 79 | poisson 80 | power 81 | rayleigh 82 | standard_cauchy 83 | standard_exponential 84 | standard_gamma 85 | standard_normal 86 | standard_t 87 | triangular 88 | uniform 89 | vonmises 90 | wald 91 | weibull 92 | zipf 93 | -------------------------------------------------------------------------------- /doc/source/mt19937.rst: -------------------------------------------------------------------------------- 1 | MT19937 Randomstate 2 | ******************* 3 | 4 | .. currentmodule:: randomstate.prng.mt19937 5 | 6 | 7 | Random generator 8 | ================ 9 | .. autoclass:: 10 | RandomState 11 | 12 | .. autosummary:: 13 | :toctree: generated/ 14 | 15 | seed 16 | get_state 17 | set_state 18 | 19 | Parallel generation 20 | =================== 21 | .. autosummary:: 22 | :toctree: generated/ 23 | 24 | jump 25 | 26 | Simple random data 27 | ================== 28 | .. autosummary:: 29 | :toctree: generated/ 30 | 31 | rand 32 | randn 33 | randint 34 | random_integers 35 | random_sample 36 | random 37 | ranf 38 | sample 39 | choice 40 | bytes 41 | random_uintegers 42 | random_raw 43 | 44 | Permutations 45 | ============ 46 | .. autosummary:: 47 | :toctree: generated/ 48 | 49 | shuffle 50 | permutation 51 | 52 | Distributions 53 | ============= 54 | .. autosummary:: 55 | :toctree: generated/ 56 | 57 | beta 58 | binomial 59 | chisquare 60 | complex_normal 61 | dirichlet 62 | exponential 63 | f 64 | gamma 65 | geometric 66 | gumbel 67 | hypergeometric 68 | laplace 69 | logistic 70 | lognormal 71 | logseries 72 | multinomial 73 | multivariate_normal 74 | negative_binomial 75 | noncentral_chisquare 76 | noncentral_f 77 | normal 78 | pareto 79 | poisson 80 | power 81 | rayleigh 82 | standard_cauchy 83 | standard_exponential 84 | standard_gamma 85 | standard_normal 86 | standard_t 87 | triangular 88 | uniform 89 | vonmises 90 | wald 91 | weibull 92 | zipf 93 | -------------------------------------------------------------------------------- /doc/source/pcg32.rst: -------------------------------------------------------------------------------- 1 | 32-bit PCG Randomstate 2 | ********************** 3 | 4 | .. currentmodule:: randomstate.prng.pcg32 5 | 6 | 7 | Random generator 8 | ================ 9 | .. autoclass:: 10 | RandomState 11 | 12 | .. autosummary:: 13 | :toctree: generated/ 14 | 15 | seed 16 | get_state 17 | set_state 18 | 19 | Parallel generation 20 | =================== 21 | .. autosummary:: 22 | :toctree: generated/ 23 | 24 | advance 25 | 26 | Simple random data 27 | ================== 28 | .. autosummary:: 29 | :toctree: generated/ 30 | 31 | rand 32 | randn 33 | randint 34 | random_integers 35 | random_sample 36 | random 37 | ranf 38 | sample 39 | choice 40 | bytes 41 | random_uintegers 42 | random_raw 43 | 44 | Permutations 45 | ============ 46 | .. autosummary:: 47 | :toctree: generated/ 48 | 49 | shuffle 50 | permutation 51 | 52 | Distributions 53 | ============= 54 | .. autosummary:: 55 | :toctree: generated/ 56 | 57 | beta 58 | binomial 59 | chisquare 60 | complex_normal 61 | dirichlet 62 | exponential 63 | f 64 | gamma 65 | geometric 66 | gumbel 67 | hypergeometric 68 | laplace 69 | logistic 70 | lognormal 71 | logseries 72 | multinomial 73 | multivariate_normal 74 | negative_binomial 75 | noncentral_chisquare 76 | noncentral_f 77 | normal 78 | pareto 79 | poisson 80 | power 81 | rayleigh 82 | standard_cauchy 83 | standard_exponential 84 | standard_gamma 85 | standard_normal 86 | standard_t 87 | triangular 88 | uniform 89 | vonmises 90 | wald 91 | weibull 92 | zipf 93 | -------------------------------------------------------------------------------- /doc/source/pcg64.rst: -------------------------------------------------------------------------------- 1 | 64-bit PCG Randomstate 2 | ********************** 3 | 4 | .. currentmodule:: randomstate.prng.pcg64 5 | 6 | 7 | Random generator 8 | ================ 9 | .. autoclass:: 10 | RandomState 11 | 12 | .. autosummary:: 13 | :toctree: generated/ 14 | 15 | seed 16 | get_state 17 | set_state 18 | 19 | Parallel generation 20 | =================== 21 | .. autosummary:: 22 | :toctree: generated/ 23 | 24 | advance 25 | 26 | Simple random data 27 | ================== 28 | .. autosummary:: 29 | :toctree: generated/ 30 | 31 | rand 32 | randn 33 | randint 34 | random_integers 35 | random_sample 36 | random 37 | ranf 38 | sample 39 | choice 40 | bytes 41 | random_uintegers 42 | random_raw 43 | 44 | Permutations 45 | ============ 46 | .. autosummary:: 47 | :toctree: generated/ 48 | 49 | shuffle 50 | permutation 51 | 52 | Distributions 53 | ============= 54 | .. autosummary:: 55 | :toctree: generated/ 56 | 57 | beta 58 | binomial 59 | chisquare 60 | complex_normal 61 | dirichlet 62 | exponential 63 | f 64 | gamma 65 | geometric 66 | gumbel 67 | hypergeometric 68 | laplace 69 | logistic 70 | lognormal 71 | logseries 72 | multinomial 73 | multivariate_normal 74 | negative_binomial 75 | noncentral_chisquare 76 | noncentral_f 77 | normal 78 | pareto 79 | poisson 80 | power 81 | rayleigh 82 | standard_cauchy 83 | standard_exponential 84 | standard_gamma 85 | standard_normal 86 | standard_t 87 | triangular 88 | uniform 89 | vonmises 90 | wald 91 | weibull 92 | zipf 93 | -------------------------------------------------------------------------------- /doc/source/performance.rst: -------------------------------------------------------------------------------- 1 | Performance 2 | ----------- 3 | 4 | .. py:module:: randomstate 5 | 6 | Recommendation 7 | ************** 8 | The recommended generator for single use is `xoroshiro128+` 9 | (:class:`~randomstate.prng.xoroshiro128plus`). The recommended generator 10 | for use in large-scale parallel applications is 11 | `xorshift1024*` (:class:`~randomstate.prng.xorshift1024`) 12 | where the `jump` method is used to advance the state. 13 | 14 | Timings 15 | ******* 16 | 17 | The timings below are ns/random value. The fastest generator is the 18 | raw generator (`random_raw`) which does not make any transformation 19 | to the underlying random value. `xoroshiro128+` is the fastest, followed by 20 | `xorshift1024*` and the two SIMD aware MT generators. The original MT19937 21 | generator is much slower since it requires 2 32-bit values to equal the output 22 | of the faster generators. 23 | 24 | Integer performance has a similar ordering although `dSFMT` is slower since 25 | it generates 53-bit floating point values rather than integer values. On the 26 | other hand, it is very fast for uniforms, although slower than `xoroshiro128+`. 27 | 28 | The patterm is similar for other, more complex generators. The normal 29 | performance of NumPy's MT19937 is much lower than the other since it 30 | uses the Box-Muller transformation rather than the Ziggurat generator. 31 | 32 | .. csv-table:: 33 | :header: ,NumPy MT19937,MT19937,SFMT,dSFMT,xoroshiro128+,xorshift1024,PCG64 34 | :widths: 14,14,14,14,14,14,14,14 35 | 36 | Raw,,4.21,1.81,1.7,1.06,1.5,2.67 37 | Random Integers,4.56,3.11,2.09,2.96,1.93,2.34,2.22 38 | Uniforms,9.77,6.13,2.41,2.22,1.46,2.37,2.56 39 | Normal,62.47,13.77,9.11,10.89,7.8,10.48,10.67 40 | Exponential,98.35,9.85,5.72,7.22,5.56,6.32,10.88 41 | Complex Normal,,37.07,24.41,27.88,23.31,27.78,28.34 42 | Gamma,97.99,44.94,38.27,33.41,31.2,34.18,34.09 43 | Binomial,87.99,79.95,78.85,77.12,76.88,76.09,76.99 44 | Laplace,101.73,103.95,91.57,89.02,91.94,93.61,92.13 45 | Poisson,131.93,119.95,99.42,94.84,92.71,100.28,101.17 46 | Neg. Binomial,433.77,416.69,410.2,397.71,389.21,396.14,394.78 47 | Multinomial,1072.82,1043.98,1021.58,1019.22,1016.7,1013.15,1018.41 48 | 49 | 50 | The next table presents the performance relative to `xoroshiro128+`. The overall 51 | performance was computed using a geometric mean. 52 | 53 | .. csv-table:: 54 | :header: ,NumPy MT19937,MT19937,SFMT,dSFMT,xoroshiro128+,xorshift1024,PCG64 55 | :widths: 14,14,14,14,14,14,14,14 56 | 57 | Raw,,3.97,1.71,1.6,1.0,1.42,2.52 58 | Random Integers,2.36,1.61,1.08,1.53,1.0,1.21,1.15 59 | Uniforms,6.69,4.2,1.65,1.52,1.0,1.62,1.75 60 | Normal,8.01,1.77,1.17,1.4,1.0,1.34,1.37 61 | Exponential,17.69,1.77,1.03,1.3,1.0,1.14,1.96 62 | Complex Normal,,1.59,1.05,1.2,1.0,1.19,1.22 63 | Gamma,3.14,1.44,1.23,1.07,1.0,1.1,1.09 64 | Binomial,1.14,1.04,1.03,1.0,1.0,0.99,1.0 65 | Laplace,1.11,1.13,1.0,0.97,1.0,1.02,1.0 66 | Poisson,1.42,1.29,1.07,1.02,1.0,1.08,1.09 67 | Neg. Binomial,1.11,1.07,1.05,1.02,1.0,1.02,1.01 68 | Multinomial,1.06,1.03,1.0,1.0,1.0,1.0,1.0 69 | Overall,2.61,1.62,1.15,1.2,1.0,1.16,1.28 70 | 71 | 72 | .. note:: 73 | 74 | All timings were taken using Linux and gcc 5.4 on a i5-3570 processor. 75 | -------------------------------------------------------------------------------- /doc/source/sfmt.rst: -------------------------------------------------------------------------------- 1 | SFMT Randomstate 2 | **************** 3 | 4 | .. currentmodule:: randomstate.prng.sfmt 5 | 6 | Random generator 7 | ================ 8 | .. autoclass:: 9 | RandomState 10 | 11 | .. autosummary:: 12 | :toctree: generated/ 13 | 14 | seed 15 | get_state 16 | set_state 17 | 18 | Parallel generation 19 | =================== 20 | .. autosummary:: 21 | :toctree: generated/ 22 | 23 | jump 24 | 25 | Simple random data 26 | ================== 27 | .. autosummary:: 28 | :toctree: generated/ 29 | 30 | rand 31 | randn 32 | randint 33 | random_integers 34 | random_sample 35 | random 36 | ranf 37 | sample 38 | choice 39 | bytes 40 | random_uintegers 41 | random_raw 42 | 43 | Permutations 44 | ============ 45 | .. autosummary:: 46 | :toctree: generated/ 47 | 48 | shuffle 49 | permutation 50 | 51 | Distributions 52 | ============= 53 | .. autosummary:: 54 | :toctree: generated/ 55 | 56 | beta 57 | binomial 58 | chisquare 59 | complex_normal 60 | dirichlet 61 | exponential 62 | f 63 | gamma 64 | geometric 65 | gumbel 66 | hypergeometric 67 | laplace 68 | logistic 69 | lognormal 70 | logseries 71 | multinomial 72 | multivariate_normal 73 | negative_binomial 74 | noncentral_chisquare 75 | noncentral_f 76 | normal 77 | pareto 78 | poisson 79 | power 80 | rayleigh 81 | standard_cauchy 82 | standard_exponential 83 | standard_gamma 84 | standard_normal 85 | standard_t 86 | triangular 87 | uniform 88 | vonmises 89 | wald 90 | weibull 91 | zipf 92 | -------------------------------------------------------------------------------- /doc/source/xoroshiro128plus.rst: -------------------------------------------------------------------------------- 1 | XoroShiro128+ Randomstate 2 | ************************* 3 | 4 | .. currentmodule:: randomstate.prng.xoroshiro128plus 5 | 6 | 7 | Random generator 8 | ================ 9 | .. autoclass:: 10 | RandomState 11 | 12 | .. autosummary:: 13 | :toctree: generated/ 14 | 15 | seed 16 | get_state 17 | set_state 18 | 19 | Parallel generation 20 | =================== 21 | .. autosummary:: 22 | :toctree: generated/ 23 | 24 | jump 25 | 26 | Simple random data 27 | ================== 28 | .. autosummary:: 29 | :toctree: generated/ 30 | 31 | rand 32 | randn 33 | randint 34 | random_integers 35 | random_sample 36 | random 37 | ranf 38 | sample 39 | choice 40 | bytes 41 | random_uintegers 42 | random_raw 43 | 44 | Permutations 45 | ============ 46 | .. autosummary:: 47 | :toctree: generated/ 48 | 49 | shuffle 50 | permutation 51 | 52 | Distributions 53 | ============= 54 | .. autosummary:: 55 | :toctree: generated/ 56 | 57 | beta 58 | binomial 59 | chisquare 60 | complex_normal 61 | dirichlet 62 | exponential 63 | f 64 | gamma 65 | geometric 66 | gumbel 67 | hypergeometric 68 | laplace 69 | logistic 70 | lognormal 71 | logseries 72 | multinomial 73 | multivariate_normal 74 | negative_binomial 75 | noncentral_chisquare 76 | noncentral_f 77 | normal 78 | pareto 79 | poisson 80 | power 81 | rayleigh 82 | standard_cauchy 83 | standard_exponential 84 | standard_gamma 85 | standard_normal 86 | standard_t 87 | triangular 88 | uniform 89 | vonmises 90 | wald 91 | weibull 92 | zipf 93 | -------------------------------------------------------------------------------- /doc/source/xorshift1024.rst: -------------------------------------------------------------------------------- 1 | XorShift1024* Randomstate 2 | ************************* 3 | 4 | .. currentmodule:: randomstate.prng.xorshift1024 5 | 6 | 7 | Random generator 8 | ================ 9 | .. autoclass:: 10 | RandomState 11 | 12 | .. autosummary:: 13 | :toctree: generated/ 14 | 15 | seed 16 | get_state 17 | set_state 18 | 19 | Parallel generation 20 | =================== 21 | .. autosummary:: 22 | :toctree: generated/ 23 | 24 | jump 25 | 26 | Simple random data 27 | ================== 28 | .. autosummary:: 29 | :toctree: generated/ 30 | 31 | rand 32 | randn 33 | randint 34 | random_integers 35 | random_sample 36 | random 37 | ranf 38 | sample 39 | choice 40 | bytes 41 | random_uintegers 42 | random_raw 43 | 44 | Permutations 45 | ============ 46 | .. autosummary:: 47 | :toctree: generated/ 48 | 49 | shuffle 50 | permutation 51 | 52 | Distributions 53 | ============= 54 | .. autosummary:: 55 | :toctree: generated/ 56 | 57 | beta 58 | binomial 59 | chisquare 60 | complex_normal 61 | dirichlet 62 | exponential 63 | f 64 | gamma 65 | geometric 66 | gumbel 67 | hypergeometric 68 | laplace 69 | logistic 70 | lognormal 71 | logseries 72 | multinomial 73 | multivariate_normal 74 | negative_binomial 75 | noncentral_chisquare 76 | noncentral_f 77 | normal 78 | pareto 79 | poisson 80 | power 81 | rayleigh 82 | standard_cauchy 83 | standard_exponential 84 | standard_gamma 85 | standard_normal 86 | standard_t 87 | triangular 88 | uniform 89 | vonmises 90 | wald 91 | weibull 92 | zipf 93 | -------------------------------------------------------------------------------- /doc/source/xorshift128.rst: -------------------------------------------------------------------------------- 1 | XorShift128+ Randomstate 2 | ************************ 3 | 4 | .. currentmodule:: randomstate.prng.xorshift128 5 | 6 | 7 | Random generator 8 | ================ 9 | .. autoclass:: 10 | RandomState 11 | 12 | .. autosummary:: 13 | :toctree: generated/ 14 | 15 | seed 16 | get_state 17 | set_state 18 | 19 | Parallel generation 20 | =================== 21 | .. autosummary:: 22 | :toctree: generated/ 23 | 24 | jump 25 | 26 | Simple random data 27 | ================== 28 | .. autosummary:: 29 | :toctree: generated/ 30 | 31 | rand 32 | randn 33 | randint 34 | random_integers 35 | random_sample 36 | random 37 | ranf 38 | sample 39 | choice 40 | bytes 41 | random_uintegers 42 | random_raw 43 | 44 | Permutations 45 | ============ 46 | .. autosummary:: 47 | :toctree: generated/ 48 | 49 | shuffle 50 | permutation 51 | 52 | Distributions 53 | ============= 54 | .. autosummary:: 55 | :toctree: generated/ 56 | 57 | beta 58 | binomial 59 | chisquare 60 | complex_normal 61 | dirichlet 62 | exponential 63 | f 64 | gamma 65 | geometric 66 | gumbel 67 | hypergeometric 68 | laplace 69 | logistic 70 | lognormal 71 | logseries 72 | multinomial 73 | multivariate_normal 74 | negative_binomial 75 | noncentral_chisquare 76 | noncentral_f 77 | normal 78 | pareto 79 | poisson 80 | power 81 | rayleigh 82 | standard_cauchy 83 | standard_exponential 84 | standard_gamma 85 | standard_normal 86 | standard_t 87 | triangular 88 | uniform 89 | vonmises 90 | wald 91 | weibull 92 | zipf 93 | -------------------------------------------------------------------------------- /github_deploy_key.enc: -------------------------------------------------------------------------------- 1 | gAAAAABY2oG1O9Rm4o9sEqrEvbdklscjnF2G01qQnu51LwkBJxs6xHQx-pL0Hpfi5eZEfV4ajcwu6C-tZ-hVUe0kGVtLDGsBaK7bn93Unzv8obxYVKXJaGEMBo7mzi0yk-TBJ-FlpOE2e_nXXgGmjwC0TuZ1QRoe_0x6vF-RDksT4ujTizFY2HdUluZ0Sa5gOfoGqV4i1PpJTrWxpn6zGMxbj2BrQ-INGyw9DD2MNwfpzVD3MPlFHvR9G_02e76koToavyLNr_X86nhqV5hMlwfn2Xrft-twGRL7OCyw9OHGlqmn_-dbSiOrdX8Pj2AIgEBNPJ0BomknRe_QReQVoRqJSUXljxu6bE1iqv7fi2HS9jX8Lgis23K_krRhrkzrJVwb8Nuzo-J7AAoOgM5AEzX19fZrz0bPUHfjy0QIiWjox4CoLMtr061rGuOxqLllufPHvopR-Vi4DBkx-vRvNuSbpI254AS9-fbTKzUVt4iO4YiIvvNn9n2p0kSnIKR9Ku0P5oxXH8nfv61ASlwBZ6UAS8eAHKWyUigBl-HDG7KTE-2PaidC8BDzUBTDtDKuV1ZApe6LbFtm9iJW6CCnglz2a6CqD8UYmkgm1zKVUCenl3IP-qluMxk5AAS36xTyvnEcAXo4riVBnvJ8xxQI4QHDC09c8D01AKsZv6-C2Fp6Ik7OWOv3fYAUBRu-Zb1jtrVKd9-ODav21GTRso-lrd3FfqYP5Aq1gfStUFGF6vSX88oDAxYKXrHiU49DNPD7AzN0J-CVFZ-Fcfu86GFrlFIKrQbGn6nIckyX2XG05O70h3JzFK9ybheEoX32cYWkyOVsXmaUwmmDzLa166YgpVRixhNmMmvr3VQ2Idx0A45u71FbQS2C2jkczxdye7Us39vVTJoR_10LhFT-9uMpJZt8qabnAz40mNCMh1IrvvyDD-Y6yZ1y5Cdv2guv__mJa1FKNEpSKpig0h6RGyV5FCHLf_YCkRwLdvve-r_hnBneu5PedRfZnNu_GSpUKYTEQRW6qIq4tlg6DvrGlalFFBefvNd78aUGvHliXoyAUsOF3YvNKNhzf2P50ZLuRaZICTfAHVGTAVScf3ToBQYD-wUVcJhE7Ms1NIz0l9bEIXBA8k4Ktn-n31uN0Wbszg4ctx0uVXbeX2pcG9dHN3cmfr5HKgsmkNp-Uj6f4BNxU4L_0fYvmGELPm5TGz8njG8PjoHKzBIdcSzYL764y-09E5HYT1fEWSKz2b_y9C5AjZ_aRYyhAnMPyf3VBKcrLmZXc7jQSOkR8IF0iKqaOzD5qnYNRcC6nx75WYiAa13Sp4OYdiuhjHGqct2LZ0AOnH-SyvO4p56r18L15pblvOhaVSoJ1319jxRLPapqTkP1wROsifn3Z512k0_ZBxLrEcxVBGjaIaczyGpva8R-X4DeVgFE6tsWLfhl7CTwVlnGr7xhI-bFfPYrGRfGIxQWnmNENW8wZWSMSXjKEbVRleScN6VjNtItkWyFaxmvVoHYqDK28s9Uruipa4HiZZ4--6kMrOBxpQMwHtA4JpD-iCJ3OhHrXd7eNScbFeCvlu241TbDHjBGnyIP7eLsnbvdjm31KnorPKXgctJBIOKqvoF5eG0oZrV7yv5XnTXwbWX96r8-ewecD6DlbiTMaonQqrrmZlhmoFMRz8iakvx4m0HDJ_rQvzAom1p-jQSv7mHJD-RP0y0ohsEmslaZ2Yx81qmwBjHFd2LLPdn531OxL8QoEhTjdEaNBEf5pTbDY7WHSdxWmpLqIyU0C_HLgFSIU3BWmBaeMOUcmBhW36G36X1QNjkqCERJmgWyJpV1zEJL7Qzs0VkyNxQsaojGuUJWbketbKv4K44-LUEmUG4-tTWej3P2LDrJ-403rR96xrNXTeFBC43werzwQIHQgGEukHntMslyWYF5sZ_Ib9UHpTHBqKRq7y5PFaVm7j3ZuNkK6jifpSDoanXrSjoA0n_5UmF6yxyq_LE51gk9_-dfLmckcLbYzdylcAbKgTNijp699N0tlZJp_k4hdtDOiE3-kD8cHTySEYKkdZIuH1lEqcXs1NXXHY_xU-D6Zaii4D7fW7gNmkr_Wnju5oys_AvIqYXaw9WUhAOWCLjk6KzoaDbIPxKWxv_2vk9Wm1H5O-B7B0h-_1k84Q4LeV1QS8Bb4Ln1wIPh-cXYmSjK7S7v0twtCqVgIsgOVuEgBasosIrZng4_yfKAKCZn7OSKQJsPTPTPORaUHMGEEteRxc8HQqWJ-DXobtUpxrifSac4EhJTH8lLqx34vYhg5RyXQSKXXVvDPbPrXM_gRKA1icf_PzKMX0BDqa6GDuCa8xx2CBHn2TxsUVmoEX3K6G6l-qnp1AvXucE6Wn8QkSZafio0Ww9zTirW1eVDR063XbM8v4oWY9s7KZdNo2twNYxNcHRrCTCqzP4q27pI6TEcoSCX3rfQ7al12UCkt2pT6XBPCHi6qwQ2Q4JJOoIqmAvOpggScwcdOOX8asQOHTIwM2xlhTrxCn4f6x_pf5hmlYgmNsEviY7retaqsIEnWIh2_JMzdmibyh9335MeWe1lPz6lGyLodTwwXr_ddqpmnl305P1ejCfB-QPOAnIC73V-EOqBSrUW1JTd-Zt06lANhDud65HpaiKljYR8F8WPQcovFqZWvRWnFmP0A8XikP9MPgOwSW2aO-0NtDJcZbtaJlpXpE66z1U5NLTE7yTO3vQgczDhLJVEr_HnPTV9MI4I1JgX0fRsmYvKN4TVMl29HLY10-vjaMGDW0Ao-8R8TnLu16wt9I0OeRT_A6lM1NE1ldeG99ejOwaoI47X3ccPQfUn5a5yszS9lfd0HVyppTt7EawCZNdq-_FUl8pYpNO3vs-E5sqpf92xOXXTMosWM3f9R8jvdNijr-WhFV7l7NqEHeOMH3I-rcSaYD_AZjQJzVeLpdaADw633Wyw86TvagVlRPFXYHlgamCGUNb_MR323I0-Y8OLac2EwTt9TsxiKBnVf9c7GkvPP8PYgWduJ7pdnNI28PLYw9weVceYt6JZdI_OgW9ooQzQ2P-bBntGGaRK_0SA9VxzMKfqUfOGTETSA2l-upfOqNZDgBsIeOaQBjeRiOp-mg7spA48DsTqTirT3jK4pijjezVYtd84n1u2DrXjg0M2z6exZ91pSG_S4Lr9l8RrMrupt3cgEaVxzeENR6qKwlOP4D0EqVHnJJhPdWz33GokhM4HX-zmv1Tut2AeWB56mUvcAWdJf8CNje_bhDIdaQvgwAJ4sA83b2MtuyuI35q2-qtcySRpACpa6ht7C7InIFWRiDSPICTFQuzCLEs_k7JaztFuK8BnudbtpQkSXYdA7_oa1rX1idsPh0BBTHTS4wW7enD0Tu5A1AXLdg0RcqVczbYFliSZe_4txsL-32-WUBgupT-PInxUCcSsuRMrimY7RbkmnhgMFJimj91LtD54YTbCFyVjDm1jXe9-jRmBOn-lJLLrUMfqKrWBK4QW2_6kxD_8lGe0A49eccNs50wQIYCYLjlKMMLPtrdCqq7wzWv1uAH_2cQgSKy9iBl-jmj2_W2Zo8nUEvNlnWIHf5_1hrdDLaHgwjjxJmEQg9R65c7lykMYhrTqOGD69IYxXDree7WNRiwGkrrlBinr-uTCJZS1lUivF8lH-w4MZEQE_dbSYIeI0L7SCNp0etKhvQ2Yg8tcum7O0lKisIHmehiLS4SLcUEl9358TRwAjVeHcDP3mXavbWninIpH5iatSaF1M47DrlhS-wBmNzvwg95WE18k3gTqXxgbW0J91WpKY8Z1bBby_TUGzb3GxuYv9J5couUO6doJ50gw_FWE1aBKZSh8a33AGOCbDuIGB5cxG_0UmLnEZVQOK5yDj7o1Kkl4OCSGvf0Jg-fpT95f8lz5vorGTp_9iTXWGIWdEHCl_gbJEBFUajTu61dJn38uHR2-oCG6UcADwBDJrLOmr6c6ZC1BkQzkPAZpUcHYdJh47Y8RP9sn4aXsKmeUmIFlQWxtIG3o4tkFmKW3Q6lLXS8w0jgXYFFGOdJXvJnqa-ChZtecY2x0a0SEgFN-lxElBbCPm10MzIMD5DnijWJDtL3emEk6gi23Te4ydxwPBPqT-CGRbwdpxhMWVvbwITsRkPdsHQioiOIwhnAMaPxa1MCElxoFy8ran0zfjGOuFHjiXku_u9Yc8oZViwWHH37piQPNawkH0dB7FMNAj-cxE61RcaClzn152qvX8c7Ak7dPmSeWfFHj54eJEtjmuLM1qSVOZ4W1YXq0MdIQ1hIAhwKGYWYseQCwsTsfC6zTnN9Ocupux-SpiLc7Vr7R5yTNac6i6Sl5VV6_Q3SqzcGvsVnWoIt2wSPiqYbn0W_5xq5Tb6G4W6vbtc7dGSF2dIYtXJQF4sNmubA= -------------------------------------------------------------------------------- /licenses/NUMPY-LICENSE.rst: -------------------------------------------------------------------------------- 1 | ************* 2 | NumPy License 3 | ************* 4 | 5 | Copyright (c) 2005, NumPy Developers 6 | 7 | All rights reserved. 8 | 9 | Redistribution and use in source and binary forms, with or without 10 | modification, are permitted provided that the following conditions are 11 | met: 12 | 13 | * Redistributions of source code must retain the above copyright 14 | notice, this list of conditions and the following disclaimer. 15 | 16 | * Redistributions in binary form must reproduce the above 17 | copyright notice, this list of conditions and the following 18 | disclaimer in the documentation and/or other materials provided 19 | with the distribution. 20 | 21 | * Neither the name of the NumPy Developers nor the names of any 22 | contributors may be used to endorse or promote products derived 23 | from this software without specific prior written permission. 24 | 25 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 | -------------------------------------------------------------------------------- /licenses/RANDOMKIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2003-2005, Jean-Sebastien Roy (js@jeannot.org) 2 | 3 | The rk_random and rk_seed functions algorithms and the original design of 4 | the Mersenne Twister RNG: 5 | 6 | Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, 7 | All rights reserved. 8 | 9 | Redistribution and use in source and binary forms, with or without 10 | modification, are permitted provided that the following conditions 11 | are met: 12 | 13 | 1. Redistributions of source code must retain the above copyright 14 | notice, this list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright 17 | notice, this list of conditions and the following disclaimer in the 18 | documentation and/or other materials provided with the distribution. 19 | 20 | 3. The names of its contributors may not be used to endorse or promote 21 | products derived from this software without specific prior written 22 | permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 27 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 28 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 29 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 30 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 31 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 32 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 33 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 34 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | 36 | Original algorithm for the implementation of rk_interval function from 37 | Richard J. Wagner's implementation of the Mersenne Twister RNG, optimised by 38 | Magnus Jonsson. 39 | 40 | Constants used in the rk_double implementation by Isaku Wada. 41 | 42 | Permission is hereby granted, free of charge, to any person obtaining a 43 | copy of this software and associated documentation files (the 44 | "Software"), to deal in the Software without restriction, including 45 | without limitation the rights to use, copy, modify, merge, publish, 46 | distribute, sublicense, and/or sell copies of the Software, and to 47 | permit persons to whom the Software is furnished to do so, subject to 48 | the following conditions: 49 | 50 | The above copyright notice and this permission notice shall be included 51 | in all copies or substantial portions of the Software. 52 | 53 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 54 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 55 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 56 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 57 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 58 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 59 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 60 | -------------------------------------------------------------------------------- /papers/136.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/ng-numpy-randomstate/b397db9cb8688b291fc40071ab043009dfa05a85/papers/136.pdf -------------------------------------------------------------------------------- /papers/17-MODGEN.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/ng-numpy-randomstate/b397db9cb8688b291fc40071ab043009dfa05a85/papers/17-MODGEN.pdf -------------------------------------------------------------------------------- /papers/IIP1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/ng-numpy-randomstate/b397db9cb8688b291fc40071ab043009dfa05a85/papers/IIP1.pdf -------------------------------------------------------------------------------- /papers/RICP2000_4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/ng-numpy-randomstate/b397db9cb8688b291fc40071ab043009dfa05a85/papers/RICP2000_4.pdf -------------------------------------------------------------------------------- /papers/RIJP2000_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/ng-numpy-randomstate/b397db9cb8688b291fc40071ab043009dfa05a85/papers/RIJP2000_2.pdf -------------------------------------------------------------------------------- /papers/combmrg2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/ng-numpy-randomstate/b397db9cb8688b291fc40071ab043009dfa05a85/papers/combmrg2.pdf -------------------------------------------------------------------------------- /papers/generate_consts.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from numpy import log, sqrt, exp 3 | 4 | pdf = lambda x: exp(-0.5 * x * x) 5 | 6 | def inverse_pdf(p): 7 | return sqrt(-2 * log(p)) 8 | 9 | 10 | C = 256 11 | r = 3.6541528853610087963519472518 12 | Vr = 0.0049286732339746519 13 | x = np.zeros(C) 14 | x[0] = r 15 | for i in range(1, C - 1): 16 | x[i] = inverse_pdf(pdf(x[i - 1]) + Vr / x[i - 1]) 17 | 18 | x = x[::-1] 19 | 20 | # ki 21 | ki_final = {} 22 | scales = (2 ** 52, 2 ** 23) 23 | names = ('double', 'float') 24 | for name, scale in zip(names, scales): 25 | ki = np.zeros(C, np.uint64) 26 | prob_ratio = x[:-1] / x[1:] 27 | ki[1:] = np.round(prob_ratio * scale).astype(np.uint64) 28 | ki[0] = np.uint64(np.round(((x.max() * pdf(x.max())) / Vr) * scale)) 29 | digits = 10 if name == 'float' else 18 30 | out = ["{0:#0{1}X}".format((ki[i]), digits) for i in range(C)] 31 | ki_final[name] = out 32 | 33 | # wi 34 | wi_final = {} 35 | scales = (2 ** 52, 2 ** 23) 36 | names = ('double', 'float') 37 | for name, scale in zip(names, scales): 38 | wi = x.copy() 39 | wi[0] = Vr / pdf(x.max()) 40 | wi_final[name] = wi / scale 41 | 42 | # fi 43 | fi_final = {'double': pdf(x), 'float': pdf(x)} 44 | 45 | constants = {'ziggurat_nor_r': 3.6541528853610087963519472518, 46 | 'ziggurat_nor_inv_r': 0.27366123732975827203338247596, 47 | 'ziggurat_exp_r': 7.6971174701310497140446280481} 48 | 49 | type_map = {'uint64': 'uint64_t', 'uint32': 'uint32_t', 'double': 'double', 50 | 'float': 'float'} 51 | extra_text = {'uint64': 'ULL', 'uint32': 'UL', 'double': '', 'float': 'f'} 52 | 53 | 54 | def write(a, name, dtype): 55 | ctype = type_map[dtype] 56 | out = 'static const ' + ctype + ' ' + name + '[] = { \n' 57 | format_str = '{0: .20e}' if dtype in ('double', 'float') else '{0}' 58 | formatted = [format_str.format(a[i]) + extra_text[dtype] for i in range(len(a))] 59 | lines = len(formatted) // 4 60 | for i in range(lines): 61 | temp = ' ' + ', '.join(formatted[4 * i:4 * i + 4]) 62 | if i < (lines - 1): 63 | temp += ',\n' 64 | else: 65 | temp += '\n' 66 | out += temp 67 | out += '};' 68 | return out 69 | 70 | 71 | with open('./ziggurat_constants.h', 'w') as f: 72 | f.write(write(ki_final['double'], 'ki_double', 'uint64')) 73 | f.write('\n\n') 74 | f.write(write(wi_final['double'], 'wi_double', 'double')) 75 | f.write('\n\n') 76 | f.write(write(fi_final['double'], 'fi_double', 'double')) 77 | 78 | f.write('\n\n\n\n') 79 | f.write(write(ki_final['float'], 'ki_float', 'uint32')) 80 | f.write('\n\n') 81 | f.write(write(wi_final['float'], 'wi_float', 'float')) 82 | f.write('\n\n') 83 | f.write(write(fi_final['float'], 'fi_float', 'float')) 84 | -------------------------------------------------------------------------------- /papers/slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/ng-numpy-randomstate/b397db9cb8688b291fc40071ab043009dfa05a85/papers/slides.pdf -------------------------------------------------------------------------------- /papers/tr_0503.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/ng-numpy-randomstate/b397db9cb8688b291fc40071ab043009dfa05a85/papers/tr_0503.pdf -------------------------------------------------------------------------------- /papers/ziggurat.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/ng-numpy-randomstate/b397db9cb8688b291fc40071ab043009dfa05a85/papers/ziggurat.pdf -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["numpy>=1.09", "cython>=0.24", "setuptools", "wheel"] -------------------------------------------------------------------------------- /randomstate/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | randomstate contains implements NumPy-like RandomState objects with an 3 | enhanced feature set. 4 | 5 | This modules includes a number of alternative random number generators 6 | in addition to the MT19937 that is included in NumPy. The RNGs include: 7 | 8 | - `MT19937 `__, 9 | the NumPy rng 10 | - `dSFMT `__ a 11 | SSE2-aware version of the MT19937 generator that is especially fast 12 | at generating doubles 13 | - `SFMT `__ a 14 | SSE2-aware version of the MT19937 generator that is optimized for 15 | integer values 16 | - `xorshift128+ `__, 17 | `xoroshiro128+ `__ and 18 | `xorshift1024\* `__ 19 | - `PCG32 `__ and 20 | `PCG64 `__ 21 | - `MRG32K3A `__ 22 | - A multiplicative lagged fibonacci generator (LFG(63, 1279, 861, \*)) 23 | 24 | Help is available at `on github `_. 25 | 26 | New Features 27 | ------------ 28 | 29 | - ``standard_normal``, ``normal``, ``randn`` and 30 | ``multivariate_normal`` all support an additional ``method`` keyword 31 | argument which can be ``bm`` or ``zig`` where ``bm`` corresponds to 32 | the current method using the Box-Muller transformation and ``zig`` 33 | uses the much faster (100%+) Ziggurat method. 34 | - ``standard_exponential`` and ``standard_gamma`` both support an 35 | additional ``method`` keyword argument which can be ``inv`` or 36 | ``zig`` where ``inv`` corresponds to the current method using the 37 | inverse CDF and ``zig`` uses the much faster (100%+) Ziggurat method. 38 | - Core random number generators can produce either single precision 39 | (``np.float32``) or double precision (``np.float64``, the default) 40 | using an the optional keyword argument ``dtype`` 41 | - Core random number generators can fill existing arrays using the 42 | ``out`` keyword argument 43 | 44 | New Functions 45 | ------------- 46 | 47 | - ``random_entropy`` - Read from the system entropy provider, which is 48 | commonly used in cryptographic applications 49 | - ``random_raw`` - Direct access to the values produced by the 50 | underlying PRNG. The range of the values returned depends on the 51 | specifics of the PRNG implementation. 52 | - ``random_uintegers`` - unsigned integers, either 32- 53 | (``[0, 2**32-1]``) or 64-bit (``[0, 2**64-1]``) 54 | - ``jump`` - Jumps RNGs that support it. ``jump`` moves the state a 55 | great distance. *Only available if supported by the RNG.* 56 | - ``advance`` - Advanced the core RNG 'as-if' a number of draws were 57 | made, without actually drawing the numbers. *Only available if 58 | supported by the RNG.* 59 | """ 60 | from __future__ import division, absolute_import, print_function 61 | 62 | import warnings 63 | 64 | from randomstate._deprecated import (DEPRECATION_MESSAGE, 65 | RandomStateDeprecationWarning) 66 | warnings.warn(DEPRECATION_MESSAGE, RandomStateDeprecationWarning) 67 | 68 | from randomstate.prng.mt19937 import * 69 | from randomstate.entropy import random_entropy 70 | import randomstate.prng 71 | 72 | from ._version import get_versions 73 | __version__ = get_versions()['version'] 74 | del get_versions 75 | -------------------------------------------------------------------------------- /randomstate/_deprecated.py: -------------------------------------------------------------------------------- 1 | DEPRECATION_MESSAGE = """ 2 | **End-of-life notification** 3 | 4 | This library was designed to bring alternative generators to the NumPy 5 | infrastructure. It as been successful in advancing the conversation 6 | for a future implementation of a new random number API in NumPy which 7 | will allow new algorithms and/or generators. The next step 8 | in this process is to separate the basic (or core RNG) from the 9 | functions that transform random bits into useful random numbers. 10 | This has been implemented in a successor project **randomgen** 11 | available on GitHub 12 | 13 | https://github.com/bashtage/randomgen 14 | 15 | or PyPi 16 | 17 | https://pypi.org/project/randomstate/. 18 | 19 | randomgen has a slightly different API, so please see the randomgen documentation 20 | 21 | https://bashtage.github.io/randomgen. 22 | """ 23 | 24 | 25 | class RandomStateDeprecationWarning(Warning): 26 | pass 27 | -------------------------------------------------------------------------------- /randomstate/aligned_malloc.c: -------------------------------------------------------------------------------- 1 | #include "aligned_malloc.h" 2 | 3 | static NPY_INLINE void *PyArray_realloc_aligned(void *p, size_t n); 4 | 5 | static NPY_INLINE void *PyArray_malloc_aligned(size_t n); 6 | 7 | static NPY_INLINE void *PyArray_calloc_aligned(size_t n, size_t s); 8 | 9 | static NPY_INLINE void PyArray_free_aligned(void *p); -------------------------------------------------------------------------------- /randomstate/aligned_malloc.h: -------------------------------------------------------------------------------- 1 | #include "Python.h" 2 | #include "numpy/npy_common.h" 3 | 4 | #define NPY_MEMALIGN 16 /* 16 for SSE2, 32 for AVX, 64 for Xeon Phi */ 5 | 6 | static NPY_INLINE void *PyArray_realloc_aligned(void *p, size_t n) 7 | { 8 | void *p1, **p2, *base; 9 | size_t old_offs, offs = NPY_MEMALIGN - 1 + sizeof(void *); 10 | if (NPY_UNLIKELY(p != NULL)) 11 | { 12 | base = *(((void **)p) - 1); 13 | if (NPY_UNLIKELY((p1 = PyMem_Realloc(base, n + offs)) == NULL)) 14 | return NULL; 15 | if (NPY_LIKELY(p1 == base)) 16 | return p; 17 | p2 = (void **)(((Py_uintptr_t)(p1) + offs) & ~(NPY_MEMALIGN - 1)); 18 | old_offs = (size_t)((Py_uintptr_t)p - (Py_uintptr_t)base); 19 | memmove((void *)p2, ((char *)p1) + old_offs, n); 20 | } 21 | else 22 | { 23 | if (NPY_UNLIKELY((p1 = PyMem_Malloc(n + offs)) == NULL)) 24 | return NULL; 25 | p2 = (void **)(((Py_uintptr_t)(p1) + offs) & ~(NPY_MEMALIGN - 1)); 26 | } 27 | *(p2 - 1) = p1; 28 | return (void *)p2; 29 | } 30 | 31 | static NPY_INLINE void *PyArray_malloc_aligned(size_t n) 32 | { 33 | return PyArray_realloc_aligned(NULL, n); 34 | } 35 | 36 | static NPY_INLINE void *PyArray_calloc_aligned(size_t n, size_t s) 37 | { 38 | void *p; 39 | if (NPY_UNLIKELY((p = PyArray_realloc_aligned(NULL, n * s)) == NULL)) 40 | return NULL; 41 | memset(p, 0, n * s); 42 | return p; 43 | } 44 | 45 | static NPY_INLINE void PyArray_free_aligned(void *p) 46 | { 47 | void *base = *(((void **)p) - 1); 48 | PyMem_Free(base); 49 | } -------------------------------------------------------------------------------- /randomstate/aligned_malloc.pxi: -------------------------------------------------------------------------------- 1 | cdef extern from "aligned_malloc.h": 2 | cdef void *PyArray_realloc_aligned(void *p, size_t n); 3 | cdef void *PyArray_malloc_aligned(size_t n); 4 | cdef void *PyArray_calloc_aligned(size_t n, size_t s); 5 | cdef void PyArray_free_aligned(void *p); 6 | -------------------------------------------------------------------------------- /randomstate/array_fillers.pxi.in: -------------------------------------------------------------------------------- 1 | ctypedef void (* random_float_fill)(aug_state* state, np.npy_intp count, float *out) nogil 2 | ctypedef void (* random_double_fill)(aug_state* state, np.npy_intp count, double *out) nogil 3 | 4 | {{ 5 | py: 6 | ctypes = (('float64', 'double'), 7 | ('float32', 'float')) 8 | }} 9 | {{for nptype, ctype in ctypes}} 10 | 11 | 12 | cdef object {{ctype}}_fill(aug_state* state, void *func, object size, object lock, object out): 13 | cdef random_{{ctype}}_fill f = func 14 | cdef {{ctype}} fout 15 | cdef {{ctype}} *out_array_data 16 | cdef np.ndarray out_array 17 | cdef np.npy_intp n 18 | 19 | if size is None and out is None: 20 | with lock: 21 | f(state, 1, &fout) 22 | return fout 23 | 24 | if out is not None: 25 | check_output(out, np.{{nptype}}, size) 26 | out_array = out 27 | else: 28 | out_array = np.empty(size, np.{{nptype}}) 29 | 30 | n = np.PyArray_SIZE(out_array) 31 | out_array_data = <{{ctype}} *>np.PyArray_DATA(out_array) 32 | with lock, nogil: 33 | f(state, n, out_array_data) 34 | return out_array 35 | 36 | {{endfor}} 37 | 38 | 39 | cdef object float_fill_from_double(aug_state* state, void *func, object size, object lock): 40 | cdef random_double_fill f = func 41 | cdef double out 42 | cdef double* buffer 43 | cdef float *out_array_data 44 | cdef np.ndarray out_array 45 | cdef np.npy_intp i, n, buffer_size, remaining, total, fetch 46 | 47 | if size is None: 48 | with lock: 49 | f(state, 1, &out) 50 | return out 51 | else: 52 | out_array = np.empty(size, np.float32) 53 | n = np.PyArray_SIZE(out_array) 54 | buffer_size = 1000 if n > 1000 else n 55 | total = 0 56 | remaining = n 57 | buffer = malloc(buffer_size * sizeof(double)) 58 | 59 | out_array_data = np.PyArray_DATA(out_array) 60 | while remaining > 0: 61 | fetch = 1000 if remaining > 1000 else remaining 62 | with lock, nogil: 63 | f(state, fetch, buffer) 64 | for i in range(fetch): 65 | out_array_data[i + total] = buffer[i] 66 | total += fetch 67 | remaining -= fetch 68 | 69 | free(buffer) 70 | 71 | return out_array 72 | -------------------------------------------------------------------------------- /randomstate/binomial.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "distributions.h": 2 | 3 | cdef struct s_binomial_t: 4 | int has_binomial 5 | double psave 6 | long nsave 7 | double r 8 | double q 9 | double fm 10 | long m 11 | double p1 12 | double xm 13 | double xl 14 | double xr 15 | double c 16 | double laml 17 | double lamr 18 | double p2 19 | double p3 20 | double p4 21 | 22 | ctypedef s_binomial_t binomial_t 23 | -------------------------------------------------------------------------------- /randomstate/config.pxi: -------------------------------------------------------------------------------- 1 | # Autogenerated 2 | 3 | DEF RS_RNG_MOD_NAME='xorshift128' 4 | -------------------------------------------------------------------------------- /randomstate/defaults.pxi: -------------------------------------------------------------------------------- 1 | DEF RS_NORMAL_METHOD = u'zig' 2 | DEF RS_RNG_SEED = 1 3 | DEF RS_RNG_ADVANCEABLE = 0 4 | DEF RS_RNG_JUMPABLE = 0 5 | DEF RS_RNG_STATE_LEN = 4 6 | DEF RS_SEED_NBYTES = 2 7 | DEF RS_SEED_ARRAY_BITS = 64 8 | DEF JUMP_DOCSTRING = """ 9 | """ -------------------------------------------------------------------------------- /randomstate/entropy.pyx: -------------------------------------------------------------------------------- 1 | cimport numpy as np 2 | import numpy as np 3 | 4 | from libc.stdint cimport uint32_t 5 | 6 | cdef extern from "src/common/entropy.h": 7 | cdef bint entropy_getbytes(void* dest, size_t size) 8 | cdef bint entropy_fallback_getbytes(void *dest, size_t size) 9 | 10 | cdef Py_ssize_t compute_numel(size): 11 | cdef Py_ssize_t i, n = 1 12 | if isinstance(size, tuple): 13 | for i in range(len(size)): 14 | n *= size[i] 15 | else: 16 | n = size 17 | return n 18 | 19 | def random_entropy(size=None, source='system'): 20 | """ 21 | random_entropy(size=None, source='system') 22 | 23 | Read entropy from the system cryptographic provider 24 | 25 | Parameters 26 | ---------- 27 | size : int or tuple of ints, optional 28 | Output shape. If the given shape is, e.g., ``(m, n, k)``, then 29 | ``m * n * k`` samples are drawn. Default is None, in which case a 30 | single value is returned. 31 | source : str {'system', 'fallback'} 32 | Source of entropy. 'system' uses system cryptographic pool. 33 | 'fallback' uses a hash of the time and process id. 34 | 35 | Returns 36 | ------- 37 | entropy : scalar or ndarray 38 | Entropy bits in 32-bit unsigned integers 39 | 40 | Notes 41 | ----- 42 | On Unix-like machines, reads from ``/dev/urandom``. On Windows machines 43 | reads from the RSA algorithm provided by the cryptographic service 44 | provider. 45 | 46 | This function reads from the system entropy pool and so samples are 47 | not reproducible. In particular, it does *NOT* make use of a 48 | RandomState, and so ``seed``, ``get_state`` and ``set_state`` have no 49 | effect. 50 | 51 | Raises RuntimeError if the command fails. 52 | """ 53 | cdef bint success = True 54 | cdef Py_ssize_t n = 0 55 | cdef uint32_t random = 0 56 | cdef uint32_t [:] randoms 57 | 58 | if source not in ('system', 'fallback'): 59 | raise ValueError('Unknown value in source.') 60 | 61 | if size is None: 62 | if source == 'system': 63 | success = entropy_getbytes(&random, 4) 64 | else: 65 | success = entropy_fallback_getbytes(&random, 4) 66 | else: 67 | n = compute_numel(size) 68 | randoms = np.zeros(n, dtype=np.uint32) 69 | if source == 'system': 70 | success = entropy_getbytes((&randoms[0]), 4 * n) 71 | else: 72 | success = entropy_fallback_getbytes((&randoms[0]), 4 * n) 73 | if not success: 74 | raise RuntimeError('Unable to read from system cryptographic provider') 75 | 76 | if n == 0: 77 | return random 78 | return np.asarray(randoms).reshape(size) -------------------------------------------------------------------------------- /randomstate/interface/dSFMT/dSFMT-shim.c: -------------------------------------------------------------------------------- 1 | #include "dSFMT-shim.h" 2 | #include "dSFMT-poly.h" 3 | 4 | extern NPY_INLINE uint32_t random_uint32(aug_state *state); 5 | 6 | extern NPY_INLINE uint64_t random_uint64(aug_state *state); 7 | 8 | extern NPY_INLINE double random_double(aug_state *state); 9 | 10 | extern NPY_INLINE uint64_t random_raw_values(aug_state *state); 11 | 12 | void reset_buffer(aug_state *state) { 13 | int i = 0; 14 | for (i = 0; i < (2 * DSFMT_N); i++) { 15 | state->buffered_uniforms[i] = 0.0; 16 | } 17 | state->buffer_loc = 2 * DSFMT_N; 18 | } 19 | 20 | extern void set_seed_by_array(aug_state *state, uint32_t init_key[], 21 | int key_length) { 22 | reset_buffer(state); 23 | dsfmt_init_by_array(state->rng, init_key, key_length); 24 | } 25 | 26 | void set_seed(aug_state *state, uint32_t seed) { 27 | reset_buffer(state); 28 | dsfmt_init_gen_rand(state->rng, seed); 29 | } 30 | 31 | void entropy_init(aug_state *state) { 32 | uint32_t seeds[1]; 33 | entropy_fill((void *)seeds, sizeof(seeds)); 34 | set_seed(state, seeds[0]); 35 | } 36 | 37 | void jump_state(aug_state *state) { dSFMT_jump(state->rng, poly_128); } -------------------------------------------------------------------------------- /randomstate/interface/dSFMT/dSFMT-shim.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #include "../../src/common/stdint.h" 3 | #else 4 | #include 5 | #endif 6 | 7 | #include "Python.h" 8 | #include "numpy/npy_common.h" 9 | 10 | #include "../../src/common/binomial.h" 11 | #include "../../src/common/entropy.h" 12 | #include "../../src/dSFMT/dSFMT-jump.h" 13 | #include "../../src/dSFMT/dSFMT.h" 14 | 15 | typedef struct s_aug_state { 16 | dsfmt_t *rng; 17 | binomial_t *binomial; 18 | 19 | int has_gauss, has_gauss_float, shift_zig_random_int, has_uint32; 20 | float gauss_float; 21 | double gauss; 22 | uint32_t uinteger; 23 | uint64_t zig_random_int; 24 | 25 | double *buffered_uniforms; 26 | int buffer_loc; 27 | } aug_state; 28 | 29 | static NPY_INLINE double random_double_from_buffer(aug_state *state) { 30 | double out; 31 | if (state->buffer_loc >= (2 * DSFMT_N)) { 32 | state->buffer_loc = 0; 33 | dsfmt_fill_array_close1_open2(state->rng, state->buffered_uniforms, 34 | 2 * DSFMT_N); 35 | } 36 | out = state->buffered_uniforms[state->buffer_loc]; 37 | state->buffer_loc++; 38 | return out; 39 | } 40 | 41 | static NPY_INLINE uint32_t random_uint32(aug_state *state) { 42 | /* TODO: This can be improved to use upper bits */ 43 | double d = random_double_from_buffer( 44 | state); // dsfmt_genrand_close1_open2(state->rng); 45 | uint64_t *out = (uint64_t *)&d; 46 | return (uint32_t)(*out & 0xffffffff); 47 | } 48 | 49 | static NPY_INLINE uint64_t random_uint64(aug_state *state) { 50 | /* TODO: This can be improved to use upper bits */ 51 | double d = random_double_from_buffer( 52 | state); // dsfmt_genrand_close1_open2(state->rng); 53 | uint64_t out; 54 | uint64_t *tmp; 55 | tmp = (uint64_t *)&d; 56 | out = *tmp << 32; 57 | d = random_double_from_buffer( 58 | state); // dsfmt_genrand_close1_open2(state->rng); 59 | tmp = (uint64_t *)&d; 60 | out |= *tmp & 0xffffffff; 61 | return out; 62 | } 63 | 64 | static NPY_INLINE double random_double(aug_state *state) { 65 | return random_double_from_buffer(state) - 1.0; 66 | // return dsfmt_genrand_close1_open2(state->rng) - 1.0; 67 | } 68 | 69 | static NPY_INLINE uint64_t random_raw_values(aug_state *state) { 70 | uint64_t out; 71 | double d = random_double_from_buffer(state); 72 | 73 | memcpy(&out, &d, sizeof(d)); 74 | return out; 75 | } 76 | 77 | extern void entropy_init(aug_state *state); 78 | 79 | extern void set_seed_by_array(aug_state *state, uint32_t init_key[], 80 | int key_length); 81 | 82 | extern void set_seed(aug_state *state, uint32_t seed); 83 | 84 | extern void jump_state(aug_state *state); -------------------------------------------------------------------------------- /randomstate/interface/mlfg-1279-861/mlfg-1279-861-shim.c: -------------------------------------------------------------------------------- 1 | #include "mlfg-1279-861-shim.h" 2 | 3 | extern NPY_INLINE uint32_t random_uint32(aug_state *state); 4 | 5 | extern NPY_INLINE uint64_t random_uint64(aug_state *state); 6 | 7 | extern NPY_INLINE double random_double(aug_state *state); 8 | 9 | extern NPY_INLINE uint64_t random_raw_values(aug_state *state); 10 | 11 | void set_seed(aug_state *state, uint64_t val) { mlfg_seed(state->rng, val); } 12 | 13 | void set_seed_by_array(aug_state *state, uint64_t *vals, int count) { 14 | mlfg_seed_by_array(state->rng, vals, count); 15 | } 16 | 17 | void entropy_init(aug_state *state) { 18 | uint64_t seeds[1]; 19 | entropy_fill((void *)seeds, sizeof(seeds)); 20 | mlfg_seed(state->rng, seeds[0]); 21 | } 22 | -------------------------------------------------------------------------------- /randomstate/interface/mlfg-1279-861/mlfg-1279-861-shim.h: -------------------------------------------------------------------------------- 1 | 2 | #ifdef _WIN32 3 | #include "../../src/common/stdint.h" 4 | #else 5 | #include 6 | #endif 7 | 8 | #include "Python.h" 9 | #include "numpy/npy_common.h" 10 | 11 | #include "../../src/common/binomial.h" 12 | #include "../../src/common/entropy.h" 13 | #include "../../src/mlfg-1279-861/mlfg-1279-861.h" 14 | 15 | 16 | typedef struct s_aug_state { 17 | mlfg_state *rng; 18 | binomial_t *binomial; 19 | 20 | int has_gauss, has_gauss_float, shift_zig_random_int, has_uint32; 21 | float gauss_float; 22 | double gauss; 23 | uint32_t uinteger; 24 | uint64_t zig_random_int; 25 | } aug_state; 26 | 27 | static NPY_INLINE uint32_t random_uint32(aug_state *state) { 28 | return (uint32_t)(mlfg_next(state->rng) >> 32); 29 | } 30 | 31 | static NPY_INLINE uint64_t random_uint64(aug_state *state) { 32 | uint64_t out = mlfg_next(state->rng) & 0xffffffff00000000ULL; 33 | out |= mlfg_next(state->rng) >> 32; 34 | return out; 35 | } 36 | 37 | static NPY_INLINE uint64_t random_raw_values(aug_state *state) { 38 | return mlfg_next(state->rng) >> 1; 39 | } 40 | 41 | static NPY_INLINE double random_double(aug_state *state) { 42 | uint64_t rn; 43 | rn = mlfg_next(state->rng); 44 | return (rn >> 11) * (1.0 / 9007199254740992.0); 45 | } 46 | 47 | extern void set_seed(aug_state *state, uint64_t seed); 48 | 49 | extern void set_seed_by_array(aug_state *state, uint64_t *vals, int count); 50 | 51 | extern void entropy_init(aug_state *state); 52 | -------------------------------------------------------------------------------- /randomstate/interface/mrg32k3a/mrg32k3a-shim.c: -------------------------------------------------------------------------------- 1 | #include "mrg32k3a-shim.h" 2 | 3 | extern NPY_INLINE uint32_t random_uint32(aug_state *state); 4 | 5 | extern NPY_INLINE uint64_t random_uint64(aug_state *state); 6 | 7 | extern NPY_INLINE double random_double(aug_state *state); 8 | 9 | extern NPY_INLINE uint64_t random_raw_values(aug_state *state); 10 | 11 | void entropy_init(aug_state *state) { 12 | uint32_t buf[6] = {0}; 13 | int64_t seeds[6]; 14 | uint32_t i, val; 15 | int all_zero = 0; 16 | while (!all_zero) { 17 | entropy_fill((void *)buf, sizeof(buf)); 18 | for (i = 0; i < 6; i++) { 19 | val = (i < 3) ? STATE_MAX_VALUE_1 : STATE_MAX_VALUE_2; 20 | seeds[i] = (int64_t)(buf[i] % val); 21 | all_zero = all_zero || (seeds[i] > 0); 22 | } 23 | } 24 | init_state(state, seeds); 25 | } 26 | 27 | void set_seed(aug_state *state, uint64_t val) { 28 | mrg32k3a_seed(state->rng, val); 29 | } 30 | 31 | void set_seed_by_array(aug_state *state, uint64_t *vals, int count) { 32 | mrg32k3a_seed_by_array(state->rng, vals, count); 33 | } 34 | 35 | void init_state(aug_state *state, int64_t vals[6]) { 36 | state->rng->s1[0] = vals[0]; 37 | state->rng->s1[1] = vals[1]; 38 | state->rng->s1[2] = vals[2]; 39 | state->rng->s2[0] = vals[3]; 40 | state->rng->s2[1] = vals[4]; 41 | state->rng->s2[2] = vals[5]; 42 | state->rng->loc = 2; 43 | } 44 | -------------------------------------------------------------------------------- /randomstate/interface/mrg32k3a/mrg32k3a-shim.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #include "../../src/common/stdint.h" 3 | #else 4 | #include 5 | #endif 6 | 7 | #include "Python.h" 8 | #include "numpy/npy_common.h" 9 | 10 | #include "../../src/common/binomial.h" 11 | #include "../../src/common/entropy.h" 12 | #include "../../src/mrg32k3a/mrg32k3a.h" 13 | 14 | 15 | #define STATE_MAX_VALUE_1 4294967086 16 | #define STATE_MAX_VALUE_2 4294944442 17 | 18 | typedef struct s_aug_state { 19 | mrg32k3a_state *rng; 20 | binomial_t *binomial; 21 | 22 | int has_gauss, has_gauss_float, shift_zig_random_int, has_uint32; 23 | float gauss_float; 24 | double gauss; 25 | uint64_t zig_random_int; 26 | uint32_t uinteger; 27 | } aug_state; 28 | 29 | static inline uint32_t random_uint32(aug_state *state) { 30 | return mrg32k3a_random(state->rng); 31 | } 32 | 33 | static inline uint64_t random_uint64(aug_state *state) { 34 | return (((uint64_t)mrg32k3a_random(state->rng) << 32) | 35 | mrg32k3a_random(state->rng)); 36 | } 37 | 38 | static inline uint64_t random_raw_values(aug_state *state) { 39 | return (uint64_t)random_uint32(state); 40 | } 41 | 42 | static inline double random_double(aug_state *state) { 43 | int32_t a = random_uint32(state) >> 5, b = random_uint32(state) >> 6; 44 | return (a * 67108864.0 + b) / 9007199254740992.0; 45 | } 46 | 47 | extern void set_seed(aug_state *state, uint64_t val); 48 | 49 | extern void set_seed_by_array(aug_state *state, uint64_t *vals, int count); 50 | 51 | extern void init_state(aug_state *state, int64_t vals[6]); 52 | 53 | extern void entropy_init(aug_state *state); 54 | -------------------------------------------------------------------------------- /randomstate/interface/pcg-32/pcg-32-shim.c: -------------------------------------------------------------------------------- 1 | #include "pcg-32-shim.h" 2 | 3 | extern NPY_INLINE uint32_t random_uint32(aug_state *state); 4 | 5 | extern NPY_INLINE uint64_t random_uint64(aug_state *state); 6 | 7 | extern NPY_INLINE double random_double(aug_state *state); 8 | 9 | extern NPY_INLINE uint64_t random_raw_values(aug_state *state); 10 | 11 | extern NPY_INLINE void set_seed(aug_state *state, uint64_t seed, uint64_t inc); 12 | 13 | extern NPY_INLINE void advance_state(aug_state *state, uint64_t delta); 14 | 15 | extern NPY_INLINE void entropy_init(aug_state *state); 16 | -------------------------------------------------------------------------------- /randomstate/interface/pcg-32/pcg-32-shim.h: -------------------------------------------------------------------------------- 1 | #include "Python.h" 2 | #include "numpy/npy_common.h" 3 | 4 | #include "../../src/common/binomial.h" 5 | #include "../../src/common/entropy.h" 6 | #include 7 | 8 | /* #include "../../src/pcg/pcg_variants.h" */ 9 | #include "../../src/pcg/pcg32.h" 10 | 11 | typedef struct s_aug_state { 12 | pcg32_random_t *rng; 13 | binomial_t *binomial; 14 | 15 | int has_gauss, has_gauss_float, shift_zig_random_int, has_uint32; 16 | float gauss_float; 17 | double gauss; 18 | uint32_t uinteger; 19 | uint64_t zig_random_int; 20 | } aug_state; 21 | 22 | static NPY_INLINE uint32_t random_uint32(aug_state *state) { 23 | return pcg32_random_r(state->rng); 24 | } 25 | 26 | static NPY_INLINE uint64_t random_uint64(aug_state *state) { 27 | return (((uint64_t)pcg32_random_r((*state).rng) << 32) | 28 | pcg32_random_r((*state).rng)); 29 | } 30 | 31 | static NPY_INLINE void set_seed(aug_state *state, uint64_t seed, uint64_t inc) { 32 | pcg32_srandom_r(state->rng, seed, inc); 33 | } 34 | 35 | static NPY_INLINE void advance_state(aug_state *state, uint64_t delta) { 36 | pcg32_advance_r(state->rng, delta); 37 | } 38 | 39 | static NPY_INLINE uint64_t random_raw_values(aug_state *state) { 40 | return (uint64_t)random_uint32(state); 41 | } 42 | 43 | static NPY_INLINE void entropy_init(aug_state *state) { 44 | uint64_t seeds[2]; 45 | entropy_fill((void *)seeds, sizeof(seeds)); 46 | set_seed(state, seeds[0], seeds[1]); 47 | } 48 | 49 | static NPY_INLINE double random_double(aug_state *state) { 50 | int32_t a = random_uint32(state) >> 5, b = random_uint32(state) >> 6; 51 | return (a * 67108864.0 + b) / 9007199254740992.0; 52 | } -------------------------------------------------------------------------------- /randomstate/interface/pcg-64/pcg-64-docstring.pxi: -------------------------------------------------------------------------------- 1 | DEF CLASS_DOCSTRING = u""" 2 | RandomState(seed=None) 3 | 4 | Container for the PCG-64 pseudo-random number generator. 5 | 6 | PCG-64 is a 128-bit implementation of O'Neill's permutation congruential 7 | generator ([1]_, [2]_). PCG-64 has a period of :math:`2^{128}` and supports advancing 8 | an arbitrary number of steps as well as :math:`2^{127}` streams. 9 | 10 | ``pcg64.RandomState`` exposes a number of methods for generating random 11 | numbers drawn from a variety of probability distributions. In addition to the 12 | distribution-specific arguments, each method takes a keyword argument 13 | `size` that defaults to ``None``. If `size` is ``None``, then a single 14 | value is generated and returned. If `size` is an integer, then a 1-D 15 | array filled with generated values is returned. If `size` is a tuple, 16 | then an array with that shape is filled and returned. 17 | 18 | **No Compatibility Guarantee** 19 | 20 | ``pcg64.RandomState`` does not make a guarantee that a fixed seed and a 21 | fixed series of calls to ``pcg64.RandomState`` methods using the same 22 | parameters will always produce the same results. This is different from 23 | ``numpy.random.RandomState`` guarantee. This is done to simplify improving 24 | random number generators. To ensure identical results, you must use the 25 | same release version. 26 | 27 | Parameters 28 | ---------- 29 | seed : {None, long}, optional 30 | Random seed initializing the pseudo-random number generator. 31 | Can be an integer in [0, 2**128] or ``None`` (the default). 32 | If `seed` is ``None``, then ``pcg64.RandomState`` will try to read data 33 | from ``/dev/urandom`` (or the Windows analog) if available. If 34 | unavailable, a 64-bit hash of the time and process ID is used. 35 | inc : {None, int}, optional 36 | Stream to return. 37 | Can be an integer in [0, 2**128] or ``None`` (the default). If `inc` is 38 | ``None``, then 1 is used. Can be used with the same seed to 39 | produce multiple streams using other values of inc. 40 | 41 | Notes 42 | ----- 43 | Supports the method advance to advance the PRNG an arbitrary number of steps. 44 | The state of the PCG-64 PRNG is represented by 2 128-bit unsigned integers. 45 | 46 | See pcg32 for a similar implementation with a smaller period. 47 | 48 | **Parallel Features** 49 | 50 | ``pcg64.RandomState`` can be used in parallel applications in one of two ways. 51 | The preferable method is to use sub-streams, which are generated by using the 52 | same value of ``seed`` and incrementing the second value, ``inc``. 53 | 54 | >>> import randomstate.prng.pcg64 as rnd 55 | >>> rs = [rng.RandomState(1234, i + 1) for i in range(10)] 56 | 57 | The alternative method is to call ``advance`` on a single RandomState to 58 | produce non-overlapping sequences. 59 | 60 | >>> import randomstate.prng.pcg64 as rnd 61 | >>> rs = [rng.RandomState(1234, 1) for _ in range(10)] 62 | >>> for i in range(10): 63 | rs[i].advance(i * 2**64) 64 | 65 | **State and Seeding** 66 | 67 | The ``pcg64.RandomState`` state vector consists of 2 unsigned 128-bit values, 68 | which are represented externally as python longs (2.x) or ints (Python 3+). 69 | ``pcg64.RandomState`` is seeded using a single 128-bit unsigned integer 70 | (Python long/int). In addition, a second 128-bit unsigned integer is used 71 | to set the stream. 72 | 73 | References 74 | ---------- 75 | .. [1] "PCG, A Family of Better Random Number Generators", 76 | http://www.pcg-random.org/ 77 | .. [2] O'Neill, Melissa E. "PCG: A Family of Simple Fast Space-Efficient 78 | Statistically Good Algorithms for Random Number Generation" 79 | """ 80 | -------------------------------------------------------------------------------- /randomstate/interface/pcg-64/pcg-64-emulated.pxi: -------------------------------------------------------------------------------- 1 | DEF RS_RNG_NAME = u'pcg64' 2 | DEF RS_RNG_ADVANCEABLE = 1 3 | DEF RS_RNG_SEED=2 4 | DEF RS_PCG128_EMULATED = 1 5 | DEF RS_SEED_NBYTES = 4 6 | 7 | from cpython cimport PyLong_FromUnsignedLongLong, PyLong_AsUnsignedLongLong 8 | 9 | cdef extern from "distributions.h": 10 | 11 | ctypedef struct pcg128_t: 12 | uint64_t high 13 | uint64_t low 14 | 15 | cdef struct pcg_state_setseq_128: 16 | pcg128_t state 17 | pcg128_t inc 18 | 19 | ctypedef pcg_state_setseq_128 pcg64_random_t 20 | 21 | cdef struct s_aug_state: 22 | pcg64_random_t *rng 23 | binomial_t *binomial 24 | 25 | int has_gauss, shift_zig_random_int, has_uint32, has_gauss_float 26 | float gauss_float 27 | double gauss 28 | uint64_t zig_random_int 29 | uint32_t uinteger 30 | 31 | ctypedef s_aug_state aug_state 32 | 33 | cdef void advance_state(aug_state* state, pcg128_t delta) 34 | 35 | cdef void set_seed(aug_state* state, pcg128_t seed, pcg128_t inc) 36 | 37 | ctypedef pcg128_t rng_state_t 38 | 39 | ctypedef pcg64_random_t rng_t 40 | 41 | cdef object pcg128_to_pylong(pcg128_t x): 42 | return PyLong_FromUnsignedLongLong(x.high) * 2**64 + PyLong_FromUnsignedLongLong(x.low) 43 | 44 | cdef pcg128_t pcg128_from_pylong(object x) except *: 45 | cdef pcg128_t out 46 | out.high = PyLong_AsUnsignedLongLong(x // (2 ** 64)) 47 | out.low = PyLong_AsUnsignedLongLong(x % (2 ** 64)) 48 | return out 49 | 50 | cdef object _get_state(aug_state state): 51 | return (pcg128_to_pylong(state.rng.state), pcg128_to_pylong(state.rng.inc)) 52 | 53 | cdef object _set_state(aug_state *state, object state_info): 54 | state.rng.state = pcg128_from_pylong(state_info[0]) 55 | state.rng.inc = pcg128_from_pylong(state_info[1]) 56 | 57 | include "pcg-64-docstring.pxi" 58 | -------------------------------------------------------------------------------- /randomstate/interface/pcg-64/pcg-64-shim.c: -------------------------------------------------------------------------------- 1 | #include "pcg-64-shim.h" 2 | #include "../../src/splitmix64/splitmix64.h" 3 | 4 | extern inline uint32_t random_uint32(aug_state *state); 5 | 6 | extern inline uint64_t random_uint64(aug_state *state); 7 | 8 | extern inline double random_double(aug_state *state); 9 | 10 | extern inline uint64_t random_raw_values(aug_state *state); 11 | 12 | extern inline void advance_state(aug_state *state, pcg128_t delta); 13 | 14 | extern inline void entropy_init(aug_state *state); 15 | 16 | extern inline void set_seed(aug_state *state, pcg128_t seed, pcg128_t inc); 17 | -------------------------------------------------------------------------------- /randomstate/interface/pcg-64/pcg-64-shim.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #ifndef _INTTYPES 3 | #include "../common/inttypes.h" 4 | #endif 5 | #define inline __forceinline 6 | #else 7 | #include 8 | #endif 9 | 10 | #include "../../src/common/binomial.h" 11 | #include "../../src/common/entropy.h" 12 | #include "../../src/pcg64-compat/pcg64.h" 13 | 14 | typedef struct s_aug_state { 15 | pcg64_random_t *rng; 16 | binomial_t *binomial; 17 | 18 | int has_gauss, has_gauss_float, shift_zig_random_int, has_uint32; 19 | float gauss_float; 20 | double gauss; 21 | uint32_t uinteger; 22 | uint64_t zig_random_int; 23 | } aug_state; 24 | 25 | static inline uint32_t random_uint32(aug_state *state) { 26 | uint64_t temp; 27 | if (state->has_uint32) { 28 | state->has_uint32 = 0; 29 | return state->uinteger; 30 | } 31 | state->has_uint32 = 1; 32 | temp = pcg64_random_r(state->rng); 33 | state->uinteger = (uint32_t)(temp >> 32); 34 | return (uint32_t)(temp & 0xFFFFFFFFLL); 35 | } 36 | 37 | static inline uint64_t random_uint64(aug_state *state) { 38 | return pcg64_random_r(state->rng); 39 | } 40 | 41 | static inline uint64_t random_raw_values(aug_state *state) { 42 | return random_uint64(state); 43 | } 44 | 45 | static inline void set_seed(aug_state *state, pcg128_t seed, pcg128_t inc) { 46 | pcg64_srandom_r(state->rng, seed, inc); 47 | } 48 | 49 | static inline void advance_state(aug_state *state, pcg128_t delta) { 50 | pcg64_advance_r(state->rng, delta); 51 | } 52 | 53 | static inline void entropy_init(aug_state *state) { 54 | pcg128_t seeds[2]; 55 | entropy_fill((void *)seeds, sizeof(seeds)); 56 | set_seed(state, seeds[0], seeds[1]); 57 | } 58 | 59 | static inline double random_double(aug_state *state) { 60 | return (random_uint64(state) >> 11) * (1.0 / 9007199254740992.0); 61 | } 62 | -------------------------------------------------------------------------------- /randomstate/interface/pcg-64/pcg-64.pxi: -------------------------------------------------------------------------------- 1 | DEF RS_RNG_NAME = u'pcg64' 2 | DEF RS_RNG_ADVANCEABLE = 1 3 | DEF RS_RNG_SEED=2 4 | DEF RS_PCG128_EMULATED = 0 5 | DEF RS_SEED_NBYTES = 4 6 | 7 | cdef extern from "inttypes.h": 8 | ctypedef unsigned long long __uint128_t 9 | 10 | cdef extern from "distributions.h": 11 | ctypedef __uint128_t pcg128_t 12 | 13 | cdef struct pcg_state_setseq_128: 14 | pcg128_t state 15 | pcg128_t inc 16 | 17 | ctypedef pcg_state_setseq_128 pcg64_random_t 18 | 19 | cdef struct s_aug_state: 20 | pcg64_random_t *rng 21 | binomial_t *binomial 22 | 23 | int has_gauss, shift_zig_random_int, has_uint32, has_gauss_float 24 | float gauss_float 25 | double gauss 26 | uint64_t zig_random_int 27 | uint32_t uinteger 28 | 29 | ctypedef s_aug_state aug_state 30 | 31 | cdef void advance_state(aug_state* state, pcg128_t delta) 32 | 33 | cdef void set_seed(aug_state* state, pcg128_t seed, pcg128_t inc) 34 | 35 | ctypedef pcg128_t rng_state_t 36 | 37 | ctypedef pcg64_random_t rng_t 38 | 39 | cdef object _get_state(aug_state state): 40 | return (state.rng.state, state.rng.inc) 41 | 42 | cdef object _set_state(aug_state *state, object state_info): 43 | state.rng.state = state_info[0] 44 | state.rng.inc = state_info[1] 45 | 46 | include "pcg-64-docstring.pxi" -------------------------------------------------------------------------------- /randomstate/interface/random-kit/random-kit-shim.c: -------------------------------------------------------------------------------- 1 | #include "random-kit-shim.h" 2 | #include "random-kit-poly.h" 3 | 4 | extern NPY_INLINE uint32_t random_uint32(aug_state *state); 5 | 6 | extern NPY_INLINE uint64_t random_uint64(aug_state *state); 7 | 8 | extern NPY_INLINE uint64_t random_raw_values(aug_state *state); 9 | 10 | extern NPY_INLINE double random_double(aug_state *state); 11 | 12 | void set_seed_by_array(aug_state *state, uint32_t *init_key, int key_length) { 13 | randomkit_init_by_array(state->rng, init_key, key_length); 14 | } 15 | 16 | void set_seed(aug_state *state, uint32_t seed) { 17 | randomkit_seed(state->rng, seed); 18 | } 19 | 20 | void entropy_init(aug_state *state) { 21 | uint32_t seeds[1]; 22 | entropy_fill((void *)seeds, sizeof(seeds)); 23 | set_seed(state, seeds[0]); 24 | } 25 | 26 | void jump_state(aug_state *state) { randomkit_jump(state->rng, poly); } -------------------------------------------------------------------------------- /randomstate/interface/random-kit/random-kit-shim.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #include "../../src/common/stdint.h" 3 | #else 4 | #include 5 | #endif 6 | 7 | #include "Python.h" 8 | #include "numpy/npy_common.h" 9 | 10 | #include "../../src/common/binomial.h" 11 | #include "../../src/common/entropy.h" 12 | #include "../../src/random-kit/random-kit-jump.h" 13 | #include "../../src/random-kit/random-kit.h" 14 | 15 | typedef struct s_aug_state { 16 | randomkit_state *rng; 17 | binomial_t *binomial; 18 | 19 | int has_gauss, has_gauss_float, shift_zig_random_int, has_uint32; 20 | float gauss_float; 21 | double gauss; 22 | uint32_t uinteger; 23 | uint64_t zig_random_int; 24 | } aug_state; 25 | 26 | static inline uint32_t random_uint32(aug_state *state) { 27 | return randomkit_random(state->rng); 28 | } 29 | 30 | static inline uint64_t random_uint64(aug_state *state) { 31 | return (((uint64_t)randomkit_random(state->rng)) << 32) | 32 | randomkit_random(state->rng); 33 | } 34 | 35 | static inline double random_double(aug_state *state) { 36 | int32_t a = random_uint32(state) >> 5, b = random_uint32(state) >> 6; 37 | return (a * 67108864.0 + b) / 9007199254740992.0; 38 | } 39 | 40 | static inline uint64_t random_raw_values(aug_state *state) { 41 | return (uint64_t)random_uint32(state); 42 | } 43 | 44 | extern void entropy_init(aug_state *state); 45 | 46 | extern void set_seed_by_array(aug_state *state, uint32_t *init_key, 47 | int key_length); 48 | 49 | extern void set_seed(aug_state *state, uint32_t seed); 50 | 51 | extern void jump_state(aug_state *state); 52 | -------------------------------------------------------------------------------- /randomstate/interface/sfmt/sfmt-shim.c: -------------------------------------------------------------------------------- 1 | #include "sfmt-shim.h" 2 | #include "sfmt-poly.h" 3 | 4 | extern NPY_INLINE uint32_t random_uint32(aug_state *state); 5 | 6 | extern NPY_INLINE uint64_t random_uint64(aug_state *state); 7 | 8 | extern NPY_INLINE double random_double(aug_state *state); 9 | 10 | extern NPY_INLINE uint64_t random_raw_values(aug_state *state); 11 | 12 | void reset_buffer(aug_state *state) { 13 | int i = 0; 14 | for (i = 0; i < (2 * SFMT_N); i++) { 15 | state->buffered_uint64[i] = 0ULL; 16 | } 17 | state->buffer_loc = 2 * SFMT_N; 18 | } 19 | 20 | extern void set_seed_by_array(aug_state *state, uint32_t init_key[], 21 | int key_length) { 22 | reset_buffer(state); 23 | sfmt_init_by_array(state->rng, init_key, key_length); 24 | } 25 | 26 | void set_seed(aug_state *state, uint32_t seed) { 27 | reset_buffer(state); 28 | sfmt_init_gen_rand(state->rng, seed); 29 | } 30 | 31 | void entropy_init(aug_state *state) { 32 | uint32_t seeds[1]; 33 | entropy_fill((void *)seeds, sizeof(seeds)); 34 | set_seed(state, seeds[0]); 35 | } 36 | 37 | void jump_state(aug_state *state) { SFMT_jump(state->rng, poly_128); } -------------------------------------------------------------------------------- /randomstate/interface/sfmt/sfmt-shim.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #include "../../src/common/stdint.h" 3 | #else 4 | #include 5 | #endif 6 | 7 | #include "Python.h" 8 | #include "numpy/npy_common.h" 9 | 10 | #include "../../src/common/binomial.h" 11 | #include "../../src/common/entropy.h" 12 | #include "../../src/sfmt/sfmt-jump.h" 13 | #include "../../src/sfmt/sfmt.h" 14 | 15 | typedef struct s_aug_state { 16 | sfmt_t *rng; 17 | binomial_t *binomial; 18 | 19 | int has_gauss, has_gauss_float, shift_zig_random_int, has_uint32; 20 | float gauss_float; 21 | double gauss; 22 | uint32_t uinteger; 23 | uint64_t zig_random_int; 24 | 25 | uint64_t *buffered_uint64; 26 | int buffer_loc; 27 | } aug_state; 28 | 29 | static NPY_INLINE uint64_t random_uint64_from_buffer(aug_state *state) { 30 | uint64_t out; 31 | if (state->buffer_loc >= (2 * SFMT_N)) { 32 | state->buffer_loc = 0; 33 | sfmt_fill_array64(state->rng, state->buffered_uint64, 2 * SFMT_N); 34 | } 35 | out = state->buffered_uint64[state->buffer_loc]; 36 | state->buffer_loc++; 37 | return out; 38 | } 39 | 40 | static NPY_INLINE uint32_t random_uint32(aug_state *state) { 41 | uint64_t d; 42 | if (state->has_uint32) { 43 | state->has_uint32 = 0; 44 | return state->uinteger; 45 | } 46 | d = random_uint64_from_buffer(state); 47 | state->uinteger = (uint32_t)(d >> 32); 48 | state->has_uint32 = 1; 49 | return (uint32_t)(d & 0xFFFFFFFFUL); 50 | } 51 | 52 | static NPY_INLINE uint64_t random_uint64(aug_state *state) { 53 | return random_uint64_from_buffer(state); 54 | } 55 | 56 | static NPY_INLINE double random_double(aug_state *state) { 57 | return (random_uint64_from_buffer(state) >> 11) * (1.0 / 9007199254740992.0); 58 | } 59 | 60 | static NPY_INLINE uint64_t random_raw_values(aug_state *state) { 61 | return random_uint64_from_buffer(state); 62 | } 63 | 64 | extern void entropy_init(aug_state *state); 65 | 66 | extern void set_seed_by_array(aug_state *state, uint32_t init_key[], 67 | int key_length); 68 | 69 | extern void set_seed(aug_state *state, uint32_t seed); 70 | 71 | extern void jump_state(aug_state *state); -------------------------------------------------------------------------------- /randomstate/interface/xoroshiro128plus/xoroshiro128plus-shim.c: -------------------------------------------------------------------------------- 1 | #include "xoroshiro128plus-shim.h" 2 | 3 | extern NPY_INLINE uint32_t random_uint32(aug_state *state); 4 | 5 | extern NPY_INLINE uint64_t random_uint64(aug_state *state); 6 | 7 | extern NPY_INLINE double random_double(aug_state *state); 8 | 9 | extern NPY_INLINE uint64_t random_raw_values(aug_state *state); 10 | 11 | void set_seed(aug_state *state, uint64_t seed) { 12 | xoroshiro128plus_seed(state->rng, seed); 13 | } 14 | 15 | void set_seed_by_array(aug_state *state, uint64_t *vals, int count) { 16 | xoroshiro128plus_seed_by_array(state->rng, vals, count); 17 | } 18 | 19 | void entropy_init(aug_state *state) { 20 | uint64_t seed[1]; 21 | entropy_fill((void *)seed, sizeof(seed)); 22 | xoroshiro128plus_seed(state->rng, seed[0]); 23 | } 24 | 25 | void jump_state(aug_state *state) { xoroshiro128plus_jump(state->rng); } 26 | 27 | void init_state(aug_state *state, uint64_t *state_vals) { 28 | xoroshiro128plus_init_state(state->rng, *(state_vals), *(state_vals + 1)); 29 | } 30 | -------------------------------------------------------------------------------- /randomstate/interface/xoroshiro128plus/xoroshiro128plus-shim.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #include "../../src/common/stdint.h" 3 | #else 4 | #include 5 | #endif 6 | 7 | #include "Python.h" 8 | #include "numpy/npy_common.h" 9 | 10 | #include "../../src/common/binomial.h" 11 | #include "../../src/common/entropy.h" 12 | #include "../../src/xoroshiro128plus/xoroshiro128plus.h" 13 | 14 | typedef struct s_aug_state { 15 | xoroshiro128plus_state *rng; 16 | binomial_t *binomial; 17 | 18 | int has_gauss, has_gauss_float, shift_zig_random_int, has_uint32; 19 | float gauss_float; 20 | double gauss; 21 | uint32_t uinteger; 22 | uint64_t zig_random_int; 23 | 24 | } aug_state; 25 | 26 | static inline uint32_t random_uint32(aug_state *state) { 27 | uint64_t temp; 28 | if (state->has_uint32) { 29 | state->has_uint32 = 0; 30 | return state->uinteger; 31 | } 32 | state->has_uint32 = 1; 33 | temp = xoroshiro128plus_next(state->rng); 34 | state->uinteger = (uint32_t)(temp >> 32); 35 | return (uint32_t)(temp & 0xFFFFFFFFLL); 36 | } 37 | 38 | static inline uint64_t random_uint64(aug_state *state) { 39 | return xoroshiro128plus_next(state->rng); 40 | } 41 | 42 | static inline uint64_t random_raw_values(aug_state *state) { 43 | return random_uint64(state); 44 | } 45 | 46 | static inline double random_double(aug_state *state) { 47 | uint64_t rn; 48 | rn = random_uint64(state); 49 | return (rn >> 11) * (1.0 / 9007199254740992.0); 50 | } 51 | 52 | extern void set_seed(aug_state *state, uint64_t seed); 53 | 54 | extern void set_seed_by_array(aug_state *state, uint64_t *vals, int count); 55 | 56 | extern void jump_state(aug_state *state); 57 | 58 | extern void entropy_init(aug_state *state); 59 | -------------------------------------------------------------------------------- /randomstate/interface/xorshift1024/xorshift1024-shim.c: -------------------------------------------------------------------------------- 1 | #include "xorshift1024-shim.h" 2 | 3 | extern NPY_INLINE uint32_t random_uint32(aug_state *state); 4 | 5 | extern NPY_INLINE uint64_t random_uint64(aug_state *state); 6 | 7 | extern NPY_INLINE double random_double(aug_state *state); 8 | 9 | extern NPY_INLINE uint64_t random_raw_values(aug_state *state); 10 | 11 | void set_seed(aug_state *state, uint64_t seed) { 12 | xorshift1024_seed(state->rng, seed); 13 | } 14 | 15 | void set_seed_by_array(aug_state *state, uint64_t *vals, int count) { 16 | xorshift1024_seed_by_array(state->rng, vals, count); 17 | } 18 | 19 | void entropy_init(aug_state *state) { 20 | uint64_t seed[1]; 21 | entropy_fill((void *)seed, sizeof(seed)); 22 | xorshift1024_seed(state->rng, seed[0]); 23 | } 24 | 25 | void jump_state(aug_state *state) { xorshift1024_jump(state->rng); } 26 | 27 | void init_state(aug_state *state, uint64_t *state_value) { 28 | xorshift1024_init_state(state->rng, state_value); 29 | } 30 | -------------------------------------------------------------------------------- /randomstate/interface/xorshift1024/xorshift1024-shim.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #include "../../src/common/stdint.h" 3 | #else 4 | #include 5 | #endif 6 | 7 | #include "Python.h" 8 | #include "numpy/npy_common.h" 9 | 10 | #include "../../src/common/binomial.h" 11 | #include "../../src/common/entropy.h" 12 | #include "../../src/xorshift1024/xorshift1024.h" 13 | 14 | typedef struct s_aug_state { 15 | xorshift1024_state *rng; 16 | binomial_t *binomial; 17 | 18 | int has_gauss, has_gauss_float, shift_zig_random_int, has_uint32; 19 | float gauss_float; 20 | double gauss; 21 | uint32_t uinteger; 22 | uint64_t zig_random_int; 23 | 24 | } aug_state; 25 | 26 | static inline uint32_t random_uint32(aug_state *state) { 27 | uint64_t temp; 28 | if (state->has_uint32) { 29 | state->has_uint32 = 0; 30 | return state->uinteger; 31 | } 32 | state->has_uint32 = 1; 33 | temp = xorshift1024_next(state->rng); 34 | state->uinteger = (uint32_t)(temp >> 32); 35 | return (uint32_t)(temp & 0xFFFFFFFFLL); 36 | } 37 | 38 | static inline uint64_t random_uint64(aug_state *state) { 39 | return xorshift1024_next(state->rng); 40 | } 41 | 42 | static inline uint64_t random_raw_values(aug_state *state) { 43 | return random_uint64(state); 44 | } 45 | 46 | static inline double random_double(aug_state *state) { 47 | return (random_uint64(state) >> 11) * (1.0 / 9007199254740992.0); 48 | } 49 | 50 | extern void set_seed(aug_state *state, uint64_t seed); 51 | 52 | extern void set_seed_by_array(aug_state *state, uint64_t *vals, int count); 53 | 54 | extern void jump_state(aug_state *state); 55 | 56 | extern void entropy_init(aug_state *state); 57 | 58 | extern void init_state(aug_state *state, uint64_t *state_value); 59 | -------------------------------------------------------------------------------- /randomstate/interface/xorshift128/xorshift128-shim.c: -------------------------------------------------------------------------------- 1 | #include "xorshift128-shim.h" 2 | 3 | extern NPY_INLINE uint32_t random_uint32(aug_state *state); 4 | 5 | extern NPY_INLINE uint64_t random_uint64(aug_state *state); 6 | 7 | extern NPY_INLINE double random_double(aug_state *state); 8 | 9 | extern NPY_INLINE uint64_t random_raw_values(aug_state *state); 10 | 11 | void set_seed(aug_state *state, uint64_t seed) { 12 | xorshift128_seed(state->rng, seed); 13 | } 14 | 15 | void set_seed_by_array(aug_state *state, uint64_t *vals, int count) { 16 | xorshift128_seed_by_array(state->rng, vals, count); 17 | } 18 | 19 | void entropy_init(aug_state *state) { 20 | uint64_t seed[1]; 21 | entropy_fill((void *)seed, sizeof(seed)); 22 | xorshift128_seed(state->rng, seed[0]); 23 | } 24 | 25 | void jump_state(aug_state *state) { xorshift128_jump(state->rng); } 26 | 27 | void init_state(aug_state *state, uint64_t *state_vals) { 28 | xorshift128_init_state(state->rng, *(state_vals), *(state_vals + 1)); 29 | } 30 | -------------------------------------------------------------------------------- /randomstate/interface/xorshift128/xorshift128-shim.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #include "../../src/common/stdint.h" 3 | #else 4 | #include 5 | #endif 6 | 7 | #include "Python.h" 8 | #include "numpy/npy_common.h" 9 | 10 | #include "../../src/common/binomial.h" 11 | #include "../../src/common/entropy.h" 12 | #include "../../src/xorshift128/xorshift128.h" 13 | 14 | typedef struct s_aug_state { 15 | xorshift128_state *rng; 16 | binomial_t *binomial; 17 | 18 | int has_gauss, has_gauss_float, shift_zig_random_int, has_uint32; 19 | float gauss_float; 20 | double gauss; 21 | uint32_t uinteger; 22 | uint64_t zig_random_int; 23 | 24 | } aug_state; 25 | 26 | static inline uint32_t random_uint32(aug_state *state) { 27 | uint64_t temp; 28 | if (state->has_uint32) { 29 | state->has_uint32 = 0; 30 | return state->uinteger; 31 | } 32 | state->has_uint32 = 1; 33 | temp = xorshift128_next(state->rng); 34 | state->uinteger = (uint32_t)(temp >> 32); 35 | return (uint32_t)(temp & 0xFFFFFFFFLL); 36 | } 37 | 38 | static inline uint64_t random_uint64(aug_state *state) { 39 | return xorshift128_next(state->rng); 40 | } 41 | 42 | static inline uint64_t random_raw_values(aug_state *state) { 43 | return random_uint64(state); 44 | } 45 | 46 | static inline double random_double(aug_state *state) { 47 | return (random_uint64(state) >> 11) * (1.0 / 9007199254740992.0); 48 | } 49 | 50 | extern void set_seed(aug_state *state, uint64_t seed); 51 | 52 | extern void set_seed_by_array(aug_state *state, uint64_t *vals, int count); 53 | 54 | extern void jump_state(aug_state *state); 55 | 56 | extern void entropy_init(aug_state *state); 57 | -------------------------------------------------------------------------------- /randomstate/performance.py: -------------------------------------------------------------------------------- 1 | import os 2 | import struct 3 | import timeit 4 | 5 | import numpy as np 6 | import pandas as pd 7 | from numpy.random import RandomState 8 | 9 | rs = RandomState() 10 | 11 | SETUP = ''' 12 | import numpy as np 13 | import {mod}.{rng} 14 | rs = {mod}.{rng}.RandomState() 15 | rs.random_sample() 16 | ''' 17 | 18 | scale_32 = scale_64 = 1 19 | if struct.calcsize('P') == 8 and os.name != 'nt': 20 | # 64 bit 21 | scale_32 = 0.5 22 | else: 23 | scale_64 = 2 24 | 25 | RNGS = ['mlfg_1279_861', 'mrg32k3a', 'pcg64', 'pcg32', 'mt19937', 'xorshift128', 'xorshift1024', 26 | 'xoroshiro128plus', 'dsfmt', 'sfmt', 'random'] 27 | 28 | 29 | def timer(code, setup): 30 | return 1000 * min(timeit.Timer(code, setup=setup).repeat(10, 10)) / 10.0 31 | 32 | 33 | def print_legend(legend): 34 | print('\n' + legend + '\n' + '*' * max(60, len(legend))) 35 | 36 | 37 | def run_timer(dist, command, numpy_command=None, setup='', random_type=''): 38 | print('-' * 80) 39 | if numpy_command is None: 40 | numpy_command = command 41 | 42 | res = {} 43 | for rng in RNGS: 44 | mod = 'randomstate.prng' if rng != 'random' else 'numpy' 45 | key = '-'.join((mod, rng, dist)).replace('"', '') 46 | command = numpy_command if 'numpy' in mod else command 47 | res[key] = timer(command, setup=setup.format(mod=mod, rng=rng)) 48 | 49 | s = pd.Series(res) 50 | t = s.apply(lambda x: '{0:0.2f} ms'.format(x)) 51 | print_legend('Time to produce 1,000,000 ' + random_type) 52 | print(t.sort_index()) 53 | 54 | p = 1000.0 / s 55 | p = p.apply(lambda x: '{0:0.2f} million'.format(x)) 56 | print_legend(random_type + ' per second') 57 | print(p.sort_index()) 58 | 59 | baseline = [k for k in p.index if 'numpy' in k][0] 60 | p = 1000.0 / s 61 | p = p / p[baseline] * 100 - 100 62 | p = p.drop(baseline, 0) 63 | p = p.apply(lambda x: '{0:0.1f}%'.format(x)) 64 | print_legend('Speed-up relative to NumPy') 65 | print(p.sort_index()) 66 | print('-' * 80) 67 | 68 | 69 | def timer_raw(): 70 | dist = 'random_raw' 71 | command = 'rs.random_raw(size=1000000, output=False)' 72 | info = np.iinfo(np.int32) 73 | command_numpy = 'rs.random_integers({max},size=1000000)' 74 | command_numpy=command_numpy.format(max=info.max) 75 | run_timer(dist, command, command_numpy, SETUP, 'Standard normals (Ziggurat)') 76 | 77 | 78 | def timer_uniform(): 79 | dist = 'random_sample' 80 | command = 'rs.random_sample(1000000)' 81 | run_timer(dist, command, None, SETUP, 'Uniforms') 82 | 83 | 84 | def timer_32bit(): 85 | info = np.iinfo(np.uint32) 86 | min, max = info.min, info.max 87 | dist = 'random_uintegers' 88 | command = 'rs.random_uintegers(1000000, 32)' 89 | command_numpy = 'rs.randint({min}, {max}+1, 1000000, dtype=np.uint32)' 90 | command_numpy = command_numpy.format(min=min, max=max) 91 | run_timer(dist, command, command_numpy, SETUP, '32-bit unsigned integers') 92 | 93 | 94 | def timer_64bit(): 95 | info = np.iinfo(np.uint64) 96 | min, max = info.min, info.max 97 | dist = 'random_uintegers' 98 | command = 'rs.random_uintegers(1000000)' 99 | command_numpy = 'rs.randint({min}, {max}+1, 1000000, dtype=np.uint64)' 100 | command_numpy = command_numpy.format(min=min, max=max) 101 | run_timer(dist, command, command_numpy, SETUP, '64-bit unsigned integers') 102 | 103 | 104 | def timer_normal(): 105 | dist = 'standard_normal' 106 | command = 'rs.standard_normal(1000000, method="bm")' 107 | command_numpy = 'rs.standard_normal(1000000)' 108 | run_timer(dist, command, command_numpy, SETUP, 'Box-Muller normals') 109 | 110 | 111 | def timer_normal_zig(): 112 | dist = 'standard_normal' 113 | command = 'rs.standard_normal(1000000, method="zig")' 114 | command_numpy = 'rs.standard_normal(1000000)' 115 | run_timer(dist, command, command_numpy, SETUP, 'Standard normals (Ziggurat)') 116 | 117 | 118 | if __name__ == '__main__': 119 | timer_raw() 120 | timer_uniform() 121 | timer_32bit() 122 | timer_64bit() 123 | timer_normal() 124 | timer_normal_zig() 125 | -------------------------------------------------------------------------------- /randomstate/prng/__init__.py: -------------------------------------------------------------------------------- 1 | from .xorshift128 import xorshift128 2 | from .xoroshiro128plus import xoroshiro128plus 3 | from .xorshift1024 import xorshift1024 4 | from .mlfg_1279_861 import mlfg_1279_861 5 | from .mt19937 import mt19937 6 | from .mrg32k3a import mrg32k3a 7 | from .pcg32 import pcg32 8 | from .pcg64 import pcg64 9 | from .dsfmt import dsfmt 10 | from .sfmt import sfmt 11 | 12 | def __generic_ctor(mod_name='mt19937'): 13 | """ 14 | Pickling helper function that returns a mod_name.RandomState object 15 | 16 | Parameters 17 | ---------- 18 | mod_name: str 19 | String containing the module name 20 | 21 | Returns 22 | ------- 23 | rs: RandomState 24 | RandomState from the module randomstate.prng.mod_name 25 | """ 26 | try: 27 | mod_name = mod_name.decode('ascii') 28 | except AttributeError: 29 | pass 30 | if mod_name == 'mt19937': 31 | mod = mt19937 32 | elif mod_name == 'mlfg_1279_861': 33 | mod = mlfg_1279_861 34 | elif mod_name == 'mrg32k3a': 35 | mod = mrg32k3a 36 | elif mod_name == 'pcg32': 37 | mod = pcg32 38 | elif mod_name == 'pcg64': 39 | mod = pcg64 40 | elif mod_name == 'pcg32': 41 | mod = pcg32 42 | elif mod_name == 'xorshift128': 43 | mod = xorshift128 44 | elif mod_name == 'xoroshiro128plus': 45 | mod = xoroshiro128plus 46 | elif mod_name == 'xorshift1024': 47 | mod = xorshift1024 48 | elif mod_name == 'dsfmt': 49 | mod = dsfmt 50 | elif mod_name == 'sfmt': 51 | mod = sfmt 52 | else: 53 | raise ValueError(str(mod_name) + ' is not a known PRNG module.') 54 | 55 | return mod.RandomState(0) 56 | -------------------------------------------------------------------------------- /randomstate/prng/dsfmt/__init__.py: -------------------------------------------------------------------------------- 1 | from .dsfmt import * 2 | -------------------------------------------------------------------------------- /randomstate/prng/mlfg_1279_861/__init__.py: -------------------------------------------------------------------------------- 1 | from .mlfg_1279_861 import * -------------------------------------------------------------------------------- /randomstate/prng/mrg32k3a/__init__.py: -------------------------------------------------------------------------------- 1 | from .mrg32k3a import * -------------------------------------------------------------------------------- /randomstate/prng/mt19937/__init__.py: -------------------------------------------------------------------------------- 1 | from .mt19937 import * -------------------------------------------------------------------------------- /randomstate/prng/pcg32/__init__.py: -------------------------------------------------------------------------------- 1 | from .pcg32 import * -------------------------------------------------------------------------------- /randomstate/prng/pcg64/__init__.py: -------------------------------------------------------------------------------- 1 | from .pcg64 import * -------------------------------------------------------------------------------- /randomstate/prng/sfmt/__init__.py: -------------------------------------------------------------------------------- 1 | from .sfmt import * -------------------------------------------------------------------------------- /randomstate/prng/xoroshiro128plus/__init__.py: -------------------------------------------------------------------------------- 1 | from .xoroshiro128plus import * -------------------------------------------------------------------------------- /randomstate/prng/xorshift1024/__init__.py: -------------------------------------------------------------------------------- 1 | from .xorshift1024 import * -------------------------------------------------------------------------------- /randomstate/prng/xorshift128/__init__.py: -------------------------------------------------------------------------------- 1 | from .xorshift128 import * -------------------------------------------------------------------------------- /randomstate/setup-single-rng.py: -------------------------------------------------------------------------------- 1 | """ 2 | Build a single PRNG for testing/development 3 | 4 | Usage: 5 | 6 | python setup-single-rng.py build_ext --in-place -f 7 | """ 8 | import glob 9 | import os 10 | import sys 11 | from distutils.core import setup 12 | from distutils.extension import Extension 13 | from os import getcwd, name 14 | from os.path import join 15 | 16 | import Cython.Tempita as tempita 17 | import numpy 18 | from Cython.Build import cythonize 19 | 20 | rngs = ['RNG_PCG32', 'RNG_PCG64', 21 | 'RNG_MT19937', 'RNG_XORSHIFT128', 22 | 'RNG_XORSHIFT1024', 'RNG_MRG32K3A', 'RNG_MLFG_1279_861'] 23 | 24 | files = glob.glob('./*.in') 25 | for templated_file in files: 26 | output_file_name = os.path.splitext(templated_file)[0] 27 | if os.path.exists(output_file_name): 28 | if os.path.getmtime(templated_file) < os.path.getmtime(output_file_name): 29 | continue 30 | with open(templated_file, 'r') as source_file: 31 | template = tempita.Template(source_file.read()) 32 | with open(output_file_name, 'w') as output_file: 33 | output_file.write(template.substitute()) 34 | 35 | with open('config.pxi', 'w') as config: 36 | config.write('# Autogenerated\n\n') 37 | config.write("DEF RS_RNG_MOD_NAME='mt19937'\n") 38 | 39 | pwd = getcwd() 40 | 41 | sources = [join(pwd, 'randomstate.pyx'), 42 | join(pwd, 'src', 'common', 'entropy.c'), 43 | join(pwd, 'distributions.c')] 44 | 45 | sources += [join(pwd, 'src', 'random-kit', p) for p in ('random-kit.c','random-kit-jump.c')] 46 | sources += [join(pwd, 'interface', 'random-kit', 'random-kit-shim.c')] 47 | 48 | defs = [('RS_RANDOMKIT', '1')] 49 | 50 | include_dirs = [pwd] + [numpy.get_include()] 51 | if os.name == 'nt' and sys.version_info < (3, 5): 52 | include_dirs += [join(pwd, 'src', 'common')] 53 | include_dirs += [join(pwd, 'src', 'random-kit')] 54 | 55 | extra_link_args = ['Advapi32.lib'] if name == 'nt' else [] 56 | extra_compile_args = [] if name == 'nt' else ['-std=c99'] 57 | 58 | setup(ext_modules=cythonize([Extension("randomstate.randomstate", 59 | sources=sources, 60 | include_dirs=include_dirs, 61 | define_macros=defs, 62 | extra_compile_args=extra_compile_args, 63 | extra_link_args=extra_link_args) 64 | ]) 65 | ) 66 | -------------------------------------------------------------------------------- /randomstate/src/common/binomial.h: -------------------------------------------------------------------------------- 1 | typedef struct s_binomial_t 2 | { 3 | int has_binomial; /* !=0: following parameters initialized for binomial */ 4 | double psave; 5 | long nsave; 6 | double r; 7 | double q; 8 | double fm; 9 | long m; 10 | double p1; 11 | double xm; 12 | double xl; 13 | double xr; 14 | double c; 15 | double laml; 16 | double lamr; 17 | double p2; 18 | double p3; 19 | double p4; 20 | } binomial_t; 21 | -------------------------------------------------------------------------------- /randomstate/src/common/binomial.pxi: -------------------------------------------------------------------------------- 1 | cdef extern from "distributions.h": 2 | 3 | cdef struct s_binomial_t: 4 | int has_binomial 5 | double psave 6 | long nsave 7 | double r 8 | double q 9 | double fm 10 | long m 11 | double p1 12 | double xm 13 | double xl 14 | double xr 15 | double c 16 | double laml 17 | double lamr 18 | double p2 19 | double p3 20 | double p4 21 | 22 | ctypedef s_binomial_t binomial_t 23 | -------------------------------------------------------------------------------- /randomstate/src/common/entropy.h: -------------------------------------------------------------------------------- 1 | /* 2 | * PCG Random Number Generation for C. 3 | * 4 | * Copyright 2014 Melissa O'Neill 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * For additional information about the PCG random number generation scheme, 19 | * including its license and other licensing options, visit 20 | * 21 | * http://www.pcg-random.org 22 | */ 23 | #include 24 | #ifdef _WIN32 25 | #include "../common/stdint.h" 26 | typedef int bool; 27 | #define false 0 28 | #define true 1 29 | #else 30 | #include 31 | #include 32 | #endif 33 | 34 | extern void entropy_fill(void *dest, size_t size); 35 | 36 | extern bool entropy_getbytes(void* dest, size_t size); 37 | 38 | extern bool entropy_fallback_getbytes(void *dest, size_t size); -------------------------------------------------------------------------------- /randomstate/src/dSFMT/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2007, 2008, 2009 Mutsuo Saito, Makoto Matsumoto 2 | and Hiroshima University. 3 | Copyright (c) 2011, 2002 Mutsuo Saito, Makoto Matsumoto, Hiroshima 4 | University and The University of Tokyo. 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are 9 | met: 10 | 11 | * Redistributions of source code must retain the above copyright 12 | notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above 14 | copyright notice, this list of conditions and the following 15 | disclaimer in the documentation and/or other materials provided 16 | with the distribution. 17 | * Neither the name of the Hiroshima University nor the names of 18 | its contributors may be used to endorse or promote products 19 | derived from this software without specific prior written 20 | permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | -------------------------------------------------------------------------------- /randomstate/src/dSFMT/calc-jump.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file calc-jump.cpp 3 | * 4 | * @brief calc jump function. 5 | * 6 | * @author Mutsuo Saito (Hiroshima University) 7 | * @author Makoto Matsumoto (The University of Tokyo) 8 | * 9 | * Copyright (C) 2012 Mutsuo Saito, Makoto Matsumoto, 10 | * Hiroshima University and The University of Tokyo. 11 | * All rights reserved. 12 | * 13 | * The 3-clause BSD License is applied to this software, see 14 | * LICENSE.txt 15 | * 16 | * Compile: 17 | * g++ calc-jump.cpp -o calc-jump -lntl 18 | * 19 | * Compute polynomial for 2^128 steps: 20 | * ./calc-jump 340282366920938463463374607431768211456 poly.19937.txt 21 | * 22 | */ 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include "dSFMT-calc-jump.hpp" 35 | 36 | using namespace NTL; 37 | using namespace std; 38 | using namespace dsfmt; 39 | 40 | static void read_file(GF2X& lcmpoly, long line_no, const string& file); 41 | 42 | int main(int argc, char * argv[]) { 43 | if (argc <= 2) { 44 | cout << argv[0] << " jump-step poly-file" << endl; 45 | cout << " jump-step: a number between zero and 2^{DSFMT_MEXP}-1.\n" 46 | << " large decimal number is allowed." << endl; 47 | cout << " poly-file: one of poly.{MEXP}.txt " 48 | << "file" << endl; 49 | return -1; 50 | } 51 | string step_string = argv[1]; 52 | string filename = argv[2]; 53 | long no = 0; 54 | GF2X lcmpoly; 55 | read_file(lcmpoly, no, filename); 56 | ZZ step; 57 | stringstream ss(step_string); 58 | ss >> step; 59 | string jump_str; 60 | calc_jump(jump_str, step, lcmpoly); 61 | cout << "jump polynomial:" << endl; 62 | cout << jump_str << endl; 63 | return 0; 64 | } 65 | 66 | 67 | static void read_file(GF2X& lcmpoly, long line_no, const string& file) 68 | { 69 | ifstream ifs(file.c_str()); 70 | string line; 71 | for (int i = 0; i < line_no; i++) { 72 | ifs >> line; 73 | ifs >> line; 74 | } 75 | if (ifs) { 76 | ifs >> line; 77 | line = ""; 78 | ifs >> line; 79 | } 80 | stringtopoly(lcmpoly, line); 81 | } 82 | -------------------------------------------------------------------------------- /randomstate/src/dSFMT/dSFMT-calc-jump.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef DSFMT_CALC_JUMP_HPP 3 | #define DSFMT_CALC_JUMP_HPP 4 | /** 5 | * @file dSFMT-calc-jump.hpp 6 | * 7 | * @brief functions for calculating jump polynomial. 8 | * 9 | * @author Mutsuo Saito (Hiroshima University) 10 | * @author Makoto Matsumoto (The University of Tokyo) 11 | * 12 | * Copyright (C) 2012 Mutsuo Saito, Makoto Matsumoto, 13 | * Hiroshima University and The University of Tokyo. 14 | * All rights reserved. 15 | * 16 | * The 3-clause BSD License is applied to this software, see 17 | * LICENSE.txt 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | namespace dsfmt { 25 | /** 26 | * converts polynomial to string for convenient use in C language. 27 | * @param x output string 28 | * @param polynomial input polynomial 29 | */ 30 | static inline void polytostring(std::string& x, NTL::GF2X& polynomial) 31 | { 32 | using namespace NTL; 33 | using namespace std; 34 | 35 | long degree = deg(polynomial); 36 | int buff; 37 | stringstream ss; 38 | for (int i = 0; i <= degree; i+=4) { 39 | buff = 0; 40 | for (int j = 0; j < 4; j++) { 41 | if (IsOne(coeff(polynomial, i + j))) { 42 | buff |= 1 << j; 43 | } else { 44 | buff &= (0x0f ^ (1 << j)); 45 | } 46 | } 47 | ss << hex << buff; 48 | } 49 | ss << flush; 50 | x = ss.str(); 51 | } 52 | 53 | /** 54 | * converts string to polynomial 55 | * @param str string 56 | * @param poly output polynomial 57 | */ 58 | static inline void stringtopoly(NTL::GF2X& poly, std::string& str) 59 | { 60 | using namespace NTL; 61 | using namespace std; 62 | 63 | stringstream ss(str); 64 | char c; 65 | long p = 0; 66 | clear(poly); 67 | while(ss) { 68 | ss >> c; 69 | if (!ss) { 70 | break; 71 | } 72 | if (c >= 'a') { 73 | c = c - 'a' + 10; 74 | } else { 75 | c = c - '0'; 76 | } 77 | for (int j = 0; j < 4; j++) { 78 | if (c & (1 << j)) { 79 | SetCoeff(poly, p, 1); 80 | } else { 81 | SetCoeff(poly, p, 0); 82 | } 83 | p++; 84 | } 85 | } 86 | } 87 | 88 | /** 89 | * calculate the jump polynomial. 90 | * SFMT generates 4 32-bit integers from one internal state. 91 | * @param jump_str output string which represents jump polynomial. 92 | * @param step jump step of internal state 93 | * @param characteristic polynomial 94 | */ 95 | static inline void calc_jump(std::string& jump_str, 96 | NTL::ZZ& step, 97 | NTL::GF2X& characteristic) 98 | { 99 | using namespace NTL; 100 | using namespace std; 101 | GF2X jump; 102 | PowerXMod(jump, step, characteristic); 103 | polytostring(jump_str, jump); 104 | } 105 | } 106 | #endif 107 | -------------------------------------------------------------------------------- /randomstate/src/dSFMT/dSFMT-common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /** 3 | * @file dSFMT-common.h 4 | * 5 | * @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom 6 | * number generator with jump function. This file includes common functions 7 | * used in random number generation and jump. 8 | * 9 | * @author Mutsuo Saito (Hiroshima University) 10 | * @author Makoto Matsumoto (The University of Tokyo) 11 | * 12 | * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima 13 | * University. 14 | * Copyright (C) 2012 Mutsuo Saito, Makoto Matsumoto, Hiroshima 15 | * University and The University of Tokyo. 16 | * All rights reserved. 17 | * 18 | * The 3-clause BSD License is applied to this software, see 19 | * LICENSE.txt 20 | */ 21 | #ifndef DSFMT_COMMON_H 22 | #define DSFMT_COMMON_H 23 | 24 | #include "dSFMT.h" 25 | 26 | #if defined(HAVE_SSE2) 27 | # include 28 | union X128I_T { 29 | uint64_t u[2]; 30 | __m128i i128; 31 | }; 32 | union X128D_T { 33 | double d[2]; 34 | __m128d d128; 35 | }; 36 | /** mask data for sse2 */ 37 | static const union X128I_T sse2_param_mask = {{DSFMT_MSK1, DSFMT_MSK2}}; 38 | #endif 39 | 40 | #if defined(HAVE_ALTIVEC) 41 | inline static void do_recursion(w128_t *r, w128_t *a, w128_t * b, 42 | w128_t *lung) { 43 | const vector unsigned char sl1 = ALTI_SL1; 44 | const vector unsigned char sl1_perm = ALTI_SL1_PERM; 45 | const vector unsigned int sl1_msk = ALTI_SL1_MSK; 46 | const vector unsigned char sr1 = ALTI_SR; 47 | const vector unsigned char sr1_perm = ALTI_SR_PERM; 48 | const vector unsigned int sr1_msk = ALTI_SR_MSK; 49 | const vector unsigned char perm = ALTI_PERM; 50 | const vector unsigned int msk1 = ALTI_MSK; 51 | vector unsigned int w, x, y, z; 52 | 53 | z = a->s; 54 | w = lung->s; 55 | x = vec_perm(w, (vector unsigned int)perm, perm); 56 | y = vec_perm(z, (vector unsigned int)sl1_perm, sl1_perm); 57 | y = vec_sll(y, sl1); 58 | y = vec_and(y, sl1_msk); 59 | w = vec_xor(x, b->s); 60 | w = vec_xor(w, y); 61 | x = vec_perm(w, (vector unsigned int)sr1_perm, sr1_perm); 62 | x = vec_srl(x, sr1); 63 | x = vec_and(x, sr1_msk); 64 | y = vec_and(w, msk1); 65 | z = vec_xor(z, y); 66 | r->s = vec_xor(z, x); 67 | lung->s = w; 68 | } 69 | #elif defined(HAVE_SSE2) 70 | /** 71 | * This function represents the recursion formula. 72 | * @param r output 128-bit 73 | * @param a a 128-bit part of the internal state array 74 | * @param b a 128-bit part of the internal state array 75 | * @param d a 128-bit part of the internal state array (I/O) 76 | */ 77 | inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *u) { 78 | __m128i v, w, x, y, z; 79 | 80 | x = a->si; 81 | z = _mm_slli_epi64(x, DSFMT_SL1); 82 | y = _mm_shuffle_epi32(u->si, SSE2_SHUFF); 83 | z = _mm_xor_si128(z, b->si); 84 | y = _mm_xor_si128(y, z); 85 | 86 | v = _mm_srli_epi64(y, DSFMT_SR); 87 | w = _mm_and_si128(y, sse2_param_mask.i128); 88 | v = _mm_xor_si128(v, x); 89 | v = _mm_xor_si128(v, w); 90 | r->si = v; 91 | u->si = y; 92 | } 93 | #else 94 | /** 95 | * This function represents the recursion formula. 96 | * @param r output 128-bit 97 | * @param a a 128-bit part of the internal state array 98 | * @param b a 128-bit part of the internal state array 99 | * @param lung a 128-bit part of the internal state array (I/O) 100 | */ 101 | inline static void do_recursion(w128_t *r, w128_t *a, w128_t * b, 102 | w128_t *lung) { 103 | uint64_t t0, t1, L0, L1; 104 | 105 | t0 = a->u[0]; 106 | t1 = a->u[1]; 107 | L0 = lung->u[0]; 108 | L1 = lung->u[1]; 109 | lung->u[0] = (t0 << DSFMT_SL1) ^ (L1 >> 32) ^ (L1 << 32) ^ b->u[0]; 110 | lung->u[1] = (t1 << DSFMT_SL1) ^ (L0 >> 32) ^ (L0 << 32) ^ b->u[1]; 111 | r->u[0] = (lung->u[0] >> DSFMT_SR) ^ (lung->u[0] & DSFMT_MSK1) ^ t0; 112 | r->u[1] = (lung->u[1] >> DSFMT_SR) ^ (lung->u[1] & DSFMT_MSK2) ^ t1; 113 | } 114 | #endif 115 | #endif 116 | -------------------------------------------------------------------------------- /randomstate/src/dSFMT/dSFMT-jump.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef DSFMT_JUMP_H 3 | #define DSFMT_JUMP_H 4 | /** 5 | * @file SFMT-jump.h 6 | * 7 | * @brief jump header file. 8 | * 9 | * @author Mutsuo Saito (Hiroshima University) 10 | * @author Makoto Matsumoto (The University of Tokyo) 11 | * 12 | * Copyright (C) 2012 Mutsuo Saito, Makoto Matsumoto, 13 | * Hiroshima University and The University of Tokyo. 14 | * All rights reserved. 15 | * 16 | * The 3-clause BSD License is applied to this software, see 17 | * LICENSE.txt 18 | */ 19 | #if defined(__cplusplus) 20 | extern "C" { 21 | #endif 22 | 23 | #include "dSFMT.h" 24 | void dSFMT_jump(dsfmt_t * dsfmt, const char * jump_str); 25 | 26 | #if defined(__cplusplus) 27 | } 28 | #endif 29 | #endif 30 | -------------------------------------------------------------------------------- /randomstate/src/dSFMT/dSFMT-params.h: -------------------------------------------------------------------------------- 1 | #ifndef DSFMT_PARAMS_H 2 | #define DSFMT_PARAMS_H 3 | 4 | #include "dSFMT.h" 5 | 6 | /*---------------------- 7 | the parameters of DSFMT 8 | following definitions are in dSFMT-paramsXXXX.h file. 9 | ----------------------*/ 10 | /** the pick up position of the array. 11 | #define DSFMT_POS1 122 12 | */ 13 | 14 | /** the parameter of shift left as four 32-bit registers. 15 | #define DSFMT_SL1 18 16 | */ 17 | 18 | /** the parameter of shift right as four 32-bit registers. 19 | #define DSFMT_SR1 12 20 | */ 21 | 22 | /** A bitmask, used in the recursion. These parameters are introduced 23 | * to break symmetry of SIMD. 24 | #define DSFMT_MSK1 (uint64_t)0xdfffffefULL 25 | #define DSFMT_MSK2 (uint64_t)0xddfecb7fULL 26 | */ 27 | 28 | /** These definitions are part of a 128-bit period certification vector. 29 | #define DSFMT_PCV1 UINT64_C(0x00000001) 30 | #define DSFMT_PCV2 UINT64_C(0x00000000) 31 | */ 32 | 33 | #define DSFMT_LOW_MASK UINT64_C(0x000FFFFFFFFFFFFF) 34 | #define DSFMT_HIGH_CONST UINT64_C(0x3FF0000000000000) 35 | #define DSFMT_SR 12 36 | 37 | /* for sse2 */ 38 | #if defined(HAVE_SSE2) 39 | #define SSE2_SHUFF 0x1b 40 | #elif defined(HAVE_ALTIVEC) 41 | #if defined(__APPLE__) /* For OSX */ 42 | #define ALTI_SR (vector unsigned char)(4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4) 43 | #define ALTI_SR_PERM \ 44 | (vector unsigned char)(15,0,1,2,3,4,5,6,15,8,9,10,11,12,13,14) 45 | #define ALTI_SR_MSK \ 46 | (vector unsigned int)(0x000fffffU,0xffffffffU,0x000fffffU,0xffffffffU) 47 | #define ALTI_PERM \ 48 | (vector unsigned char)(12,13,14,15,8,9,10,11,4,5,6,7,0,1,2,3) 49 | #else 50 | #define ALTI_SR {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4} 51 | #define ALTI_SR_PERM {15,0,1,2,3,4,5,6,15,8,9,10,11,12,13,14} 52 | #define ALTI_SR_MSK {0x000fffffU,0xffffffffU,0x000fffffU,0xffffffffU} 53 | #define ALTI_PERM {12,13,14,15,8,9,10,11,4,5,6,7,0,1,2,3} 54 | #endif 55 | #endif 56 | 57 | #if DSFMT_MEXP == 521 58 | #include "dSFMT-params521.h" 59 | #elif DSFMT_MEXP == 1279 60 | #include "dSFMT-params1279.h" 61 | #elif DSFMT_MEXP == 2203 62 | #include "dSFMT-params2203.h" 63 | #elif DSFMT_MEXP == 4253 64 | #include "dSFMT-params4253.h" 65 | #elif DSFMT_MEXP == 11213 66 | #include "dSFMT-params11213.h" 67 | #elif DSFMT_MEXP == 19937 68 | #include "dSFMT-params19937.h" 69 | #elif DSFMT_MEXP == 44497 70 | #include "dSFMT-params44497.h" 71 | #elif DSFMT_MEXP == 86243 72 | #include "dSFMT-params86243.h" 73 | #elif DSFMT_MEXP == 132049 74 | #include "dSFMT-params132049.h" 75 | #elif DSFMT_MEXP == 216091 76 | #include "dSFMT-params216091.h" 77 | #else 78 | #ifdef __GNUC__ 79 | #error "DSFMT_MEXP is not valid." 80 | #undef DSFMT_MEXP 81 | #else 82 | #undef DSFMT_MEXP 83 | #endif 84 | 85 | #endif 86 | 87 | #endif /* DSFMT_PARAMS_H */ 88 | -------------------------------------------------------------------------------- /randomstate/src/dSFMT/dSFMT-params19937.h: -------------------------------------------------------------------------------- 1 | #ifndef DSFMT_PARAMS19937_H 2 | #define DSFMT_PARAMS19937_H 3 | 4 | /* #define DSFMT_N 191 */ 5 | /* #define DSFMT_MAXDEGREE 19992 */ 6 | #define DSFMT_POS1 117 7 | #define DSFMT_SL1 19 8 | #define DSFMT_MSK1 UINT64_C(0x000ffafffffffb3f) 9 | #define DSFMT_MSK2 UINT64_C(0x000ffdfffc90fffd) 10 | #define DSFMT_MSK32_1 0x000ffaffU 11 | #define DSFMT_MSK32_2 0xfffffb3fU 12 | #define DSFMT_MSK32_3 0x000ffdffU 13 | #define DSFMT_MSK32_4 0xfc90fffdU 14 | #define DSFMT_FIX1 UINT64_C(0x90014964b32f4329) 15 | #define DSFMT_FIX2 UINT64_C(0x3b8d12ac548a7c7a) 16 | #define DSFMT_PCV1 UINT64_C(0x3d84e1ac0dc82880) 17 | #define DSFMT_PCV2 UINT64_C(0x0000000000000001) 18 | #define DSFMT_IDSTR "dSFMT2-19937:117-19:ffafffffffb3f-ffdfffc90fffd" 19 | 20 | 21 | /* PARAMETERS FOR ALTIVEC */ 22 | #if defined(__APPLE__) /* For OSX */ 23 | #define ALTI_SL1 (vector unsigned char)(3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3) 24 | #define ALTI_SL1_PERM \ 25 | (vector unsigned char)(2,3,4,5,6,7,30,30,10,11,12,13,14,15,0,1) 26 | #define ALTI_SL1_MSK \ 27 | (vector unsigned int)(0xffffffffU,0xfff80000U,0xffffffffU,0xfff80000U) 28 | #define ALTI_MSK (vector unsigned int)(DSFMT_MSK32_1, \ 29 | DSFMT_MSK32_2, DSFMT_MSK32_3, DSFMT_MSK32_4) 30 | #else /* For OTHER OSs(Linux?) */ 31 | #define ALTI_SL1 {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3} 32 | #define ALTI_SL1_PERM \ 33 | {2,3,4,5,6,7,30,30,10,11,12,13,14,15,0,1} 34 | #define ALTI_SL1_MSK \ 35 | {0xffffffffU,0xfff80000U,0xffffffffU,0xfff80000U} 36 | #define ALTI_MSK \ 37 | {DSFMT_MSK32_1, DSFMT_MSK32_2, DSFMT_MSK32_3, DSFMT_MSK32_4} 38 | #endif 39 | 40 | #endif /* DSFMT_PARAMS19937_H */ 41 | -------------------------------------------------------------------------------- /randomstate/src/dSFMT/dSFMT-test-gen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * gcc dSFMT-test-gen.c dSFMT.c -DHAVE_SSE2 -DDSFMT_MEXP=19937 -o dSFMT 4 | */ 5 | #include 6 | #include 7 | #include "dSFMT.h" 8 | 9 | int main(void) 10 | { 11 | int i; 12 | double d; 13 | uint64_t *temp; 14 | uint32_t seed = 1UL; 15 | dsfmt_t state; 16 | dsfmt_init_gen_rand(&state, seed); 17 | double out[1000]; 18 | dsfmt_fill_array_close1_open2(&state, out, 1000); 19 | 20 | FILE *fp; 21 | fp = fopen("dSFMT-testset-1.csv", "w"); 22 | if(fp == NULL){ 23 | printf("Couldn't open file\n"); 24 | return -1; 25 | } 26 | fprintf(fp, "seed, %" PRIu32 "\n", seed); 27 | for (i=0; i < 1000; i++) 28 | { 29 | d = out[i]; 30 | temp = (uint64_t *)&d; 31 | fprintf(fp, "%d, %" PRIu64 "\n", i, *temp); 32 | printf("%d, %" PRIu64 "\n", i, *temp); 33 | } 34 | fclose(fp); 35 | 36 | seed = 123456789UL; 37 | dsfmt_init_gen_rand(&state, seed); 38 | dsfmt_fill_array_close1_open2(&state, out, 1000); 39 | fp = fopen("dSFMT-testset-2.csv", "w"); 40 | if(fp == NULL){ 41 | printf("Couldn't open file\n"); 42 | return -1; 43 | } 44 | fprintf(fp, "seed, %" PRIu32 "\n", seed); 45 | for (i=0; i < 1000; i++) 46 | { 47 | d = out[i]; 48 | temp = (uint64_t *)&d; 49 | fprintf(fp, "%d, %" PRIu64 "\n", i, *temp); 50 | printf("%d, %" PRIu64 "\n", i, *temp); 51 | } 52 | fclose(fp); 53 | } -------------------------------------------------------------------------------- /randomstate/src/dSFMT/dSFMT_wrapper.pyx: -------------------------------------------------------------------------------- 1 | from libc.stdint cimport uint32_t, uint64_t 2 | from libc.stdlib cimport malloc, free 3 | 4 | DEF DSFMT_MEXP = 19937 5 | DEF DSFMT_N = 191 # 6 | DEF DSFMT_N_PLUS_1 = 192 # 7 | 8 | cdef extern from "dSFMT.h": 9 | cdef union W128_T: 10 | uint64_t u[2]; 11 | uint32_t u32[4]; 12 | double d[2]; 13 | 14 | ctypedef W128_T w128_t; 15 | 16 | cdef struct DSFMT_T: 17 | w128_t status[DSFMT_N_PLUS_1]; 18 | int idx; 19 | 20 | ctypedef DSFMT_T dsfmt_t; 21 | 22 | cdef void dsfmt_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed) 23 | cdef int dsfmt_get_min_array_size() 24 | cdef double dsfmt_genrand_close1_open2(dsfmt_t *dsfmt) 25 | cdef uint32_t dsfmt_genrand_uint32(dsfmt_t *dsfmt) 26 | cdef void dsfmt_fill_array_close1_open2(dsfmt_t *dsfmt, double array[], int size) 27 | cdef void dsfmt_init_by_array(dsfmt_t * dsfmt, uint32_t init_key[], int key_length) 28 | cdef void dsfmt_fill_array_close_open(dsfmt_t *dsfmt, double array[], int size) 29 | cdef double dsfmt_genrand_close_open(dsfmt_t *dsfmt) 30 | 31 | 32 | cdef class Test: 33 | cdef: 34 | int idx 35 | dsfmt_t state 36 | 37 | def print_state(self): 38 | cdef w128_t val 39 | for i in range(DSFMT_N_PLUS_1): 40 | print(i) 41 | val = self.state.status[i] 42 | print(val.u[0]) 43 | print(val.u[1]) 44 | 45 | def init(self): 46 | dsfmt_init_gen_rand(&self.state, 0) 47 | 48 | def get_min_array_size(self): 49 | return dsfmt_get_min_array_size() 50 | 51 | def get_a_double(self): 52 | return dsfmt_genrand_close1_open2(&self.state) 53 | 54 | -------------------------------------------------------------------------------- /randomstate/src/dSFMT/setup-dsfmt.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | from distutils.core import setup 4 | from distutils.extension import Extension 5 | from os import getcwd, name 6 | from os.path import join 7 | 8 | import numpy 9 | from Cython.Build import cythonize 10 | 11 | pwd = getcwd() 12 | 13 | sources = [join(pwd, 'dSFMT_wrapper.pyx'), 14 | join(pwd, 'dSFMT.c')] 15 | 16 | 17 | defs = [('HAVE_SSE2', '1'),('DSFMT_MEXP','19937')] 18 | 19 | include_dirs = [pwd] + [numpy.get_include()] 20 | 21 | extra_link_args = ['Advapi32.lib'] if name == 'nt' else [] 22 | extra_compile_args = [] if name == 'nt' else ['-std=c99'] 23 | 24 | setup(ext_modules=cythonize([Extension("dSFMT_wrapper", 25 | sources=sources, 26 | include_dirs=include_dirs, 27 | define_macros=defs, 28 | extra_compile_args=extra_compile_args, 29 | extra_link_args=extra_link_args) 30 | ]) 31 | ) 32 | -------------------------------------------------------------------------------- /randomstate/src/mlfg-1279-861/mlfg-1279-861-test-gen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * gcc -O2 ../splitmix64/splitmix64.c mlfg-1279-861.c mlfg-1279-861-test-gen.c -o mlfg -std=c99 3 | */ 4 | 5 | #include 6 | #include 7 | #include "../splitmix64/splitmix64.h" 8 | #include "mlfg-1279-861.h" 9 | 10 | int main(void) 11 | { 12 | int i; 13 | uint64_t seed = 1ULL; 14 | uint64_t temp; 15 | mlfg_state state = {{ 0 }}; 16 | mlfg_seed(&state, seed); 17 | 18 | FILE *fp; 19 | fp = fopen("mlfg-testset-1.csv", "w"); 20 | if(fp == NULL){ 21 | printf("Couldn't open file\n"); 22 | return -1; 23 | } 24 | fprintf(fp, "seed, %" PRIu64 "\n", seed); 25 | for (i=0; i < 1000; i++) 26 | { 27 | temp = mlfg_next(&state); 28 | fprintf(fp, "%d, %" PRIu64 "\n", i, temp); 29 | printf("%d, %" PRIu64 "\n", i, temp); 30 | } 31 | fclose(fp); 32 | 33 | seed = 12345678910111ULL; 34 | mlfg_seed(&state, seed); 35 | fp = fopen("mlfg-testset-2.csv", "w"); 36 | if(fp == NULL){ 37 | printf("Couldn't open file\n"); 38 | return -1; 39 | } 40 | fprintf(fp, "seed, %" PRIu64 "\n", seed); 41 | for (i=0; i < 1000; i++) 42 | { 43 | temp = mlfg_next(&state); 44 | fprintf(fp, "%d, %" PRIu64 "\n", i, temp); 45 | printf("%d, %" PRIu64 "\n", i, temp); 46 | } 47 | fclose(fp); 48 | } -------------------------------------------------------------------------------- /randomstate/src/mlfg-1279-861/mlfg-1279-861.c: -------------------------------------------------------------------------------- 1 | #include "mlfg-1279-861.h" 2 | #include "../splitmix64/splitmix64.h" 3 | 4 | extern inline uint64_t mlfg_next(mlfg_state* state); 5 | 6 | void mlfg_seed(mlfg_state* state, uint64_t seed) 7 | { 8 | uint64_t seeds[K]; 9 | uint64_t seed_copy = seed; 10 | int i; 11 | for (i = 0; i < K; i++) 12 | { 13 | seeds[i] = splitmix64_next(&seed_copy); 14 | if ((seeds[i] % 2) != 1) 15 | seeds[i]++; 16 | } 17 | mlfg_init_state(state, seeds); 18 | } 19 | 20 | void mlfg_seed_by_array(mlfg_state* state, uint64_t *seed_array, int count) 21 | { 22 | uint64_t seeds[K]; 23 | uint64_t seed_copy = 0; 24 | int iter_bound = K >= count ? K : count; 25 | int i, loc = 0; 26 | for (i = 0; i < iter_bound ; i++) 27 | { 28 | if (i < count) { 29 | seed_copy ^= seed_array[i]; 30 | } 31 | seeds[loc] = splitmix64_next(&seed_copy); 32 | if ((seeds[loc] % 2) != 1) 33 | seeds[loc]++; 34 | loc++; 35 | if (loc == K) 36 | loc = 0; 37 | } 38 | mlfg_init_state(state, seeds); 39 | } 40 | 41 | 42 | void mlfg_init_state(mlfg_state *state, uint64_t seeds[K]) 43 | { 44 | int i; 45 | for (i = 0; i < K; i++) 46 | { 47 | state->lags[i] = seeds[i]; 48 | } 49 | state->pos = 0; 50 | state->lag_pos = K - J; 51 | } -------------------------------------------------------------------------------- /randomstate/src/mlfg-1279-861/mlfg-1279-861.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #include "../../src/common/stdint.h" 3 | #define inline __forceinline 4 | #else 5 | #include 6 | #endif 7 | 8 | #define K 1279 9 | #define J 861 10 | 11 | typedef struct s_mlfg_state 12 | { 13 | uint64_t lags[K]; 14 | int pos; 15 | int lag_pos; 16 | } mlfg_state; 17 | 18 | void mlfg_seed(mlfg_state* state, uint64_t seed); 19 | 20 | void mlfg_seed_by_array(mlfg_state* state, uint64_t *seed_array, int count); 21 | 22 | void mlfg_init_state(mlfg_state *state, uint64_t seeds[K]); 23 | 24 | /* 25 | * Returns 64 bits, but the last bit is always 1. 26 | * Upstream functions are expected to understand this and 27 | * only use the upper 63 bits. In most implementations, 28 | * fewer than 63 bits are needed, and it is thought to 29 | * be better to use the upper bits first. For example, 30 | * when making a 64 bit unsigned int, take the two upper 31 | * 32 bit segments. 32 | */ 33 | static inline uint64_t mlfg_next(mlfg_state* state) 34 | { 35 | state->pos++; 36 | state->lag_pos++; 37 | if (state->pos >= K) 38 | state->pos = 0; 39 | else if (state->lag_pos >= K) 40 | state->lag_pos = 0; 41 | state->lags[state->pos] = state->lags[state->lag_pos] * state->lags[state->pos]; 42 | return state->lags[state->pos]; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /randomstate/src/mrg32k3a/mrg32k3a-original.c: -------------------------------------------------------------------------------- 1 | /* 2 | 32-bits Random number generator U(0,1): MRG32k3a 3 | Author: Pierre L'Ecuyer, 4 | Source: Good Parameter Sets for Combined Multiple Recursive Random 5 | Number Generators, 6 | Shorter version in Operations Research, 7 | 47, 1 (1999), 159--164. 8 | --------------------------------------------------------- 9 | */ 10 | #include "MRG32k3a.h" 11 | 12 | #define norm 2.328306549295728e-10 13 | #define m1 4294967087.0 14 | #define m2 4294944443.0 15 | #define a12 1403580.0 16 | #define a13n 810728.0 17 | #define a21 527612.0 18 | #define a23n 1370589.0 19 | 20 | /*** 21 | The seeds for s10, s11, s12 must be integers in [0, m1 - 1] and not all 0. 22 | The seeds for s20, s21, s22 must be integers in [0, m2 - 1] and not all 0. 23 | ***/ 24 | 25 | #define SEED 12345 26 | 27 | static double s10 = SEED, s11 = SEED, s12 = SEED, 28 | s20 = SEED, s21 = SEED, s22 = SEED; 29 | 30 | 31 | double MRG32k3a (void) 32 | { 33 | long k; 34 | double p1, p2; 35 | /* Component 1 */ 36 | p1 = a12 * s11 - a13n * s10; 37 | k = p1 / m1; 38 | p1 -= k * m1; 39 | if (p1 < 0.0) 40 | p1 += m1; 41 | s10 = s11; 42 | s11 = s12; 43 | s12 = p1; 44 | 45 | /* Component 2 */ 46 | p2 = a21 * s22 - a23n * s20; 47 | k = p2 / m2; 48 | p2 -= k * m2; 49 | if (p2 < 0.0) 50 | p2 += m2; 51 | s20 = s21; 52 | s21 = s22; 53 | s22 = p2; 54 | 55 | /* Combination */ 56 | if (p1 <= p2) 57 | return ((p1 - p2 + m1) * norm); 58 | else 59 | return ((p1 - p2) * norm); 60 | } 61 | -------------------------------------------------------------------------------- /randomstate/src/mrg32k3a/mrg32k3a-test-gen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int i; 7 | uint64_t seed = 1ULL; 8 | uint32_t temp; 9 | mrg32k3a_state state = { 0 }; 10 | mrg32k3a_seed(&state, seed); 11 | 12 | FILE *fp; 13 | fp = fopen("mrg32k3a-testset-1.csv", "w"); 14 | if(fp == NULL){ 15 | printf("Couldn't open file\n"); 16 | return -1; 17 | } 18 | fprintf(fp, "seed, %" PRIu64 "\n", seed); 19 | for (i=0; i < 1000; i++) 20 | { 21 | temp = mrg32k3a_random(&state); 22 | fprintf(fp, "%d, %" PRIu32 "\n", i, temp); 23 | printf("%d, %" PRIu32 "\n", i, temp); 24 | } 25 | fclose(fp); 26 | 27 | seed = 12345678910111ULL; 28 | mrg32k3a_seed(&state, seed); 29 | fp = fopen("mrg32k3a-testset-2.csv", "w"); 30 | if(fp == NULL){ 31 | printf("Couldn't open file\n"); 32 | return -1; 33 | } 34 | fprintf(fp, "seed, %" PRIu64 "\n", seed); 35 | for (i=0; i < 1000; i++) 36 | { 37 | temp = mrg32k3a_random(&state); 38 | fprintf(fp, "%d, %" PRIu32 "\n", i, temp); 39 | printf("%d, %" PRIu32 "\n", i, temp); 40 | } 41 | fclose(fp); 42 | } 43 | -------------------------------------------------------------------------------- /randomstate/src/mrg32k3a/mrg32k3a.c: -------------------------------------------------------------------------------- 1 | /* 2 | 32-bits Random number generator U(0,1): MRG32k3a 3 | Author: Pierre L'Ecuyer, 4 | Source: Good Parameter Sets for Combined Multiple Recursive Random 5 | Number Generators, 6 | Shorter version in Operations Research, 7 | 47, 1 (1999), 159--164. 8 | --------------------------------------------------------- 9 | */ 10 | #include "mrg32k3a.h" 11 | #include "../splitmix64/splitmix64.h" 12 | 13 | /*** 14 | The seeds for state->s10, state->s11, state->s12 must be integers in [0, m1 - 1] and not all 0. 15 | The seeds for state->s20, state->s21, state->s22 must be integers in [0, m2 - 1] and not all 0. 16 | ***/ 17 | 18 | extern inline uint32_t mrg32k3a_random(mrg32k3a_state* state); 19 | 20 | static void mrg32k3a_set_state(mrg32k3a_state* state, int64_t *seeds) 21 | { 22 | state->s1[0] = seeds[0]; 23 | state->s1[1] = seeds[1]; 24 | state->s1[2] = seeds[2]; 25 | state->s2[0] = seeds[3]; 26 | state->s2[1] = seeds[4]; 27 | state->s2[2] = seeds[5]; 28 | state->loc = 2; 29 | } 30 | 31 | void mrg32k3a_seed(mrg32k3a_state* state, uint64_t seed) 32 | { 33 | uint64_t seed_copy = seed; 34 | int64_t seeds[6]; 35 | int64_t draw, upper; 36 | int i; 37 | for (i=0; i<6; i++) 38 | { 39 | if(i < 3) 40 | upper = m1; 41 | else 42 | upper = m2; 43 | 44 | draw = upper; 45 | while(draw >= upper) 46 | { 47 | draw = splitmix64_next(&seed_copy) >> 32; 48 | } 49 | seeds[i] = draw; 50 | } 51 | mrg32k3a_set_state(state, &seeds[0]); 52 | } 53 | 54 | 55 | 56 | void mrg32k3a_seed_by_array(mrg32k3a_state* state, uint64_t *seed, int count) 57 | { 58 | uint64_t seed_copy = 0; 59 | int iter_bound = 6 >= count ? 6 : count; 60 | int64_t seeds[6]; 61 | int64_t draw, upper; 62 | int i, loc = 0; 63 | for (i=0; i= upper) 75 | { 76 | draw = splitmix64_next(&seed_copy) >> 32; 77 | } 78 | seeds[loc] = draw; 79 | loc++; 80 | if (loc == 6) 81 | loc = 0; 82 | } 83 | mrg32k3a_set_state(state, &seeds[0]); 84 | } 85 | -------------------------------------------------------------------------------- /randomstate/src/mrg32k3a/mrg32k3a.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #include "../../src/common/stdint.h" 3 | #define inline __forceinline 4 | #else 5 | #include 6 | #endif 7 | 8 | #define m1 4294967087LL 9 | #define m2 4294944443LL 10 | #define a12 1403580LL 11 | #define a13n 810728LL 12 | #define a21 527612LL 13 | #define a23n 1370589LL 14 | 15 | typedef struct s_mrg32k3a_state 16 | { 17 | int64_t s1[3]; 18 | int64_t s2[3]; 19 | int loc; 20 | } mrg32k3a_state; 21 | 22 | static inline uint32_t mrg32k3a_random(mrg32k3a_state* state) 23 | { 24 | int64_t p1 = 0; 25 | int64_t p2 = 0; 26 | /* Component 1 */ 27 | switch (state->loc) { 28 | case 0: 29 | p1 = a12 * state->s1[2] - a13n * state->s1[1]; 30 | p2 = a21 * state->s2[0] - a23n * state->s2[1]; 31 | state->loc = 1; 32 | break; 33 | case 1: 34 | p1 = a12 * state->s1[0] - a13n * state->s1[2]; 35 | p2 = a21 * state->s2[1] - a23n * state->s2[2]; 36 | state->loc = 2; 37 | break; 38 | case 2: 39 | p1 = a12 * state->s1[1] - a13n * state->s1[0]; 40 | p2 = a21 * state->s2[2] - a23n * state->s2[0]; 41 | state->loc = 0; 42 | break; 43 | } 44 | 45 | p1 -= (p1 >= 0) ? (p1 / m1) * m1 : (p1 / m1) * m1 - m1; 46 | state->s1[state->loc] = p1; 47 | /* Component 2 */ 48 | p2 -= (p2 >= 0) ? (p2 / m2) * m2 : (p2 / m2) * m2 - m2; 49 | state->s2[state->loc] = p2; 50 | 51 | /* Combination */ 52 | return (uint32_t)((p1 <= p2) ? (p1 - p2 + m1) : (p1 - p2)); 53 | } 54 | 55 | void mrg32k3a_seed(mrg32k3a_state* state, uint64_t seed); 56 | 57 | void mrg32k3a_seed_by_array(mrg32k3a_state* state, uint64_t *seed, int count); -------------------------------------------------------------------------------- /randomstate/src/pcg/pcg-32-test-gen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * gcc pcg-32.c pcg-rngs-64.c pcg-output-64.c pcg-output-32.c pcg-advance-64.c -std=c99 -o pcg-32 3 | */ 4 | 5 | #include 6 | #include 7 | #include "pcg_variants.h" 8 | 9 | int main(void) 10 | { 11 | int i; 12 | uint64_t seed1 = 42ULL, seed2 = 1ULL; 13 | uint32_t temp; 14 | pcg32_random_t state = {{ 0 }}; 15 | pcg32_srandom_r(&state, seed1, seed2); 16 | FILE *fp; 17 | fp = fopen("pcg32-testset-1.csv", "w"); 18 | if(fp == NULL){ 19 | printf("Couldn't open file\n"); 20 | return -1; 21 | } 22 | fprintf(fp, "seed, %" PRIu64 ", %" PRIu64 "\n", seed1, seed2); 23 | for (i=0; i < 1000; i++) 24 | { 25 | temp = pcg32_random_r(&state); 26 | fprintf(fp, "%d, %" PRIu32 "\n", i, temp); 27 | printf("%d, %" PRIu32 "\n", i, temp); 28 | } 29 | fclose(fp); 30 | 31 | seed1 = 12345678910111ULL; 32 | seed2 = 53ULL; 33 | pcg32_srandom_r(&state, seed1, seed2); 34 | fp = fopen("pcg32-testset-2.csv", "w"); 35 | if(fp == NULL){ 36 | printf("Couldn't open file\n"); 37 | return -1; 38 | } 39 | fprintf(fp, "seed, %" PRIu64 ", %" PRIu64 "\n", seed1, seed2); 40 | for (i=0; i < 1000; i++) 41 | { 42 | temp = pcg32_random_r(&state); 43 | fprintf(fp, "%d, %" PRIu32 "\n", i, temp); 44 | printf("%d, %" PRIu32 "\n", i, temp); 45 | } 46 | fclose(fp); 47 | 48 | } -------------------------------------------------------------------------------- /randomstate/src/pcg/pcg-64-test-gen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * gcc pcg-64.c pcg-rngs-128.c pcg-output-64.c pcg-output-128.c pcg-advance-128.c pcg-output-32.c -std=c99 -D__SIZEOF_INT128__=16 -o pcg-64 3 | */ 4 | 5 | #include 6 | #include 7 | #include "pcg_variants.h" 8 | 9 | int main(void) 10 | { 11 | int i; 12 | pcg128_t seed1 = 42ULL, seed2 = 1ULL; 13 | uint64_t temp; 14 | pcg64_random_t state = {{ 0 }}; 15 | pcg64_srandom_r(&state, seed1, seed2); 16 | FILE *fp; 17 | fp = fopen("pcg64-testset-1.csv", "w"); 18 | if(fp == NULL){ 19 | printf("Couldn't open file\n"); 20 | return -1; 21 | } 22 | fprintf(fp, "seed, %" PRIu64 ", %" PRIu64 "\n", (uint64_t)seed1, (uint64_t)seed2); 23 | for (i=0; i < 1000; i++) 24 | { 25 | temp = pcg64_random_r(&state); 26 | fprintf(fp, "%d, %" PRIu64 "\n", i, temp); 27 | printf("%d, %" PRIu64 "\n", i, temp); 28 | } 29 | fclose(fp); 30 | 31 | seed1 = 12345678910111ULL; 32 | seed2 = 53ULL; 33 | pcg64_srandom_r(&state, seed1, seed2); 34 | fp = fopen("pcg64-testset-2.csv", "w"); 35 | if(fp == NULL){ 36 | printf("Couldn't open file\n"); 37 | return -1; 38 | } 39 | fprintf(fp, "seed, %" PRIu64 ", %" PRIu64 "\n", (uint64_t)seed1, (uint64_t)seed2); 40 | for (i=0; i < 1000; i++) 41 | { 42 | temp = pcg64_random_r(&state); 43 | fprintf(fp, "%d, %" PRIu64 "\n", i, temp); 44 | printf("%d, %" PRIu64 "\n", i, temp); 45 | } 46 | fclose(fp); 47 | 48 | } 49 | -------------------------------------------------------------------------------- /randomstate/src/pcg/pcg-advance-128.c: -------------------------------------------------------------------------------- 1 | /* 2 | * PCG Random Number Generation for C. 3 | * 4 | * Copyright 2014 Melissa O'Neill 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * For additional information about the PCG random number generation scheme, 19 | * including its license and other licensing options, visit 20 | * 21 | * http://www.pcg-random.org 22 | */ 23 | 24 | /* 25 | * This code is derived from the canonical C++ PCG implementation, which 26 | * has many additional features and is preferable if you can use C++ in 27 | * your project. 28 | * 29 | * Repetative C code is derived using C preprocessor metaprogramming 30 | * techniques. 31 | */ 32 | 33 | #include "pcg_variants.h" 34 | 35 | /* Multi-step advance functions (jump-ahead, jump-back) 36 | * 37 | * The method used here is based on Brown, "Random Number Generation 38 | * with Arbitrary Stride,", Transactions of the American Nuclear 39 | * Society (Nov. 1994). The algorithm is very similar to fast 40 | * exponentiation. 41 | * 42 | * Even though delta is an unsigned integer, we can pass a 43 | * signed integer to go backwards, it just goes "the long way round". 44 | */ 45 | 46 | #if PCG_HAS_128BIT_OPS 47 | pcg128_t pcg_advance_lcg_128(pcg128_t state, pcg128_t delta, pcg128_t cur_mult, 48 | pcg128_t cur_plus) 49 | { 50 | pcg128_t acc_mult = 1u; 51 | pcg128_t acc_plus = 0u; 52 | while (delta > 0) { 53 | if (delta & 1) { 54 | acc_mult *= cur_mult; 55 | acc_plus = acc_plus * cur_mult + cur_plus; 56 | } 57 | cur_plus = (cur_mult + 1) * cur_plus; 58 | cur_mult *= cur_mult; 59 | delta /= 2; 60 | } 61 | return acc_mult * state + acc_plus; 62 | } 63 | #endif 64 | 65 | -------------------------------------------------------------------------------- /randomstate/src/pcg/pcg-advance-64.c: -------------------------------------------------------------------------------- 1 | /* 2 | * PCG Random Number Generation for C. 3 | * 4 | * Copyright 2014 Melissa O'Neill 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * For additional information about the PCG random number generation scheme, 19 | * including its license and other licensing options, visit 20 | * 21 | * http://www.pcg-random.org 22 | */ 23 | 24 | /* 25 | * This code is derived from the canonical C++ PCG implementation, which 26 | * has many additional features and is preferable if you can use C++ in 27 | * your project. 28 | * 29 | * Repetative C code is derived using C preprocessor metaprogramming 30 | * techniques. 31 | */ 32 | 33 | #include "pcg_variants.h" 34 | 35 | /* Multi-step advance functions (jump-ahead, jump-back) 36 | * 37 | * The method used here is based on Brown, "Random Number Generation 38 | * with Arbitrary Stride,", Transactions of the American Nuclear 39 | * Society (Nov. 1994). The algorithm is very similar to fast 40 | * exponentiation. 41 | * 42 | * Even though delta is an unsigned integer, we can pass a 43 | * signed integer to go backwards, it just goes "the long way round". 44 | */ 45 | 46 | uint64_t pcg_advance_lcg_64(uint64_t state, uint64_t delta, uint64_t cur_mult, 47 | uint64_t cur_plus) 48 | { 49 | uint64_t acc_mult = 1u; 50 | uint64_t acc_plus = 0u; 51 | while (delta > 0) { 52 | if (delta & 1) { 53 | acc_mult *= cur_mult; 54 | acc_plus = acc_plus * cur_mult + cur_plus; 55 | } 56 | cur_plus = (cur_mult + 1) * cur_plus; 57 | cur_mult *= cur_mult; 58 | delta /= 2; 59 | } 60 | return acc_mult * state + acc_plus; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /randomstate/src/pcg/pcg-output-128.c: -------------------------------------------------------------------------------- 1 | /* 2 | * PCG Random Number Generation for C. 3 | * 4 | * Copyright 2014 Melissa O'Neill 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * For additional information about the PCG random number generation scheme, 19 | * including its license and other licensing options, visit 20 | * 21 | * http://www.pcg-random.org 22 | */ 23 | 24 | /* 25 | * This code is derived from the canonical C++ PCG implementation, which 26 | * has many additional features and is preferable if you can use C++ in 27 | * your project. 28 | * 29 | * The contents of this file were mechanically derived from pcg_variants.h 30 | * (every inline function defined there gets an exern declaration here). 31 | */ 32 | 33 | #include "pcg_variants.h" 34 | 35 | /* 36 | * Rotate helper functions. 37 | */ 38 | 39 | #if PCG_HAS_128BIT_OPS 40 | extern inline pcg128_t pcg_rotr_128(pcg128_t value, unsigned int rot); 41 | #endif 42 | 43 | /* 44 | * Output functions. These are the core of the PCG generation scheme. 45 | */ 46 | 47 | // XSH RS 48 | 49 | // XSH RR 50 | 51 | // RXS M XS 52 | 53 | #if PCG_HAS_128BIT_OPS 54 | extern inline pcg128_t pcg_output_rxs_m_xs_128_128(pcg128_t state); 55 | #endif 56 | 57 | // XSL RR (only defined for >= 64 bits) 58 | 59 | // XSL RR RR (only defined for >= 64 bits) 60 | 61 | #if PCG_HAS_128BIT_OPS 62 | extern inline pcg128_t pcg_output_xsl_rr_rr_128_128(pcg128_t state); 63 | #endif 64 | 65 | -------------------------------------------------------------------------------- /randomstate/src/pcg/pcg-output-32.c: -------------------------------------------------------------------------------- 1 | /* 2 | * PCG Random Number Generation for C. 3 | * 4 | * Copyright 2014 Melissa O'Neill 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * For additional information about the PCG random number generation scheme, 19 | * including its license and other licensing options, visit 20 | * 21 | * http://www.pcg-random.org 22 | */ 23 | 24 | /* 25 | * This code is derived from the canonical C++ PCG implementation, which 26 | * has many additional features and is preferable if you can use C++ in 27 | * your project. 28 | * 29 | * The contents of this file were mechanically derived from pcg_variants.h 30 | * (every inline function defined there gets an exern declaration here). 31 | */ 32 | 33 | #include "pcg_variants.h" 34 | 35 | /* 36 | * Rotate helper functions. 37 | */ 38 | 39 | extern inline uint32_t pcg_rotr_32(uint32_t value, unsigned int rot); 40 | 41 | /* 42 | * Output functions. These are the core of the PCG generation scheme. 43 | */ 44 | 45 | // XSH RS 46 | 47 | extern inline uint32_t pcg_output_xsh_rs_64_32(uint64_t state); 48 | 49 | // XSH RR 50 | 51 | extern inline uint32_t pcg_output_xsh_rr_64_32(uint64_t state); 52 | 53 | // RXS M XS 54 | 55 | extern inline uint32_t pcg_output_rxs_m_xs_32_32(uint32_t state); 56 | 57 | // XSL RR (only defined for >= 64 bits) 58 | 59 | extern inline uint32_t pcg_output_xsl_rr_64_32(uint64_t state); 60 | 61 | // XSL RR RR (only defined for >= 64 bits) 62 | 63 | -------------------------------------------------------------------------------- /randomstate/src/pcg/pcg-output-64.c: -------------------------------------------------------------------------------- 1 | /* 2 | * PCG Random Number Generation for C. 3 | * 4 | * Copyright 2014 Melissa O'Neill 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | * For additional information about the PCG random number generation scheme, 19 | * including its license and other licensing options, visit 20 | * 21 | * http://www.pcg-random.org 22 | */ 23 | 24 | /* 25 | * This code is derived from the canonical C++ PCG implementation, which 26 | * has many additional features and is preferable if you can use C++ in 27 | * your project. 28 | * 29 | * The contents of this file were mechanically derived from pcg_variants.h 30 | * (every inline function defined there gets an exern declaration here). 31 | */ 32 | 33 | #include "pcg_variants.h" 34 | 35 | /* 36 | * Rotate helper functions. 37 | */ 38 | 39 | extern inline uint64_t pcg_rotr_64(uint64_t value, unsigned int rot); 40 | 41 | /* 42 | * Output functions. These are the core of the PCG generation scheme. 43 | */ 44 | 45 | // XSH RS 46 | 47 | #if PCG_HAS_128BIT_OPS 48 | extern inline uint64_t pcg_output_xsh_rs_128_64(pcg128_t state); 49 | #endif 50 | 51 | // XSH RR 52 | 53 | #if PCG_HAS_128BIT_OPS 54 | extern inline uint64_t pcg_output_xsh_rr_128_64(pcg128_t state); 55 | #endif 56 | 57 | // RXS M XS 58 | 59 | extern inline uint64_t pcg_output_rxs_m_xs_64_64(uint64_t state); 60 | 61 | // XSL RR (only defined for >= 64 bits) 62 | 63 | #if PCG_HAS_128BIT_OPS 64 | extern inline uint64_t pcg_output_xsl_rr_128_64(pcg128_t state); 65 | #endif 66 | 67 | // XSL RR RR (only defined for >= 64 bits) 68 | 69 | extern inline uint64_t pcg_output_xsl_rr_rr_64_64(uint64_t state); 70 | 71 | -------------------------------------------------------------------------------- /randomstate/src/pcg/pcg32.c: -------------------------------------------------------------------------------- 1 | #include "pcg32.h" 2 | 3 | extern inline void pcg_setseq_64_step_r(pcg_state_setseq_64* rng); 4 | extern inline uint32_t pcg_output_xsl_rr_64_32(uint64_t state); 5 | extern inline void pcg_setseq_64_srandom_r(pcg_state_setseq_64* rng, 6 | uint64_t initstate, uint64_t initseq); 7 | extern inline uint32_t 8 | pcg_setseq_64_xsl_rr_32_random_r(pcg_state_setseq_64* rng); 9 | extern inline uint32_t 10 | pcg_setseq_64_xsl_rr_32_boundedrand_r(pcg_state_setseq_64* rng, 11 | uint32_t bound); 12 | extern inline void pcg_setseq_64_advance_r(pcg_state_setseq_64* rng, 13 | uint64_t delta); 14 | 15 | uint64_t pcg_advance_lcg_64(uint64_t state, uint64_t delta, uint64_t cur_mult, 16 | uint64_t cur_plus) 17 | { 18 | uint64_t acc_mult = 1u; 19 | uint64_t acc_plus = 0u; 20 | while (delta > 0) { 21 | if (delta & 1) { 22 | acc_mult *= cur_mult; 23 | acc_plus = acc_plus * cur_mult + cur_plus; 24 | } 25 | cur_plus = (cur_mult + 1) * cur_plus; 26 | cur_mult *= cur_mult; 27 | delta /= 2; 28 | } 29 | return acc_mult * state + acc_plus; 30 | } 31 | -------------------------------------------------------------------------------- /randomstate/src/pcg/pcg32.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #ifndef _INTTYPES 3 | #include "../common/inttypes.h" 4 | #endif 5 | #define inline __forceinline 6 | #else 7 | #include 8 | #endif 9 | 10 | #if __GNUC_GNU_INLINE__ && !defined(__cplusplus) 11 | #error Nonstandard GNU inlining semantics. Compile with -std=c99 or better. 12 | #endif 13 | 14 | typedef struct { 15 | uint64_t state; 16 | } pcg_state_64; 17 | 18 | typedef struct { 19 | uint64_t state; 20 | uint64_t inc; 21 | } pcg_state_setseq_64; 22 | 23 | #define PCG_DEFAULT_MULTIPLIER_64 6364136223846793005ULL 24 | #define PCG_DEFAULT_INCREMENT_64 1442695040888963407ULL 25 | #define PCG_STATE_SETSEQ_64_INITIALIZER \ 26 | { 0x853c49e6748fea9bULL, 0xda3e39cb94b95bdbULL } 27 | 28 | static inline uint32_t pcg_rotr_32(uint32_t value, unsigned int rot) 29 | { 30 | #if PCG_USE_INLINE_ASM && __clang__ && (__x86_64__ || __i386__) 31 | asm ("rorl %%cl, %0" : "=r" (value) : "0" (value), "c" (rot)); 32 | return value; 33 | #else 34 | return (value >> rot) | (value << ((- rot) & 31)); 35 | #endif 36 | } 37 | 38 | static inline void pcg_setseq_64_step_r(pcg_state_setseq_64* rng) 39 | { 40 | rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64 + rng->inc; 41 | } 42 | 43 | static inline uint32_t pcg_output_xsl_rr_64_32(uint64_t state) 44 | { 45 | return pcg_rotr_32(((uint32_t)(state >> 32u)) ^ (uint32_t)state, 46 | state >> 59u); 47 | } 48 | 49 | static inline uint32_t pcg_output_xsh_rr_64_32(uint64_t state) 50 | { 51 | return pcg_rotr_32(((state >> 18u) ^ state) >> 27u, state >> 59u); 52 | } 53 | 54 | 55 | static inline uint32_t 56 | pcg_setseq_64_xsh_rr_32_random_r(pcg_state_setseq_64* rng) 57 | { 58 | uint64_t oldstate = rng->state; 59 | pcg_setseq_64_step_r(rng); 60 | return pcg_output_xsh_rr_64_32(oldstate); 61 | } 62 | 63 | static inline void pcg_setseq_64_srandom_r(pcg_state_setseq_64* rng, 64 | uint64_t initstate, uint64_t initseq) 65 | { 66 | rng->state = 0U; 67 | rng->inc = (initseq << 1u) | 1u; 68 | pcg_setseq_64_step_r(rng); 69 | rng->state += initstate; 70 | pcg_setseq_64_step_r(rng); 71 | } 72 | 73 | static inline uint32_t 74 | pcg_setseq_64_xsl_rr_32_random_r(pcg_state_setseq_64* rng) 75 | { 76 | uint64_t oldstate = rng->state; 77 | pcg_setseq_64_step_r(rng); 78 | return pcg_output_xsl_rr_64_32(oldstate); 79 | } 80 | 81 | static inline uint32_t 82 | pcg_setseq_64_xsl_rr_32_boundedrand_r(pcg_state_setseq_64* rng, 83 | uint32_t bound) 84 | { 85 | uint32_t threshold = -bound % bound; 86 | for (;;) { 87 | uint32_t r = pcg_setseq_64_xsl_rr_32_random_r(rng); 88 | if (r >= threshold) 89 | return r % bound; 90 | } 91 | } 92 | 93 | extern uint64_t pcg_advance_lcg_64(uint64_t state, uint64_t delta, 94 | uint64_t cur_mult, uint64_t cur_plus); 95 | 96 | 97 | 98 | static inline void pcg_setseq_64_advance_r(pcg_state_setseq_64* rng, 99 | uint64_t delta) 100 | { 101 | rng->state = pcg_advance_lcg_64(rng->state, delta, 102 | PCG_DEFAULT_MULTIPLIER_64, rng->inc); 103 | } 104 | 105 | typedef pcg_state_setseq_64 pcg32_random_t; 106 | #define pcg32_random_r pcg_setseq_64_xsh_rr_32_random_r 107 | #define pcg32_advance_r pcg_setseq_64_advance_r 108 | #define pcg32_boundedrand_r pcg_setseq_64_xsh_rr_32_boundedrand_r 109 | #define pcg32_srandom_r pcg_setseq_64_srandom_r 110 | #define pcg32_advance_r pcg_setseq_64_advance_r 111 | #define PCG32_INITIALIZER PCG_STATE_SETSEQ_64_INITIALIZER 112 | -------------------------------------------------------------------------------- /randomstate/src/pcg64-compat/pcg64.c: -------------------------------------------------------------------------------- 1 | /* 2 | * PCG64 Random Number Generation for C. 3 | * 4 | * Copyright 2014 Melissa O'Neill 5 | * Copyright 2015 Robert Kern 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * For additional information about the PCG random number generation scheme, 20 | * including its license and other licensing options, visit 21 | * 22 | * http://www.pcg-random.org 23 | */ 24 | 25 | #include "pcg64.h" 26 | 27 | extern inline void pcg_setseq_128_step_r(pcg_state_setseq_128* rng); 28 | extern inline uint64_t pcg_output_xsl_rr_128_64(pcg128_t state); 29 | extern inline void pcg_setseq_128_srandom_r(pcg_state_setseq_128* rng, 30 | pcg128_t initstate, pcg128_t initseq); 31 | extern inline uint64_t 32 | pcg_setseq_128_xsl_rr_64_random_r(pcg_state_setseq_128* rng); 33 | extern inline uint64_t 34 | pcg_setseq_128_xsl_rr_64_boundedrand_r(pcg_state_setseq_128* rng, 35 | uint64_t bound); 36 | extern inline void pcg_setseq_128_advance_r(pcg_state_setseq_128* rng, pcg128_t delta); 37 | 38 | /* Multi-step advance functions (jump-ahead, jump-back) 39 | * 40 | * The method used here is based on Brown, "Random Number Generation 41 | * with Arbitrary Stride,", Transactions of the American Nuclear 42 | * Society (Nov. 1994). The algorithm is very similar to fast 43 | * exponentiation. 44 | * 45 | * Even though delta is an unsigned integer, we can pass a 46 | * signed integer to go backwards, it just goes "the long way round". 47 | */ 48 | 49 | #ifndef PCG_EMULATED_128BIT_MATH 50 | 51 | pcg128_t pcg_advance_lcg_128(pcg128_t state, pcg128_t delta, pcg128_t cur_mult, 52 | pcg128_t cur_plus) 53 | { 54 | pcg128_t acc_mult = 1u; 55 | pcg128_t acc_plus = 0u; 56 | while (delta > 0) { 57 | if (delta & 1) { 58 | acc_mult *= cur_mult; 59 | acc_plus = acc_plus * cur_mult + cur_plus; 60 | } 61 | cur_plus = (cur_mult + 1) * cur_plus; 62 | cur_mult *= cur_mult; 63 | delta /= 2; 64 | } 65 | return acc_mult * state + acc_plus; 66 | } 67 | 68 | #else 69 | 70 | pcg128_t pcg_advance_lcg_128(pcg128_t state, pcg128_t delta, pcg128_t cur_mult, 71 | pcg128_t cur_plus) 72 | { 73 | pcg128_t acc_mult = PCG_128BIT_CONSTANT(0u, 1u); 74 | pcg128_t acc_plus = PCG_128BIT_CONSTANT(0u, 0u); 75 | while ((delta.high > 0) || (delta.low > 0)) { 76 | if (delta.low & 1) { 77 | acc_mult = _pcg128_mult(acc_mult, cur_mult); 78 | acc_plus = _pcg128_add(_pcg128_mult(acc_plus, cur_mult), cur_plus); 79 | } 80 | cur_plus = _pcg128_mult(_pcg128_add(cur_mult, PCG_128BIT_CONSTANT(0u, 1u)), cur_plus); 81 | cur_mult = _pcg128_mult(cur_mult, cur_mult); 82 | delta.low >>= 1; 83 | delta.low += delta.high & 1; 84 | delta.high >>= 1; 85 | } 86 | return _pcg128_add(_pcg128_mult(acc_mult, state), acc_plus); 87 | } 88 | 89 | #endif -------------------------------------------------------------------------------- /randomstate/src/random-kit/random-kit-jump.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "random-kit.h" 3 | #include 4 | 5 | /* parameters for computing Jump */ 6 | #define W_SIZE 32 /* size of unsigned long */ 7 | #define MEXP 19937 8 | #define P_SIZE ((MEXP/W_SIZE)+1) 9 | #define LSB 0x00000001UL 10 | #define QQ 7 11 | #define LL 128 /* LL = 2^(QQ) */ 12 | 13 | 14 | void randomkit_jump(randomkit_state * state, const char * jump_str); 15 | 16 | void set_coef(unsigned long *pf, unsigned int deg, unsigned long v); -------------------------------------------------------------------------------- /randomstate/src/random-kit/random-kit-jump.h.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* operations for polynomials */ 5 | void random_poly(unsigned long *); 6 | void gray_code(void); 7 | unsigned long get_coef(unsigned long *, unsigned int); 8 | void set_coef(unsigned long *, unsigned int, unsigned long); 9 | 10 | /* operations for states */ 11 | State *calc_state(unsigned long *, State *); 12 | void copy_state(State *, State *); 13 | State *horner1(unsigned long *, State *); 14 | void gen_next(State *); 15 | void add_state(State *, State *); 16 | int compare_state(State *, State *); 17 | void gen_vec_h(State *); 18 | -------------------------------------------------------------------------------- /randomstate/src/random-kit/random-kit-test-gen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "randomkit.h" 4 | 5 | int main(void) 6 | { 7 | int i; 8 | uint32_t temp, seed = 1UL; 9 | randomkit_state state = {{ 0 }}; 10 | randomkit_seed(&state, seed); 11 | 12 | FILE *fp; 13 | fp = fopen("randomkit-testset-1.csv", "w"); 14 | if(fp == NULL){ 15 | printf("Couldn't open file\n"); 16 | return -1; 17 | } 18 | fprintf(fp, "seed, %" PRIu32 "\n", seed); 19 | for (i=0; i < 1000; i++) 20 | { 21 | temp = randomkit_random(&state); 22 | fprintf(fp, "%d, %" PRIu32 "\n", i, temp); 23 | printf("%d, %" PRIu32 "\n", i, temp); 24 | } 25 | fclose(fp); 26 | 27 | seed = 123456789UL; 28 | randomkit_seed(&state, seed); 29 | fp = fopen("randomkit-testset-2.csv", "w"); 30 | if(fp == NULL){ 31 | printf("Couldn't open file\n"); 32 | return -1; 33 | } 34 | fprintf(fp, "seed, %" PRIu32 "\n", seed); 35 | for (i=0; i < 1000; i++) 36 | { 37 | temp = randomkit_random(&state); 38 | fprintf(fp, "%d, %" PRIu32 "\n", i, temp); 39 | printf("%d, %" PRIu32 "\n", i, temp); 40 | } 41 | fclose(fp); 42 | } -------------------------------------------------------------------------------- /randomstate/src/random-kit/random-kit.c: -------------------------------------------------------------------------------- 1 | #include "random-kit.h" 2 | 3 | extern inline uint32_t randomkit_random(randomkit_state *state); 4 | 5 | void randomkit_seed(randomkit_state *state, uint32_t seed) 6 | { 7 | int pos; 8 | seed &= 0xffffffffUL; 9 | 10 | /* Knuth's PRNG as used in the Mersenne Twister reference implementation */ 11 | for (pos = 0; pos < RK_STATE_LEN; pos++) { 12 | state->key[pos] = seed; 13 | seed = (1812433253UL * (seed ^ (seed >> 30)) + pos + 1) & 0xffffffffUL; 14 | } 15 | state->pos = RK_STATE_LEN; 16 | } 17 | 18 | 19 | /* initializes mt[RK_STATE_LEN] with a seed */ 20 | static void init_genrand(randomkit_state *state, uint32_t s) 21 | { 22 | int mti; 23 | uint32_t *mt = state->key; 24 | 25 | mt[0] = s & 0xffffffffUL; 26 | for (mti = 1; mti < RK_STATE_LEN; mti++) { 27 | /* 28 | * See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. 29 | * In the previous versions, MSBs of the seed affect 30 | * only MSBs of the array mt[]. 31 | * 2002/01/09 modified by Makoto Matsumoto 32 | */ 33 | mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 34 | /* for > 32 bit machines */ 35 | mt[mti] &= 0xffffffffUL; 36 | } 37 | state->pos = mti; 38 | return; 39 | } 40 | 41 | 42 | /* 43 | * initialize by an array with array-length 44 | * init_key is the array for initializing keys 45 | * key_length is its length 46 | */ 47 | void randomkit_init_by_array(randomkit_state *state, uint32_t *init_key, int key_length) 48 | { 49 | /* was signed in the original code. RDH 12/16/2002 */ 50 | int i = 1; 51 | int j = 0; 52 | uint32_t *mt = state->key; 53 | int k; 54 | 55 | init_genrand(state, 19650218UL); 56 | k = (RK_STATE_LEN > key_length ? RK_STATE_LEN : key_length); 57 | for (; k; k--) { 58 | /* non linear */ 59 | mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525UL)) 60 | + init_key[j] + j; 61 | /* for > 32 bit machines */ 62 | mt[i] &= 0xffffffffUL; 63 | i++; 64 | j++; 65 | if (i >= RK_STATE_LEN) { 66 | mt[0] = mt[RK_STATE_LEN - 1]; 67 | i = 1; 68 | } 69 | if (j >= key_length) { 70 | j = 0; 71 | } 72 | } 73 | for (k = RK_STATE_LEN - 1; k; k--) { 74 | mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) 75 | - i; /* non linear */ 76 | mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ 77 | i++; 78 | if (i >= RK_STATE_LEN) { 79 | mt[0] = mt[RK_STATE_LEN - 1]; 80 | i = 1; 81 | } 82 | } 83 | 84 | mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 85 | } 86 | 87 | void randomkit_gen(randomkit_state *state) 88 | { 89 | uint32_t y; 90 | int i; 91 | 92 | for (i = 0; i < N - M; i++) { 93 | y = (state->key[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK); 94 | state->key[i] = state->key[i+M] ^ (y>>1) ^ (-(y & 1) & MATRIX_A); 95 | } 96 | for (; i < N - 1; i++) { 97 | y = (state->key[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK); 98 | state->key[i] = state->key[i+(M-N)] ^ (y>>1) ^ (-(y & 1) & MATRIX_A); 99 | } 100 | y = (state->key[N - 1] & UPPER_MASK) | (state->key[0] & LOWER_MASK); 101 | state->key[N - 1] = state->key[M - 1] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A); 102 | 103 | state->pos = 0; 104 | } 105 | 106 | -------------------------------------------------------------------------------- /randomstate/src/random-kit/random-kit.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #ifdef _WIN32 4 | #include "../common/stdint.h" 5 | #else 6 | #include 7 | #endif 8 | 9 | #ifdef _WIN32 10 | #define inline __forceinline 11 | #endif 12 | 13 | 14 | #define RK_STATE_LEN 624 15 | 16 | #define N 624 17 | #define M 397 18 | #define MATRIX_A 0x9908b0dfUL 19 | #define UPPER_MASK 0x80000000UL 20 | #define LOWER_MASK 0x7fffffffUL 21 | 22 | typedef struct s_randomkit_state 23 | { 24 | uint32_t key[RK_STATE_LEN]; 25 | int pos; 26 | } 27 | randomkit_state; 28 | 29 | extern void randomkit_seed(randomkit_state *state, uint32_t seed); 30 | 31 | extern void randomkit_gen(randomkit_state *state); 32 | 33 | /* Slightly optimized reference implementation of the Mersenne Twister */ 34 | static inline uint32_t randomkit_random(randomkit_state *state) 35 | { 36 | uint32_t y; 37 | 38 | if (state->pos == RK_STATE_LEN) { 39 | // Move to function to help inlining 40 | randomkit_gen(state); 41 | } 42 | y = state->key[state->pos++]; 43 | 44 | /* Tempering */ 45 | y ^= (y >> 11); 46 | y ^= (y << 7) & 0x9d2c5680UL; 47 | y ^= (y << 15) & 0xefc60000UL; 48 | y ^= (y >> 18); 49 | 50 | return y; 51 | } 52 | 53 | extern void randomkit_init_by_array(randomkit_state *state, uint32_t *init_key, int key_length); 54 | -------------------------------------------------------------------------------- /randomstate/src/sfmt/SFMT-calc-jump.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef SFMT_CALC_JUMP_HPP 3 | #define SFMT_CALC_JUMP_HPP 4 | /** 5 | * @file SFMT-calc-jump.hpp 6 | * 7 | * @brief functions for calculating jump polynomial. 8 | * 9 | * @author Mutsuo Saito (Hiroshima University) 10 | * @author Makoto Matsumoto (The University of Tokyo) 11 | * 12 | * Copyright (C) 2012 Mutsuo Saito, Makoto Matsumoto, 13 | * Hiroshima University and The University of Tokyo. 14 | * All rights reserved. 15 | * 16 | * The 3-clause BSD License is applied to this software, see 17 | * LICENSE.txt 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | namespace sfmt { 25 | /** 26 | * converts polynomial to string for convenient use in C language. 27 | * @param x output string 28 | * @param polynomial 29 | */ 30 | static inline void polytostring(std::string& x, NTL::GF2X& polynomial) 31 | { 32 | using namespace NTL; 33 | using namespace std; 34 | 35 | long degree = deg(polynomial); 36 | int buff; 37 | stringstream ss; 38 | for (int i = 0; i <= degree; i+=4) { 39 | buff = 0; 40 | for (int j = 0; j < 4; j++) { 41 | if (IsOne(coeff(polynomial, i + j))) { 42 | buff |= 1 << j; 43 | } else { 44 | buff &= (0x0f ^ (1 << j)); 45 | } 46 | } 47 | ss << hex << buff; 48 | } 49 | ss << flush; 50 | x = ss.str(); 51 | } 52 | 53 | /** 54 | * converts string to polynomial 55 | * @param str string 56 | * @param poly output polynomial 57 | */ 58 | static inline void stringtopoly(NTL::GF2X& poly, std::string& str) 59 | { 60 | using namespace NTL; 61 | using namespace std; 62 | 63 | stringstream ss(str); 64 | char c; 65 | long p = 0; 66 | clear(poly); 67 | while(ss) { 68 | ss >> c; 69 | if (!ss) { 70 | break; 71 | } 72 | if (c >= 'a') { 73 | c = c - 'a' + 10; 74 | } else { 75 | c = c - '0'; 76 | } 77 | for (int j = 0; j < 4; j++) { 78 | if (c & (1 << j)) { 79 | SetCoeff(poly, p, 1); 80 | } else { 81 | SetCoeff(poly, p, 0); 82 | } 83 | p++; 84 | } 85 | } 86 | } 87 | 88 | /** 89 | * calculate the jump polynomial. 90 | * SFMT generates 4 32-bit integers from one internal state. 91 | * @param jump_str output string which represents jump polynomial. 92 | * @param step jump step of internal state 93 | * @param characteristic polynomial 94 | */ 95 | static inline void calc_jump(std::string& jump_str, 96 | NTL::ZZ& step, 97 | NTL::GF2X& characteristic) 98 | { 99 | using namespace NTL; 100 | using namespace std; 101 | GF2X jump; 102 | PowerXMod(jump, step, characteristic); 103 | polytostring(jump_str, jump); 104 | } 105 | } 106 | #endif 107 | -------------------------------------------------------------------------------- /randomstate/src/sfmt/calc-jump.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file calc-jump.cpp 3 | * 4 | * @brief calc jump function. 5 | * 6 | * @author Mutsuo Saito (Hiroshima University) 7 | * @author Makoto Matsumoto (The University of Tokyo) 8 | * 9 | * Copyright (C) 2012 Mutsuo Saito, Makoto Matsumoto, 10 | * Hiroshima University and The University of Tokyo. 11 | * All rights reserved. 12 | * 13 | * The 3-clause BSD License is applied to this software, see 14 | * LICENSE.txt 15 | * 16 | * Compile: 17 | * g++ calc-jump.cpp -o calc-jump -lntl 18 | * 19 | * Run with 2^128 steps: 20 | * ./calc-jump 340282366920938463463374607431768211456 characteristic.19937.txt > sfmt-poly-128.txt 21 | * 22 | */ 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include "SFMT-calc-jump.hpp" 35 | 36 | using namespace NTL; 37 | using namespace std; 38 | using namespace sfmt; 39 | 40 | static void read_file(GF2X& characteristic, long line_no, const string& file); 41 | 42 | int main(int argc, char * argv[]) { 43 | if (argc <= 2) { 44 | cout << argv[0] << " jump-step characteristic-file [no.]" << endl; 45 | cout << " jump-step: a number between zero and 2^{SFMT_MEXP}-1.\n" 46 | << " large decimal number is allowed." << endl; 47 | cout << " characteristic-file: one of characteristic.{MEXP}.txt " 48 | << "file" << endl; 49 | cout << " [no.]: shows which characteristic polynomial in \n" 50 | << " the file should be used. 0 is used if omitted.\n" 51 | << " this is used for files in params directory." 52 | << endl; 53 | return -1; 54 | } 55 | string step_string = argv[1]; 56 | string filename = argv[2]; 57 | long no = 0; 58 | if (argc > 3) { 59 | no = strtol(argv[3], NULL, 10); 60 | } 61 | GF2X characteristic; 62 | read_file(characteristic, no, filename); 63 | ZZ step; 64 | stringstream ss(step_string); 65 | ss >> step; 66 | string jump_str; 67 | calc_jump(jump_str, step, characteristic); 68 | cout << "jump polynomial:" << endl; 69 | cout << jump_str << endl; 70 | return 0; 71 | } 72 | 73 | 74 | static void read_file(GF2X& characteristic, long line_no, const string& file) 75 | { 76 | ifstream ifs(file.c_str()); 77 | string line; 78 | for (int i = 0; i < line_no; i++) { 79 | ifs >> line; 80 | ifs >> line; 81 | } 82 | if (ifs) { 83 | ifs >> line; 84 | line = ""; 85 | ifs >> line; 86 | } 87 | stringtopoly(characteristic, line); 88 | } 89 | -------------------------------------------------------------------------------- /randomstate/src/sfmt/sfmt-jump.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file SFMT-jump.c 3 | * 4 | * @brief do jump using jump polynomial. 5 | * 6 | * @author Mutsuo Saito (Hiroshima University) 7 | * @author Makoto Matsumoto (The University of Tokyo) 8 | * 9 | * Copyright (C) 2012 Mutsuo Saito, Makoto Matsumoto, 10 | * Hiroshima University and The University of Tokyo. 11 | * All rights reserved. 12 | * 13 | * The 3-clause BSD License is applied to this software, see 14 | * LICENSE.txt 15 | */ 16 | 17 | #include "sfmt-jump.h" 18 | #include "sfmt-common.h" 19 | #include "sfmt-params.h" 20 | #include "sfmt.h" 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #if defined(__cplusplus) 27 | extern "C" { 28 | #endif 29 | 30 | inline static void next_state(sfmt_t *sfmt); 31 | 32 | #if defined(HAVE_SSE2) 33 | /** 34 | * add internal state of src to dest as F2-vector. 35 | * @param dest destination state 36 | * @param src source state 37 | */ 38 | inline static void add(sfmt_t *dest, sfmt_t *src) { 39 | int dp = dest->idx / 4; 40 | int sp = src->idx / 4; 41 | int diff = (sp - dp + SFMT_N) % SFMT_N; 42 | int p; 43 | int i; 44 | for (i = 0; i < SFMT_N - diff; i++) { 45 | p = i + diff; 46 | dest->state[i].si = _mm_xor_si128(dest->state[i].si, src->state[p].si); 47 | } 48 | for (; i < SFMT_N; i++) { 49 | p = i + diff - SFMT_N; 50 | dest->state[i].si = _mm_xor_si128(dest->state[i].si, src->state[p].si); 51 | } 52 | } 53 | #else 54 | inline static void add(sfmt_t *dest, sfmt_t *src) { 55 | int dp = dest->idx / 4; 56 | int sp = src->idx / 4; 57 | int diff = (sp - dp + SFMT_N) % SFMT_N; 58 | int p; 59 | int i, j; 60 | for (i = 0; i < SFMT_N - diff; i++) { 61 | p = i + diff; 62 | for (int j = 0; j < 4; j++) { 63 | dest->state[i].u[j] ^= src->state[p].u[j]; 64 | } 65 | } 66 | for (; i < SFMT_N; i++) { 67 | p = i + diff - SFMT_N; 68 | for (j = 0; j < 4; j++) { 69 | dest->state[i].u[j] ^= src->state[p].u[j]; 70 | } 71 | } 72 | } 73 | #endif 74 | 75 | /** 76 | * calculate next state 77 | * @param sfmt SFMT internal state 78 | */ 79 | inline static void next_state(sfmt_t *sfmt) { 80 | int idx = (sfmt->idx / 4) % SFMT_N; 81 | w128_t *r1, *r2; 82 | w128_t *pstate = sfmt->state; 83 | 84 | r1 = &pstate[(idx + SFMT_N - 2) % SFMT_N]; 85 | r2 = &pstate[(idx + SFMT_N - 1) % SFMT_N]; 86 | do_recursion(&pstate[idx], &pstate[idx], &pstate[(idx + SFMT_POS1) % SFMT_N], 87 | r1, r2); 88 | r1 = r2; 89 | r2 = &pstate[idx]; 90 | sfmt->idx = sfmt->idx + 4; 91 | } 92 | 93 | /** 94 | * jump ahead using jump_string 95 | * @param sfmt SFMT internal state input and output. 96 | * @param jump_string string which represents jump polynomial. 97 | */ 98 | void SFMT_jump(sfmt_t *sfmt, const char *jump_string) { 99 | sfmt_t work; 100 | int index = sfmt->idx; 101 | int bits, i, j; 102 | memset(&work, 0, sizeof(sfmt_t)); 103 | sfmt->idx = SFMT_N32; 104 | 105 | for (i = 0; jump_string[i] != '\0'; i++) { 106 | bits = jump_string[i]; 107 | assert(isxdigit(bits)); 108 | bits = tolower(bits); 109 | if (bits >= 'a' && bits <= 'f') { 110 | bits = bits - 'a' + 10; 111 | } else { 112 | bits = bits - '0'; 113 | } 114 | bits = bits & 0x0f; 115 | for (j = 0; j < 4; j++) { 116 | if ((bits & 1) != 0) { 117 | add(&work, sfmt); 118 | } 119 | next_state(sfmt); 120 | bits = bits >> 1; 121 | } 122 | } 123 | *sfmt = work; 124 | sfmt->idx = index; 125 | } 126 | 127 | #if defined(__cplusplus) 128 | } 129 | #endif 130 | -------------------------------------------------------------------------------- /randomstate/src/sfmt/sfmt-jump.h: -------------------------------------------------------------------------------- 1 | #ifndef SFMT_JUMP_H 2 | #define SFMT_JUMP_H 3 | /** 4 | * @file SFMT-jump.h 5 | * 6 | * @brief jump header file. 7 | * 8 | * @author Mutsuo Saito (Hiroshima University) 9 | * @author Makoto Matsumoto (The University of Tokyo) 10 | * 11 | * Copyright (C) 2012 Mutsuo Saito, Makoto Matsumoto, 12 | * Hiroshima University and The University of Tokyo. 13 | * All rights reserved. 14 | * 15 | * The 3-clause BSD License is applied to this software, see 16 | * LICENSE.txt 17 | */ 18 | #if defined(__cplusplus) 19 | extern "C" { 20 | #endif 21 | 22 | #include "sfmt.h" 23 | void SFMT_jump(sfmt_t *sfmt, const char *jump_str); 24 | 25 | #if defined(__cplusplus) 26 | } 27 | #endif 28 | #endif 29 | -------------------------------------------------------------------------------- /randomstate/src/sfmt/sfmt-neon.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file SFMT-neon.h 3 | * @brief SIMD oriented Fast Mersenne Twister(SFMT) for ARM with 128b NEON 4 | * 5 | * @author Masaki Ota 6 | * 7 | * @note We assume LITTLE ENDIAN in this file 8 | */ 9 | 10 | #ifndef SFMT_NEON_H 11 | #define SFMT_NEON_H 12 | 13 | inline static void neon_recursion(uint32x4_t *r, uint32x4_t a, uint32x4_t b, 14 | uint32x4_t c, uint32x4_t d); 15 | 16 | /** 17 | * This function represents the recursion formula. 18 | * @param r an output 19 | * @param a a 128-bit part of the interal state array 20 | * @param b a 128-bit part of the interal state array 21 | * @param c a 128-bit part of the interal state array 22 | * @param d a 128-bit part of the interal state array 23 | */ 24 | inline static void neon_recursion(uint32x4_t *r, uint32x4_t a, uint32x4_t b, 25 | uint32x4_t c, uint32x4_t d) { 26 | uint32x4_t v, x, y, z; 27 | static const uint32x4_t vzero = {0, 0, 0, 0}; 28 | static const uint32x4_t vmask = {SFMT_MSK1, SFMT_MSK2, SFMT_MSK3, SFMT_MSK4}; 29 | 30 | #define rotate_bytes(A, B, C) \ 31 | vreinterpretq_u32_u8( \ 32 | vextq_u8(vreinterpretq_u8_u32(A), vreinterpretq_u8_u32(B), (C))) 33 | 34 | y = vshrq_n_u32(b, SFMT_SR1); 35 | z = rotate_bytes(c, vzero, SFMT_SR2); 36 | v = vshlq_n_u32(d, SFMT_SL1); 37 | z = veorq_u32(z, a); 38 | z = veorq_u32(z, v); 39 | x = rotate_bytes(vzero, a, 16 - SFMT_SL2); 40 | y = vandq_u32(y, vmask); 41 | z = veorq_u32(z, x); 42 | z = veorq_u32(z, y); 43 | *r = z; 44 | } 45 | 46 | /** 47 | * This function fills the internal state array with pseudorandom 48 | * integers. 49 | * @param sfmt SFMT internal state 50 | */ 51 | void sfmt_gen_rand_all(sfmt_t *sfmt) { 52 | int i; 53 | uint32x4_t r1, r2; 54 | w128_t *pstate = sfmt->state; 55 | 56 | r1 = pstate[SFMT_N - 2].si; 57 | r2 = pstate[SFMT_N - 1].si; 58 | for (i = 0; i < SFMT_N - SFMT_POS1; i++) { 59 | neon_recursion(&pstate[i].si, pstate[i].si, pstate[i + SFMT_POS1].si, r1, 60 | r2); 61 | r1 = r2; 62 | r2 = pstate[i].si; 63 | } 64 | for (; i < SFMT_N; i++) { 65 | neon_recursion(&pstate[i].si, pstate[i].si, 66 | pstate[i + SFMT_POS1 - SFMT_N].si, r1, r2); 67 | r1 = r2; 68 | r2 = pstate[i].si; 69 | } 70 | } 71 | 72 | /** 73 | * This function fills the user-specified array with pseudorandom 74 | * integers. 75 | * @param sfmt SFMT internal state. 76 | * @param array an 128-bit array to be filled by pseudorandom numbers. 77 | * @param size number of 128-bit pseudorandom numbers to be generated. 78 | */ 79 | static void gen_rand_array(sfmt_t *sfmt, w128_t *array, int size) { 80 | int i, j; 81 | uint32x4_t r1, r2; 82 | w128_t *pstate = sfmt->state; 83 | 84 | r1 = pstate[SFMT_N - 2].si; 85 | r2 = pstate[SFMT_N - 1].si; 86 | for (i = 0; i < SFMT_N - SFMT_POS1; i++) { 87 | neon_recursion(&array[i].si, pstate[i].si, pstate[i + SFMT_POS1].si, r1, 88 | r2); 89 | r1 = r2; 90 | r2 = array[i].si; 91 | } 92 | for (; i < SFMT_N; i++) { 93 | neon_recursion(&array[i].si, pstate[i].si, array[i + SFMT_POS1 - SFMT_N].si, 94 | r1, r2); 95 | r1 = r2; 96 | r2 = array[i].si; 97 | } 98 | for (; i < size - SFMT_N; i++) { 99 | neon_recursion(&array[i].si, array[i - SFMT_N].si, 100 | array[i + SFMT_POS1 - SFMT_N].si, r1, r2); 101 | r1 = r2; 102 | r2 = array[i].si; 103 | } 104 | for (j = 0; j < 2 * SFMT_N - size; j++) { 105 | pstate[j] = array[j + size - SFMT_N]; 106 | } 107 | for (; i < size; i++, j++) { 108 | neon_recursion(&array[i].si, array[i - SFMT_N].si, 109 | array[i + SFMT_POS1 - SFMT_N].si, r1, r2); 110 | r1 = r2; 111 | r2 = pstate[j].si = array[i].si; 112 | } 113 | } 114 | 115 | #endif 116 | -------------------------------------------------------------------------------- /randomstate/src/sfmt/sfmt-params.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef SFMT_PARAMS_H 3 | #define SFMT_PARAMS_H 4 | 5 | #if !defined(SFMT_MEXP) 6 | #if defined(__GNUC__) && !defined(__ICC) 7 | #warning "SFMT_MEXP is not defined. I assume MEXP is 19937." 8 | #endif 9 | #define SFMT_MEXP 19937 10 | #endif 11 | /*----------------- 12 | BASIC DEFINITIONS 13 | -----------------*/ 14 | /** Mersenne Exponent. The period of the sequence 15 | * is a multiple of 2^MEXP-1. 16 | * #define SFMT_MEXP 19937 */ 17 | /** SFMT generator has an internal state array of 128-bit integers, 18 | * and N is its size. */ 19 | #define SFMT_N (SFMT_MEXP / 128 + 1) 20 | /** N32 is the size of internal state array when regarded as an array 21 | * of 32-bit integers.*/ 22 | #define SFMT_N32 (SFMT_N * 4) 23 | /** N64 is the size of internal state array when regarded as an array 24 | * of 64-bit integers.*/ 25 | #define SFMT_N64 (SFMT_N * 2) 26 | 27 | /*---------------------- 28 | the parameters of SFMT 29 | following definitions are in paramsXXXX.h file. 30 | ----------------------*/ 31 | /** the pick up position of the array. 32 | #define SFMT_POS1 122 33 | */ 34 | 35 | /** the parameter of shift left as four 32-bit registers. 36 | #define SFMT_SL1 18 37 | */ 38 | 39 | /** the parameter of shift left as one 128-bit register. 40 | * The 128-bit integer is shifted by (SFMT_SL2 * 8) bits. 41 | #define SFMT_SL2 1 42 | */ 43 | 44 | /** the parameter of shift right as four 32-bit registers. 45 | #define SFMT_SR1 11 46 | */ 47 | 48 | /** the parameter of shift right as one 128-bit register. 49 | * The 128-bit integer is shifted by (SFMT_SR2 * 8) bits. 50 | #define SFMT_SR2 1 51 | */ 52 | 53 | /** A bitmask, used in the recursion. These parameters are introduced 54 | * to break symmetry of SIMD. 55 | #define SFMT_MSK1 0xdfffffefU 56 | #define SFMT_MSK2 0xddfecb7fU 57 | #define SFMT_MSK3 0xbffaffffU 58 | #define SFMT_MSK4 0xbffffff6U 59 | */ 60 | 61 | /** These definitions are part of a 128-bit period certification vector. 62 | #define SFMT_PARITY1 0x00000001U 63 | #define SFMT_PARITY2 0x00000000U 64 | #define SFMT_PARITY3 0x00000000U 65 | #define SFMT_PARITY4 0xc98e126aU 66 | */ 67 | 68 | #if SFMT_MEXP == 607 69 | #include "sfmt-params607.h" 70 | #elif SFMT_MEXP == 1279 71 | #include "sfmt-params1279.h" 72 | #elif SFMT_MEXP == 2281 73 | #include "sfmt-params2281.h" 74 | #elif SFMT_MEXP == 4253 75 | #include "sfmt-params4253.h" 76 | #elif SFMT_MEXP == 11213 77 | #include "sfmt-params11213.h" 78 | #elif SFMT_MEXP == 19937 79 | #include "sfmt-params19937.h" 80 | #elif SFMT_MEXP == 44497 81 | #include "sfmt-params44497.h" 82 | #elif SFMT_MEXP == 86243 83 | #include "sfmt-params86243.h" 84 | #elif SFMT_MEXP == 132049 85 | #include "sfmt-params132049.h" 86 | #elif SFMT_MEXP == 216091 87 | #include "sfmt-params216091.h" 88 | #else 89 | #if defined(__GNUC__) && !defined(__ICC) 90 | #error "SFMT_MEXP is not valid." 91 | #undef SFMT_MEXP 92 | #else 93 | #undef SFMT_MEXP 94 | #endif 95 | 96 | #endif 97 | 98 | #endif /* SFMT_PARAMS_H */ 99 | -------------------------------------------------------------------------------- /randomstate/src/sfmt/sfmt-params19937.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef SFMT_PARAMS19937_H 3 | #define SFMT_PARAMS19937_H 4 | 5 | #define SFMT_POS1 122 6 | #define SFMT_SL1 18 7 | #define SFMT_SL2 1 8 | #define SFMT_SR1 11 9 | #define SFMT_SR2 1 10 | #define SFMT_MSK1 0xdfffffefU 11 | #define SFMT_MSK2 0xddfecb7fU 12 | #define SFMT_MSK3 0xbffaffffU 13 | #define SFMT_MSK4 0xbffffff6U 14 | #define SFMT_PARITY1 0x00000001U 15 | #define SFMT_PARITY2 0x00000000U 16 | #define SFMT_PARITY3 0x00000000U 17 | #define SFMT_PARITY4 0x13c9e684U 18 | 19 | /* PARAMETERS FOR ALTIVEC */ 20 | #if defined(__APPLE__) /* For OSX */ 21 | #define SFMT_ALTI_SL1 \ 22 | (vector unsigned int)(SFMT_SL1, SFMT_SL1, SFMT_SL1, SFMT_SL1) 23 | #define SFMT_ALTI_SR1 \ 24 | (vector unsigned int)(SFMT_SR1, SFMT_SR1, SFMT_SR1, SFMT_SR1) 25 | #define SFMT_ALTI_MSK \ 26 | (vector unsigned int)(SFMT_MSK1, SFMT_MSK2, SFMT_MSK3, SFMT_MSK4) 27 | #define SFMT_ALTI_MSK64 \ 28 | (vector unsigned int)(SFMT_MSK2, SFMT_MSK1, SFMT_MSK4, SFMT_MSK3) 29 | #define SFMT_ALTI_SL2_PERM \ 30 | (vector unsigned char)(1, 2, 3, 23, 5, 6, 7, 0, 9, 10, 11, 4, 13, 14, 15, 8) 31 | #define SFMT_ALTI_SL2_PERM64 \ 32 | (vector unsigned char)(1, 2, 3, 4, 5, 6, 7, 31, 9, 10, 11, 12, 13, 14, 15, 0) 33 | #define SFMT_ALTI_SR2_PERM \ 34 | (vector unsigned char)(7, 0, 1, 2, 11, 4, 5, 6, 15, 8, 9, 10, 17, 12, 13, 14) 35 | #define SFMT_ALTI_SR2_PERM64 \ 36 | (vector unsigned char)(15, 0, 1, 2, 3, 4, 5, 6, 17, 8, 9, 10, 11, 12, 13, 14) 37 | #else /* For OTHER OSs(Linux?) */ 38 | #define SFMT_ALTI_SL1 \ 39 | { SFMT_SL1, SFMT_SL1, SFMT_SL1, SFMT_SL1 } 40 | #define SFMT_ALTI_SR1 \ 41 | { SFMT_SR1, SFMT_SR1, SFMT_SR1, SFMT_SR1 } 42 | #define SFMT_ALTI_MSK \ 43 | { SFMT_MSK1, SFMT_MSK2, SFMT_MSK3, SFMT_MSK4 } 44 | #define SFMT_ALTI_MSK64 \ 45 | { SFMT_MSK2, SFMT_MSK1, SFMT_MSK4, SFMT_MSK3 } 46 | #define SFMT_ALTI_SL2_PERM \ 47 | { 1, 2, 3, 23, 5, 6, 7, 0, 9, 10, 11, 4, 13, 14, 15, 8 } 48 | #define SFMT_ALTI_SL2_PERM64 \ 49 | { 1, 2, 3, 4, 5, 6, 7, 31, 9, 10, 11, 12, 13, 14, 15, 0 } 50 | #define SFMT_ALTI_SR2_PERM \ 51 | { 7, 0, 1, 2, 11, 4, 5, 6, 15, 8, 9, 10, 17, 12, 13, 14 } 52 | #define SFMT_ALTI_SR2_PERM64 \ 53 | { 15, 0, 1, 2, 3, 4, 5, 6, 17, 8, 9, 10, 11, 12, 13, 14 } 54 | #endif /* For OSX */ 55 | #define SFMT_IDSTR \ 56 | "SFMT-19937:122-18-1-11-1:dfffffef-ddfecb7f-bffaffff-bffffff6" 57 | 58 | #endif /* SFMT_PARAMS19937_H */ 59 | -------------------------------------------------------------------------------- /randomstate/src/sfmt/sfmt-sse2.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /** 3 | * @file SFMT-sse2.h 4 | * @brief SIMD oriented Fast Mersenne Twister(SFMT) for Intel SSE2 5 | * 6 | * @author Mutsuo Saito (Hiroshima University) 7 | * @author Makoto Matsumoto (Hiroshima University) 8 | * 9 | * @note We assume LITTLE ENDIAN in this file 10 | * 11 | * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima 12 | * University. All rights reserved. 13 | * 14 | * The new BSD License is applied to this software, see LICENSE.txt 15 | */ 16 | 17 | #ifndef SFMT_SSE2_H 18 | #define SFMT_SSE2_H 19 | 20 | inline static void mm_recursion(__m128i *r, __m128i a, __m128i b, __m128i c, 21 | __m128i d); 22 | 23 | /** 24 | * This function represents the recursion formula. 25 | * @param r an output 26 | * @param a a 128-bit part of the interal state array 27 | * @param b a 128-bit part of the interal state array 28 | * @param c a 128-bit part of the interal state array 29 | * @param d a 128-bit part of the interal state array 30 | */ 31 | inline static void mm_recursion(__m128i *r, __m128i a, __m128i b, __m128i c, 32 | __m128i d) { 33 | __m128i v, x, y, z; 34 | 35 | y = _mm_srli_epi32(b, SFMT_SR1); 36 | z = _mm_srli_si128(c, SFMT_SR2); 37 | v = _mm_slli_epi32(d, SFMT_SL1); 38 | z = _mm_xor_si128(z, a); 39 | z = _mm_xor_si128(z, v); 40 | x = _mm_slli_si128(a, SFMT_SL2); 41 | y = _mm_and_si128(y, sse2_param_mask.si); 42 | z = _mm_xor_si128(z, x); 43 | z = _mm_xor_si128(z, y); 44 | *r = z; 45 | } 46 | 47 | /** 48 | * This function fills the internal state array with pseudorandom 49 | * integers. 50 | * @param sfmt SFMT internal state 51 | */ 52 | void sfmt_gen_rand_all(sfmt_t *sfmt) { 53 | int i; 54 | __m128i r1, r2; 55 | w128_t *pstate = sfmt->state; 56 | 57 | r1 = pstate[SFMT_N - 2].si; 58 | r2 = pstate[SFMT_N - 1].si; 59 | for (i = 0; i < SFMT_N - SFMT_POS1; i++) { 60 | mm_recursion(&pstate[i].si, pstate[i].si, pstate[i + SFMT_POS1].si, r1, r2); 61 | r1 = r2; 62 | r2 = pstate[i].si; 63 | } 64 | for (; i < SFMT_N; i++) { 65 | mm_recursion(&pstate[i].si, pstate[i].si, pstate[i + SFMT_POS1 - SFMT_N].si, 66 | r1, r2); 67 | r1 = r2; 68 | r2 = pstate[i].si; 69 | } 70 | } 71 | 72 | /** 73 | * This function fills the user-specified array with pseudorandom 74 | * integers. 75 | * @param sfmt SFMT internal state. 76 | * @param array an 128-bit array to be filled by pseudorandom numbers. 77 | * @param size number of 128-bit pseudorandom numbers to be generated. 78 | */ 79 | static void gen_rand_array(sfmt_t *sfmt, w128_t *array, int size) { 80 | int i, j; 81 | __m128i r1, r2; 82 | w128_t *pstate = sfmt->state; 83 | 84 | r1 = pstate[SFMT_N - 2].si; 85 | r2 = pstate[SFMT_N - 1].si; 86 | for (i = 0; i < SFMT_N - SFMT_POS1; i++) { 87 | mm_recursion(&array[i].si, pstate[i].si, pstate[i + SFMT_POS1].si, r1, r2); 88 | r1 = r2; 89 | r2 = array[i].si; 90 | } 91 | for (; i < SFMT_N; i++) { 92 | mm_recursion(&array[i].si, pstate[i].si, array[i + SFMT_POS1 - SFMT_N].si, 93 | r1, r2); 94 | r1 = r2; 95 | r2 = array[i].si; 96 | } 97 | for (; i < size - SFMT_N; i++) { 98 | mm_recursion(&array[i].si, array[i - SFMT_N].si, 99 | array[i + SFMT_POS1 - SFMT_N].si, r1, r2); 100 | r1 = r2; 101 | r2 = array[i].si; 102 | } 103 | for (j = 0; j < 2 * SFMT_N - size; j++) { 104 | pstate[j] = array[j + size - SFMT_N]; 105 | } 106 | for (; i < size; i++, j++) { 107 | mm_recursion(&array[i].si, array[i - SFMT_N].si, 108 | array[i + SFMT_POS1 - SFMT_N].si, r1, r2); 109 | r1 = r2; 110 | r2 = array[i].si; 111 | pstate[j] = array[i]; 112 | } 113 | } 114 | 115 | #endif 116 | -------------------------------------------------------------------------------- /randomstate/src/sfmt/sfmt-test-gen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * gcc dSFMT-test-gen.c dSFMT.c -DHAVE_SSE2 -DDSFMT_MEXP=19937 -o dSFMT 4 | */ 5 | #include "sfmt.h" 6 | #include 7 | #include 8 | 9 | 10 | int main(void) { 11 | int i; 12 | uint64_t *temp; 13 | uint32_t seed = 1UL; 14 | sfmt_t state; 15 | sfmt_init_gen_rand(&state, seed); 16 | uint64_t out[1000]; 17 | sfmt_fill_array64(&state, out, 1000); 18 | 19 | FILE *fp; 20 | fp = fopen("sfmt-testset-1.csv", "w"); 21 | if (fp == NULL) { 22 | printf("Couldn't open file\n"); 23 | return -1; 24 | } 25 | fprintf(fp, "seed, %" PRIu32 "\n", seed); 26 | for (i = 0; i < 1000; i++) { 27 | fprintf(fp, "%d, %" PRIu64 "\n", i, out[i]); 28 | printf("%d, %" PRIu64 "\n", i, out[i]); 29 | } 30 | fclose(fp); 31 | 32 | seed = 123456789UL; 33 | sfmt_init_gen_rand(&state, seed); 34 | sfmt_fill_array64(&state, out, 1000); 35 | fp = fopen("sfmt-testset-2.csv", "w"); 36 | if (fp == NULL) { 37 | printf("Couldn't open file\n"); 38 | return -1; 39 | } 40 | fprintf(fp, "seed, %" PRIu32 "\n", seed); 41 | for (i = 0; i < 1000; i++) { 42 | fprintf(fp, "%d, %" PRIu64 "\n", i, out[i]); 43 | printf("%d, %" PRIu64 "\n", i, out[i]); 44 | } 45 | fclose(fp); 46 | } 47 | -------------------------------------------------------------------------------- /randomstate/src/splitmix64/splitmix64-original.c: -------------------------------------------------------------------------------- 1 | /* Written in 2015 by Sebastiano Vigna (vigna@acm.org) 2 | 3 | To the extent possible under law, the author has dedicated all copyright 4 | and related and neighboring rights to this software to the public domain 5 | worldwide. This software is distributed without any warranty. 6 | 7 | See . */ 8 | 9 | #include 10 | 11 | /* This is a fixed-increment version of Java 8's SplittableRandom generator 12 | See http://dx.doi.org/10.1145/2714064.2660195 and 13 | http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html 14 | 15 | It is a very fast generator passing BigCrush, and it can be useful if 16 | for some reason you absolutely want 64 bits of state; otherwise, we 17 | rather suggest to use a xorshift128+ (for moderately parallel 18 | computations) or xorshift1024* (for massively parallel computations) 19 | generator. */ 20 | 21 | inline uint64_t next(uint64_t* x) { 22 | uint64_t z = (x += UINT64_C(0x9E3779B97F4A7C15)); 23 | z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9); 24 | z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB); 25 | return z ^ (z >> 31); 26 | } -------------------------------------------------------------------------------- /randomstate/src/splitmix64/splitmix64.c: -------------------------------------------------------------------------------- 1 | #include "splitmix64.h" 2 | 3 | inline uint64_t splitmix64_next(uint64_t* x); 4 | 5 | -------------------------------------------------------------------------------- /randomstate/src/splitmix64/splitmix64.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #include "../common/inttypes.h" 3 | #define inline __forceinline 4 | #else 5 | #include 6 | #endif 7 | 8 | inline uint64_t splitmix64_next(uint64_t* x) { 9 | uint64_t z = (*x += UINT64_C(0x9E3779B97F4A7C15)); 10 | z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9); 11 | z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB); 12 | return z ^ (z >> 31); 13 | } -------------------------------------------------------------------------------- /randomstate/src/xoroshiro128plus/xoroshiro128plus-original.c: -------------------------------------------------------------------------------- 1 | /* Written in 2016 by David Blackman and Sebastiano Vigna (vigna@acm.org) 2 | 3 | To the extent possible under law, the author has dedicated all copyright 4 | and related and neighboring rights to this software to the public domain 5 | worldwide. This software is distributed without any warranty. 6 | 7 | See . */ 8 | 9 | #include 10 | 11 | /* This is the successor to xorshift128+. It is the fastest full-period 12 | generator passing BigCrush without systematic failures, but due to the 13 | relatively short period it is acceptable only for applications with a 14 | mild amount of parallelism; otherwise, use a xorshift1024* generator. 15 | 16 | Beside passing BigCrush, this generator passes the PractRand test suite 17 | up to (and included) 16TB, with the exception of binary rank tests, 18 | which fail due to the lowest bit being an LFSR; all other bits pass all 19 | tests. We suggest to use a sign test to extract a random Boolean value. 20 | 21 | Note that the generator uses a simulated rotate operation, which most C 22 | compilers will turn into a single instruction. In Java, you can use 23 | Long.rotateLeft(). In languages that do not make low-level rotation 24 | instructions accessible xorshift128+ could be faster. 25 | 26 | The state must be seeded so that it is not everywhere zero. If you have 27 | a 64-bit seed, we suggest to seed a splitmix64 generator and use its 28 | output to fill s. */ 29 | 30 | uint64_t s[2]; 31 | 32 | static inline uint64_t rotl(const uint64_t x, int k) { 33 | return (x << k) | (x >> (64 - k)); 34 | } 35 | 36 | uint64_t next(void) { 37 | const uint64_t s0 = s[0]; 38 | uint64_t s1 = s[1]; 39 | const uint64_t result = s0 + s1; 40 | 41 | s1 ^= s0; 42 | s[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b 43 | s[1] = rotl(s1, 36); // c 44 | 45 | return result; 46 | } 47 | 48 | 49 | /* This is the jump function for the generator. It is equivalent 50 | to 2^64 calls to next(); it can be used to generate 2^64 51 | non-overlapping subsequences for parallel computations. */ 52 | 53 | void jump(void) { 54 | static const uint64_t JUMP[] = { 0xbeac0467eba5facb, 0xd86b048b86aa9922 }; 55 | 56 | uint64_t s0 = 0; 57 | uint64_t s1 = 0; 58 | for(int i = 0; i < sizeof JUMP / sizeof *JUMP; i++) 59 | for(int b = 0; b < 64; b++) { 60 | if (JUMP[i] & 1ULL << b) { 61 | s0 ^= s[0]; 62 | s1 ^= s[1]; 63 | } 64 | next(); 65 | } 66 | 67 | s[0] = s0; 68 | s[1] = s1; 69 | } 70 | -------------------------------------------------------------------------------- /randomstate/src/xoroshiro128plus/xoroshiro128plus-test.gen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "xoroshiro128plus.h" 4 | 5 | int main(void) 6 | { 7 | int i; 8 | uint64_t temp, seed = 1ULL; 9 | xoroshiro128plus_state state = {{ 0 }}; 10 | xoroshiro128plus_seed(&state, seed); 11 | 12 | FILE *fp; 13 | fp = fopen("xoroshiro128plus-testset-1.csv", "w"); 14 | if(fp == NULL){ 15 | printf("Couldn't open file\n"); 16 | return -1; 17 | } 18 | fprintf(fp, "seed, %" PRIu64 "\n", seed); 19 | for (i=0; i < 1000; i++) 20 | { 21 | temp = xoroshiro128plus_next(&state); 22 | fprintf(fp, "%d, %" PRIu64 "\n", i, temp); 23 | printf("%d, %" PRIu64 "\n", i, temp); 24 | } 25 | fclose(fp); 26 | 27 | seed = 12345678910111ULL; 28 | xoroshiro128plus_seed(&state, seed); 29 | fp = fopen("xoroshiro128plus-testset-2.csv", "w"); 30 | if(fp == NULL){ 31 | printf("Couldn't open file\n"); 32 | return -1; 33 | } 34 | fprintf(fp, "seed, %" PRIu64 "\n", seed); 35 | for (i=0; i < 1000; i++) 36 | { 37 | temp = xoroshiro128plus_next(&state); 38 | fprintf(fp, "%d, %" PRIu64 "\n", i, temp); 39 | printf("%d, %" PRIu64 "\n", i, temp); 40 | } 41 | fclose(fp); 42 | } -------------------------------------------------------------------------------- /randomstate/src/xoroshiro128plus/xoroshiro128plus.c: -------------------------------------------------------------------------------- 1 | #include "xoroshiro128plus.h" 2 | #include "../splitmix64/splitmix64.h" 3 | 4 | extern inline uint64_t xoroshiro128plus_next(xoroshiro128plus_state* state); 5 | 6 | extern inline uint64_t rotl(const uint64_t x, int k); 7 | 8 | void xoroshiro128plus_jump(xoroshiro128plus_state* state) { 9 | static const uint64_t JUMP[] = { 0xbeac0467eba5facb, 0xd86b048b86aa9922 }; 10 | 11 | int i, b; 12 | uint64_t s0 = 0; 13 | uint64_t s1 = 0; 14 | for(i = 0; i < sizeof JUMP / sizeof *JUMP; i++) 15 | for(b = 0; b < 64; b++) { 16 | if (JUMP[i] & 1ULL << b) { 17 | s0 ^= state->s[0]; 18 | s1 ^= state->s[1]; 19 | } 20 | xoroshiro128plus_next(state); 21 | } 22 | 23 | state->s[0] = s0; 24 | state->s[1] = s1; 25 | } 26 | 27 | void xoroshiro128plus_seed(xoroshiro128plus_state* state, uint64_t seed) 28 | { 29 | 30 | uint64_t seed_copy = seed; 31 | uint64_t state1 = splitmix64_next(&seed_copy); 32 | uint64_t state2 = splitmix64_next(&seed_copy); 33 | xoroshiro128plus_init_state(state, state1, state2); 34 | } 35 | 36 | void xoroshiro128plus_seed_by_array(xoroshiro128plus_state* state, uint64_t *seed_array, int count) 37 | { 38 | uint64_t initial_state[2] = {0}; 39 | uint64_t seed_copy = 0; 40 | int iter_bound = 2>=count ? 2 : count; 41 | int i, loc = 0; 42 | for (i = 0; i < iter_bound; i++) 43 | { 44 | if (i < count) 45 | seed_copy ^= seed_array[i]; 46 | initial_state[loc] = splitmix64_next(&seed_copy); 47 | loc ++; 48 | if (loc == 2) 49 | loc = 0; 50 | } 51 | xoroshiro128plus_init_state(state, initial_state[0], initial_state[1]); 52 | } 53 | 54 | void xoroshiro128plus_init_state(xoroshiro128plus_state* state, uint64_t seed, uint64_t inc) 55 | { 56 | state->s[0] = seed; 57 | state->s[1] = inc; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /randomstate/src/xoroshiro128plus/xoroshiro128plus.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #ifdef _WIN32 4 | #include "../common/stdint.h" 5 | #define inline __forceinline 6 | #else 7 | #include 8 | #endif 9 | 10 | typedef struct s_xoroshiro128plus_state 11 | { 12 | uint64_t s[2]; 13 | } xoroshiro128plus_state; 14 | 15 | void xoroshiro128plus_jump(xoroshiro128plus_state* state); 16 | 17 | void xoroshiro128plus_seed(xoroshiro128plus_state* state, uint64_t seed); 18 | 19 | void xoroshiro128plus_seed_by_array(xoroshiro128plus_state* state, uint64_t *seed_array, int count); 20 | 21 | void xoroshiro128plus_init_state(xoroshiro128plus_state* state, uint64_t seed, uint64_t inc); 22 | 23 | static inline uint64_t rotl(const uint64_t x, int k) { 24 | return (x << k) | (x >> (64 - k)); 25 | } 26 | 27 | static inline uint64_t xoroshiro128plus_next(xoroshiro128plus_state* state) { 28 | const uint64_t s0 = state->s[0]; 29 | uint64_t s1 = state->s[1]; 30 | const uint64_t result = s0 + s1; 31 | 32 | s1 ^= s0; 33 | state->s[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b 34 | state->s[1] = rotl(s1, 36); // c 35 | 36 | return result; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /randomstate/src/xorshift1024/xorshift1024-original.c: -------------------------------------------------------------------------------- 1 | /* Written in 2014-2015 by Sebastiano Vigna (vigna@acm.org) 2 | 3 | To the extent possible under law, the author has dedicated all copyright 4 | and related and neighboring rights to this software to the public domain 5 | worldwide. This software is distributed without any warranty. 6 | 7 | See . */ 8 | 9 | #include 10 | #include 11 | 12 | /* This is a fast, top-quality generator. If 1024 bits of state are too 13 | much, try a xorshift128+ generator. 14 | 15 | The state must be seeded so that it is not everywhere zero. If you have 16 | a 64-bit seed, we suggest to seed a splitmix64 generator and use its 17 | output to fill s. */ 18 | 19 | uint64_t s[16]; 20 | int p; 21 | 22 | uint64_t next(void) { 23 | const uint64_t s0 = s[p]; 24 | uint64_t s1 = s[p = (p + 1) & 15]; 25 | s1 ^= s1 << 31; // a 26 | s[p] = s1 ^ s0 ^ (s1 >> 11) ^ (s0 >> 30); // b,c 27 | return s[p] * UINT64_C(1181783497276652981); 28 | } 29 | 30 | 31 | /* This is the jump function for the generator. It is equivalent 32 | to 2^512 calls to next(); it can be used to generate 2^512 33 | non-overlapping subsequences for parallel computations. */ 34 | 35 | void jump() { 36 | static const uint64_t JUMP[] = { 0x84242f96eca9c41dULL, 37 | 0xa3c65b8776f96855ULL, 0x5b34a39f070b5837ULL, 0x4489affce4f31a1eULL, 38 | 0x2ffeeb0a48316f40ULL, 0xdc2d9891fe68c022ULL, 0x3659132bb12fea70ULL, 39 | 0xaac17d8efa43cab8ULL, 0xc4cb815590989b13ULL, 0x5ee975283d71c93bULL, 40 | 0x691548c86c1bd540ULL, 0x7910c41d10a1e6a5ULL, 0x0b5fc64563b3e2a8ULL, 41 | 0x047f7684e9fc949dULL, 0xb99181f2d8f685caULL, 0x284600e3f30e38c3ULL 42 | }; 43 | 44 | uint64_t t[16] = { 0 }; 45 | for(int i = 0; i < sizeof JUMP / sizeof *JUMP; i++) 46 | for(int b = 0; b < 64; b++) { 47 | if (JUMP[i] & 1ULL << b) 48 | for(int j = 0; j < 16; j++) 49 | t[j] ^= s[(j + p) & 15]; 50 | next(); 51 | } 52 | 53 | memcpy(s, t, sizeof t); 54 | } 55 | -------------------------------------------------------------------------------- /randomstate/src/xorshift1024/xorshift1024-test-gen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int i; 7 | uint64_t temp, seed = 1ULL; 8 | xorshift1024_state state = {{ 0 }}; 9 | xorshift1024_seed(&state, seed); 10 | 11 | FILE *fp; 12 | fp = fopen("xorshift1024-testset-1.csv", "w"); 13 | if(fp == NULL){ 14 | printf("Couldn't open file\n"); 15 | return -1; 16 | } 17 | fprintf(fp, "seed, %" PRIu64 "\n", seed); 18 | for (i=0; i < 1000; i++) 19 | { 20 | temp = xorshift1024_next(&state); 21 | fprintf(fp, "%d, %" PRIu64 "\n", i, temp); 22 | printf("%d, %" PRIu64 "\n", i, temp); 23 | } 24 | fclose(fp); 25 | 26 | seed = 12345678910111ULL; 27 | xorshift1024_seed(&state, seed); 28 | fp = fopen("xorshift1024-testset-2.csv", "w"); 29 | if(fp == NULL){ 30 | printf("Couldn't open file\n"); 31 | return -1; 32 | } 33 | fprintf(fp, "seed, %" PRIu64 "\n", seed); 34 | for (i=0; i < 1000; i++) 35 | { 36 | temp = xorshift1024_next(&state); 37 | fprintf(fp, "%d, %" PRIu64 "\n", i, temp); 38 | printf("%d, %" PRIu64 "\n", i, temp); 39 | } 40 | fclose(fp); 41 | } -------------------------------------------------------------------------------- /randomstate/src/xorshift1024/xorshift1024.c: -------------------------------------------------------------------------------- 1 | #include "../splitmix64/splitmix64.h" 2 | #include "xorshift1024.h" 3 | 4 | #include 5 | 6 | extern inline uint64_t xorshift1024_next(xorshift1024_state* state); 7 | 8 | void xorshift1024_jump(xorshift1024_state* state) { 9 | static const uint64_t JUMP[] = { 0x84242f96eca9c41dULL, 10 | 0xa3c65b8776f96855ULL, 0x5b34a39f070b5837ULL, 0x4489affce4f31a1eULL, 11 | 0x2ffeeb0a48316f40ULL, 0xdc2d9891fe68c022ULL, 0x3659132bb12fea70ULL, 12 | 0xaac17d8efa43cab8ULL, 0xc4cb815590989b13ULL, 0x5ee975283d71c93bULL, 13 | 0x691548c86c1bd540ULL, 0x7910c41d10a1e6a5ULL, 0x0b5fc64563b3e2a8ULL, 14 | 0x047f7684e9fc949dULL, 0xb99181f2d8f685caULL, 0x284600e3f30e38c3ULL 15 | }; 16 | size_t i, j; 17 | uint64_t b; 18 | uint64_t t[16] = { 0 }; 19 | for(i = 0; i < sizeof JUMP / sizeof *JUMP; i++) 20 | for(b = 0; b < 64; b++) { 21 | if (JUMP[i] & 1ULL << b) 22 | for(j = 0; j < 16; j++) 23 | t[j] ^= state->s[(j + state->p) & 15]; 24 | xorshift1024_next(state); 25 | } 26 | 27 | memcpy(state->s, t, sizeof t); 28 | } 29 | 30 | void xorshift1024_seed(xorshift1024_state* state, uint64_t seed) 31 | { 32 | uint64_t initial_state[16] = {0}; 33 | uint64_t seed_copy = seed; 34 | int i; 35 | for (i = 0; i < 16; i++) 36 | { 37 | initial_state[i] = splitmix64_next(&seed_copy); 38 | } 39 | xorshift1024_init_state(state, initial_state); 40 | } 41 | 42 | void xorshift1024_init_state(xorshift1024_state* state, uint64_t* seeds) 43 | { 44 | memcpy(&(state->s), seeds, sizeof(state->s)); 45 | state->p = 0; 46 | } 47 | 48 | 49 | void xorshift1024_seed_by_array(xorshift1024_state* state, uint64_t *seed_array, int count) 50 | { 51 | uint64_t initial_state[16] = {0}; 52 | uint64_t seed_copy = 0; 53 | int iter_bound = 16>=count ? 16 : count; 54 | int i, loc = 0; 55 | for (i = 0; i < iter_bound; i++) 56 | { 57 | if (i < count) 58 | seed_copy ^= seed_array[i]; 59 | initial_state[loc] = splitmix64_next(&seed_copy); 60 | loc ++; 61 | if (loc == 16) 62 | loc = 0; 63 | } 64 | xorshift1024_init_state(state, initial_state); 65 | } -------------------------------------------------------------------------------- /randomstate/src/xorshift1024/xorshift1024.h: -------------------------------------------------------------------------------- 1 | #include 2 | #ifdef _WIN32 3 | #include "../common/stdint.h" 4 | #define inline __forceinline 5 | #else 6 | #include 7 | #endif 8 | 9 | 10 | typedef struct s_xorshift1024_state 11 | { 12 | uint64_t s[16]; 13 | int p; 14 | } xorshift1024_state; 15 | 16 | static inline uint64_t xorshift1024_next(xorshift1024_state* state) { 17 | const uint64_t s0 = state->s[state->p]; 18 | uint64_t s1; 19 | state->p = (state->p + 1) & 15; 20 | s1 = state->s[state->p]; 21 | s1 ^= s1 << 31; // a 22 | state->s[state->p] = s1 ^ s0 ^ (s1 >> 11) ^ (s0 >> 30); // b,c 23 | return state->s[state->p] * UINT64_C(1181783497276652981); 24 | } 25 | 26 | void xorshift1024_jump(xorshift1024_state* state); 27 | 28 | void xorshift1024_seed(xorshift1024_state* state, uint64_t seed); 29 | 30 | void xorshift1024_seed_by_array(xorshift1024_state* state, uint64_t *seed_array, int count); 31 | 32 | void xorshift1024_init_state(xorshift1024_state* state, uint64_t* seeds); -------------------------------------------------------------------------------- /randomstate/src/xorshift128/xorshift128-original.c: -------------------------------------------------------------------------------- 1 | /* Written in 2014-2015 by Sebastiano Vigna (vigna@acm.org) 2 | 3 | To the extent possible under law, the author has dedicated all copyright 4 | and related and neighboring rights to this software to the public domain 5 | worldwide. This software is distributed without any warranty. 6 | 7 | See . */ 8 | 9 | #include 10 | 11 | /* This is the fastest generator passing BigCrush without 12 | systematic failures, but due to the relatively short period it is 13 | acceptable only for applications with a mild amount of parallelism; 14 | otherwise, use a xorshift1024* generator. 15 | 16 | The state must be seeded so that it is not everywhere zero. If you have 17 | a 64-bit seed, we suggest to seed a splitmix64 generator and use its 18 | output to fill s. */ 19 | 20 | uint64_t s[2]; 21 | 22 | uint64_t next(void) { 23 | uint64_t s1 = s[0]; 24 | const uint64_t s0 = s[1]; 25 | s[0] = s0; 26 | s1 ^= s1 << 23; // a 27 | s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5); // b, c 28 | return s[1] + s0; 29 | } 30 | 31 | 32 | /* This is the jump function for the generator. It is equivalent 33 | to 2^64 calls to next(); it can be used to generate 2^64 34 | non-overlapping subsequences for parallel computations. */ 35 | 36 | void jump() { 37 | static const uint64_t JUMP[] = { 0x8a5cd789635d2dffULL, 0x121fd2155c472f96ULL }; 38 | 39 | uint64_t s0 = 0; 40 | uint64_t s1 = 0; 41 | for(int i = 0; i < sizeof JUMP / sizeof *JUMP; i++) 42 | for(int b = 0; b < 64; b++) { 43 | if (JUMP[i] & 1ULL << b) { 44 | s0 ^= s[0]; 45 | s1 ^= s[1]; 46 | } 47 | next(); 48 | } 49 | 50 | s[0] = s0; 51 | s[1] = s1; 52 | } -------------------------------------------------------------------------------- /randomstate/src/xorshift128/xorshift128-test.gen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "xorshift128.h" 4 | 5 | int main(void) 6 | { 7 | int i; 8 | uint64_t temp, seed = 1ULL; 9 | xorshift128_state state = {{ 0 }}; 10 | xorshift128_seed(&state, seed); 11 | 12 | FILE *fp; 13 | fp = fopen("xorshift128-testset-1.csv", "w"); 14 | if(fp == NULL){ 15 | printf("Couldn't open file\n"); 16 | return -1; 17 | } 18 | fprintf(fp, "seed, %" PRIu64 "\n", seed); 19 | for (i=0; i < 1000; i++) 20 | { 21 | temp = xorshift128_next(&state); 22 | fprintf(fp, "%d, %" PRIu64 "\n", i, temp); 23 | printf("%d, %" PRIu64 "\n", i, temp); 24 | } 25 | fclose(fp); 26 | 27 | seed = 12345678910111ULL; 28 | xorshift128_seed(&state, seed); 29 | fp = fopen("xorshift128-testset-2.csv", "w"); 30 | if(fp == NULL){ 31 | printf("Couldn't open file\n"); 32 | return -1; 33 | } 34 | fprintf(fp, "seed, %" PRIu64 "\n", seed); 35 | for (i=0; i < 1000; i++) 36 | { 37 | temp = xorshift128_next(&state); 38 | fprintf(fp, "%d, %" PRIu64 "\n", i, temp); 39 | printf("%d, %" PRIu64 "\n", i, temp); 40 | } 41 | fclose(fp); 42 | } -------------------------------------------------------------------------------- /randomstate/src/xorshift128/xorshift128.c: -------------------------------------------------------------------------------- 1 | #include "xorshift128.h" 2 | #include "../splitmix64/splitmix64.h" 3 | 4 | extern inline uint64_t xorshift128_next(xorshift128_state* state); 5 | 6 | void xorshift128_jump(xorshift128_state* state) { 7 | static const uint64_t JUMP[] = { 0x8a5cd789635d2dffULL, 0x121fd2155c472f96ULL }; 8 | 9 | size_t i; 10 | uint64_t b; 11 | uint64_t s0 = 0; 12 | uint64_t s1 = 0; 13 | for(i = 0; i < sizeof JUMP / sizeof *JUMP; i++) 14 | for(b = 0; b < 64; b++) { 15 | if (JUMP[i] & 1ULL << b) { 16 | s0 ^= state->s[0]; 17 | s1 ^= state->s[1]; 18 | } 19 | xorshift128_next(state); 20 | } 21 | 22 | state->s[0] = s0; 23 | state->s[1] = s1; 24 | } 25 | 26 | void xorshift128_seed(xorshift128_state* state, uint64_t seed) 27 | { 28 | 29 | uint64_t seed_copy = seed; 30 | uint64_t state1 = splitmix64_next(&seed_copy); 31 | uint64_t state2 = splitmix64_next(&seed_copy); 32 | xorshift128_init_state(state, state1, state2); 33 | } 34 | 35 | void xorshift128_seed_by_array(xorshift128_state* state, uint64_t *seed_array, int count) 36 | { 37 | uint64_t initial_state[2] = {0}; 38 | uint64_t seed_copy = 0; 39 | int iter_bound = 2>=count ? 2 : count; 40 | int i, loc = 0; 41 | for (i = 0; i < iter_bound; i++) 42 | { 43 | if (i < count) 44 | seed_copy ^= seed_array[i]; 45 | initial_state[loc] = splitmix64_next(&seed_copy); 46 | loc ++; 47 | if (loc == 2) 48 | loc = 0; 49 | } 50 | xorshift128_init_state(state, initial_state[0], initial_state[1]); 51 | } 52 | 53 | void xorshift128_init_state(xorshift128_state* state, uint64_t seed, uint64_t inc) 54 | { 55 | state->s[0] = seed; 56 | state->s[1] = inc; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /randomstate/src/xorshift128/xorshift128.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #ifdef _WIN32 4 | #include "../common/stdint.h" 5 | #define inline __forceinline 6 | #else 7 | #include 8 | #endif 9 | 10 | 11 | typedef struct s_xorshift128_state 12 | { 13 | uint64_t s[2]; 14 | } xorshift128_state; 15 | 16 | void xorshift128_jump(xorshift128_state* state); 17 | 18 | void xorshift128_seed(xorshift128_state* state, uint64_t seed); 19 | 20 | void xorshift128_seed_by_array(xorshift128_state* state, uint64_t *seed_array, int count); 21 | 22 | void xorshift128_init_state(xorshift128_state* state, uint64_t seed, uint64_t inc); 23 | 24 | static inline uint64_t xorshift128_next(xorshift128_state* state) { 25 | uint64_t s1 = state->s[0]; 26 | const uint64_t s0 = state->s[1]; 27 | state->s[0] = s0; 28 | s1 ^= s1 << 23; // a 29 | state->s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5); // b, c 30 | return state->s[1] + s0; 31 | } 32 | -------------------------------------------------------------------------------- /randomstate/tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/ng-numpy-randomstate/b397db9cb8688b291fc40071ab043009dfa05a85/randomstate/tests/__init__.py -------------------------------------------------------------------------------- /randomstate/tests/data/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bashtage/ng-numpy-randomstate/b397db9cb8688b291fc40071ab043009dfa05a85/randomstate/tests/data/__init__.py -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [versioneer] 2 | VCS = git 3 | style = pep440 4 | versionfile_source = randomstate/_version.py 5 | versionfile_build = randomstate/_version.py 6 | tag_prefix = 7 | parentdir_prefix = randomstate- --------------------------------------------------------------------------------