Note:
simdrngis pre-1.0 — the API may change before the first tagged release.
C++20 random number generators that run scalar and SIMD-accelerated, with nanobind-powered Python bindings that drop straight into NumPy and SciPy.
- Header-only (plus one tiny dispatch TU for runtime SIMD selection); the scalar path is dependency-free.
- Four generator families — Xoshiro256++, SplitMix64, ChaCha, Philox — each
available scalar, runtime-SIMD-dispatched, and
-march=native. - Drop-in with
std::uniform_int_distributionand friends, plus a fastuniform()returning adoublein[0, 1). - Parallel-ready — per-thread and per-node independent streams via
thread_id/cluster_id, or Philox's counter-based reproducibility. - Python — every generator is a
numpy.random.Generatorwith a GIL-released C++ bulk-fill fast path.
General-purpose PRNGs are written one number at a time, leaving most of a modern
CPU's vector units idle. simdrng pairs best-in-class scalar generators
(Blackman & Vigna's xoshiro256++, the Random123 Philox counter-based design,
the ChaCha cipher core) with hand-tuned SIMD backends, and picks the right
implementation at runtime via xsimd
dispatch — so the same binary uses AVX-512 on a server and NEON on an ARM
laptop with no recompilation. When you can commit to a target with
-march=native, the *Native generators inline the widest available width
directly. The result is NumPy-beating bulk throughput from C++ or Python while
keeping single-value calls as fast as the reference scalar code.
#include <simdrng/xoshiro.hpp>
simdrng::Xoshiro rng(42); // seed
std::uint64_t x = rng(); // next 64-bit value
double u = rng.uniform(); // double in [0, 1)Bulk fill is where SIMD pays off. XoshiroSIMD refills an internal cache with
wide SIMD batches, so a plain generate loop runs vectorised under the hood — no
special bulk API to learn:
#include <simdrng/xoshiro_simd.hpp>
#include <vector>
simdrng::XoshiroSIMD rng(42);
std::vector<double> out(1'000'000);
for (auto &x : out)
x = rng.uniform(); // cache refilled with SIMD batchesFrom Python it is a numpy.random.Generator:
import simdrng
rng = simdrng.XoshiroSIMD(seed=42) # a numpy.random.Generator
samples = rng.random(10_000_000) # GIL-released C++ bulk fillMore runnable programs live in examples/ (C++ and Python), and
ready-to-run Compiler Explorer links
are published to the godbolt-links branch.
| Family | Scalar | SIMD dispatch | -march=native |
|---|---|---|---|
| Xoshiro256++ | ✅ | ✅ | ✅ |
| SplitMix64 | ✅ | — | — |
| ChaCha 8/12/20 | ✅ | ✅ | ✅ |
| Philox 2x/4x × 32/64 | ✅ | ✅ | ✅ |
All generators satisfy the standard UniformRandomBitGenerator requirements, so
they compose with std::uniform_int_distribution and friends. The uniform()
method returns a double in [0, 1) faster than
std::uniform_real_distribution; see https://prng.di.unimi.it/#remarks for
the rationale. Per-family guidance is in the
documentation (Guides section).
Each generator takes optional thread_id and cluster_id parameters that carve
out independent, non-overlapping streams per thread and per node (via xoshiro's
jump() / long_jump()):
#include <simdrng/xoshiro.hpp>
simdrng::Xoshiro rng(42, 1, 2); // seed, thread, cluster
simdrng::Xoshiro rng(42, omp_get_thread_num()); // OpenMP
simdrng::Xoshiro rng(42, omp_get_thread_num(), MPI_rank); // MPI + OpenMPPhilox takes the counter-based route instead: each work item derives its own
sub-stream from (seed, counter) with no coordination — see
examples/cpp/philox_counter_based.cpp.
For the threaded example, see
examples/cpp/threaded_openmp.cpp.
All scalar generators compile in CUDA device code: SplitMix, XoshiroScalar,
MWC64X32, Philox and ChaCha. The headers compile without
--expt-relaxed-constexpr. macros.hpp marks all scalar members
__host__ __device__. macros.hpp has simdrng::array, a
host-and-device array type, and simdrng_bit_cast. In nvcc,
simdrng_bit_cast uses the CCCL std::bit_cast.
Install the CUDA toolkit first. SIMDRNG_CUDA is OFF by default. Set it to
ON to make the CUDA device test:
cmake -S . -B build -DSIMDRNG_CUDA=ON
cmake --build build
ctest --test-dir build -R test_cudaCMake sets CMAKE_CUDA_ARCHITECTURES to the host GPU. Set a value to change
the target. test_cuda runs the check on the GPU: device output equals
host output for all scalar classes, including stream().
Each scalar class has a constexpr function stream(seed, i). Each thread
owns a disjoint stream. XoshiroScalar and MWC64X32 make the state in
O(log i) steps. The other three use O(1).
| Generator | Start offset for index i | Values in one stream | Max streams |
|---|---|---|---|
SplitMix |
state = seed + i × 2^32 × gamma | 2^32 | 2^32 |
XoshiroScalar |
i × 2^128 outputs | 2^128 | 2^128 |
MWC64X32 |
i × 2^32 outputs | 2^32 | 2^31 |
Philox4x32 |
i in the top 64 counter bits | 2^64 blocks = 2^65 | 2^64 |
ChaCha |
i as the nonce | 2^64 blocks = 2^67 | 2^64 |
Philox2x32 puts the index in the top 32 counter bits. Its stream is 2^32
blocks (2^32 values). An index of 2^32 or more wraps to the stream of
i mod 2^32.
The kernel uses one stream for each thread:
#include <simdrng/xoshiro_scalar.hpp>
#include <cuda_runtime.h>
__global__ void fill(std::uint64_t seed, std::uint64_t *out) {
const auto tid = static_cast<std::uint64_t>(blockIdx.x * blockDim.x + threadIdx.x);
auto g = simdrng::XoshiroScalar::stream(seed, tid);
out[tid] = g();
}Each process jumps the seed by its process id with long_jump(). Each thread
then gets its stream from the process base. The (seed, thread_id, cluster_id)
constructor of XoshiroScalar does the long jumps and the thread jumps:
simdrng::XoshiroScalar rng{42, omp_get_thread_num(), MPI_rank};long_jump() equals 2^192 calls to next(). It gives 2^64 process bases,
and each base gives 2^64 threads of 2^128 values. For MWC64X32, the base
uses at most 2^15 long_jump() calls. Each thread calls jump(t * 2^32) on
the base, with at most 2^16 threads of 2^32 values.
Vigna sets a limit on the overlap of random streams (https://prng.di.unimi.it/#period). For period P, n streams of length L starting at random points overlap with probability at most n^2 * L / P. MWC64X32 holds a 64-bit state with a period of about 2^63, and Vigna calls such a state space too small for parallel use. It fits register-scarce hardware such as GPUs. For bulk CPU fill, use XoshiroSIMD. Its SIMD batches are faster than the scalar loop.
git clone https://github.com/DiamonDinoia/simdrng.git
cd simdrng
cmake --preset release
cmake --build build/release
ctest --preset testCMakePresets.json ships ready-made profiles so you rarely need raw -D flags:
release, debug, ci, bench, sanitizers, tsan, valgrind,
static-analysis, coverage.
CMake options:
| Option | Default | Purpose |
|---|---|---|
SIMDRNG_BUILD_TESTS |
ON* | Build Catch2 tests and benchmarks (*follows BUILD_TESTING) |
SIMDRNG_BUILD_PYTHON |
OFF | Build the nanobind Python extension |
SIMDRNG_BUILD_EXAMPLES |
OFF | Build C++ examples under examples/cpp |
SIMDRNG_BUILD_DOCS |
OFF | Generate Sphinx/Doxygen docs |
SIMDRNG_MARCH_NATIVE |
OFF | Compile benchmarks with -march=native |
SIMDRNG_USE_SANITIZERS |
OFF | ON = ASan+UBSan, TSAN = ThreadSanitizer |
Consume from another CMake project — install and find_package:
find_package(simdrng CONFIG REQUIRED)
target_link_libraries(my_target PRIVATE simdrng::simdrng)The installed package config pulls in its dependencies (xsimd, poet) via
find_dependency automatically — but only when built with xsimd; a scalar-only
install requires no dependencies.
Or vendor it directly with FetchContent:
include(FetchContent)
FetchContent_Declare(simdrng
GIT_REPOSITORY https://github.com/DiamonDinoia/simdrng.git
GIT_TAG main)
FetchContent_MakeAvailable(simdrng)
target_link_libraries(my_target PRIVATE simdrng::simdrng)An amalgamated simdrng.hpp is published to the
single-header
branch. poet is inlined; xsimd is kept as an external <xsimd/...> include, so
compile with the xsimd headers available. The compile-time-arch *Native
generators (XoshiroNative, Philox4x64Native, …) work header-only; the
runtime-dispatch types (XoshiroSIMD, Philox*SIMD, and the default
simdrng::Xoshiro alias) require linking the compiled library and are not
available from the single header alone.
pip install .
# or, for development:
pip install -e '.[test]' --no-build-isolationThe full matrix runs in CI and is rendered to SVG on the
benchmark-results
branch.
The chart below shows u64 bulk throughput — the native output of every
engine — comparing the scalar loop against SIMD generate(). Per-call latency,
double generation (uniform() vs std::uniform_real_distribution), and memory
cost are covered in
docs → benchmarks.
Full documentation — quick start, per-family guides, the auto-generated C++ and Python API reference, and the technical references — is hosted at https://simdrng.readthedocs.io/.
Formatting and linting run through pre-commit (clang-format, gersemi, darker/ruff, shfmt, markdownlint). Install once:
pip install pre-commit && pre-commit install
git config blame.ignoreRevsFile .git-blame-ignore-revs # skip the bulk-format commit in blameGrouped by the family each source backs — full annotations (periods, scrambling,
seeding, the uniform() rationale) live in
docs → references:
- Xoshiro256++ & SplitMix64 — D. Blackman and S. Vigna, Scrambled Linear
Pseudorandom Number Generators, ACM TOMS (2021),
https://vigna.di.unimi.it/papers.php#BlVSLPNG; and S. Vigna's
xoshiro/xoroshiro shootout, SplitMix64 seeding, and
uniform()rationale, https://prng.di.unimi.it/. - ChaCha 8/12/20 — D. J. Bernstein, ChaCha, a variant of Salsa20, https://cr.yp.to/chacha.html; cross-checked against Monocypher in the tests.
- Philox — J. K. Salmon et al., Parallel Random Numbers: As Easy as 1, 2, 3 (Philox), SC '11.
The reference splitmix64.c / xoshiro256plusplus.c used by the tests are the
authors' own public-domain (CC0) code from https://prng.di.unimi.it/.
BSD-3-Clause-Attribution — see LICENSE.