Skip to main content

dlrand

Exact, reproducible random variate generation: a portable C99 core with a clean Python interface.

  • Exact samplers. Poisson (PTRS), Binomial (inversion / BTRS), Geometric, Multinomial, mixtures, alias tables, rejection sampling, DL Ziggurat. Every distribution is tested against its exact PMF/CDF.
  • Reproducible. A seed fully determines a stream. The C library has no global or static mutable state, so generators never interfere. getstate()/setstate() and spawn() (jump-ahead, 2^128 apart) are built in.
  • Safe. Every argument is validated; failures are status codes in C and exceptions in Python - never sentinel values and never silent bias. Rejection sampling detects an undersized envelope constant M (EnvelopeError).
  • Portable. Plain C99 targeting Linux, macOS and Windows (MSVC, GCC, Clang); pip install dlrand or CMake. The included GitHub Actions workflow builds and tests all three.

Naming. The distribution that is also called the normal distribution is called the DL (De Moivre-Laplace) distribution throughout this library, after Abraham de Moivre (1733) and Pierre-Simon Laplace, who introduced and developed it: dl_box_muller, dl_ziggurat, rl_dl_pdf, ...

Not for cryptography. xoshiro256++ is a fast statistical generator, not a CSPRNG. Do not use it for keys, tokens or passwords.

Install

pip install dlrand              # prebuilt wheels for Windows, macOS and Linux (no compiler needed)
pip install "dlrand[numpy]"     # optional: RandomGenerator(numpy=True) returns arrays

From source (needs a C99 compiler): pip install . in a checkout. You can also build the C library alone with cmake -S . -B build && cmake --build build; the Python package then finds build/libdlrand.* automatically (or set DLRAND_PATH).

Quick start

import dlrand

rng = dlrand.RandomGenerator(seed=2024)           # same seed -> same stream, always

rng.uniform(size=3)                                # U[0,1), 53 random bits
rng.integers(1, 6, size=10)                        # unbiased, inclusive bounds
rng.dl_box_muller(mu=0.0, sigma=1.0, size=3)       # DL distribution (Box-Muller)
rng.dl_ziggurat(size=3)                            # DL distribution (Ziggurat, any layer count)
rng.poisson(800, size=5)                           # exact for every lambda up to 1e15
rng.binomial(2000, 0.5, size=5)                    # exact for n up to 2^53
rng.multinomial(1000, [0.5, 0.3, 0.2])
rng.permutation(["A", "K", "Q", "J"], r=3)         # sampling without replacement, any objects
rng.bernoulli_sampling(list(range(20)), 0.25)      # keep each item with probability p

mix = dlrand.MixtureBuilder(rng).add_uniform(0.5, 0, 10).add_poisson(0.5, 100.0).build("alias")
mix.sample(8)                                      # engine built once, O(1) per draw

kids = rng.spawn(4)                                # independent parallel streams

Rejection sampling with an automatically found, verified envelope:

f = lambda x: x * (2 - x) ** 2 if 0 <= x <= 2 else 0.0       # need not be normalised

opt = dlrand.RejectionOptimizer(rng)
print(opt.compare(f, domain=(0, 2)))                          # every proposal: M, expected vs measured acceptance
best = opt.best(f, domain=(0, 2))
xs = best.sample(rng, f, size=10_000)

rng.rejection_sampling(f, "uniform", 1.2, 0.0, 2.0)           # raises EnvelopeError: M = 1.2 < sup f/g = 2.37

More in examples/. Algorithms, domains and references: docs/ALGORITHMS.md. Coming from the earlier prototypes? See docs/MIGRATION.md.

Python API at a glance

Area Methods
Uniform uniform, integers
DL distribution dl_box_muller (alias normal_bm), dl_ziggurat
Other continuous exponential, cauchy, laplace, rejection_sampling, rejection_sampling_stats, get_proposal_density
Discrete geometric, poisson, binomial, multinomial, bernoulli_sampling, permutation, choice
Custom discrete sample_custom_array, sample_custom_func
Composition mixture, sample_mixture, MixtureBuilder, alias_sampler
Tuning RejectionOptimizer: analyze, plan, best, benchmark, compare, report
State reseed, getstate, setstate, spawn, close

Methods taking size return a scalar for size=None, otherwise a list (NumPy array with numpy=True).

C API

#include <dlrand/dlrand.h>

RL_State* st = rl_state_new(2024);
double x;
int rc = rl_dl_ziggurat_sample(st, table, &x);       /* 0 = RL_OK */
if (rc != RL_OK) fprintf(stderr, "%s\n", rl_status_string(rc));

Results are written through output pointers; the return value is an rl_status (RL_ERR_ARG, RL_ERR_TIMEOUT, RL_ERR_ALLOC, RL_ERR_CALLBACK, RL_ERR_ENVELOPE). Headers live in include/dlrand/, one per engine plus the umbrella dlrand.h.

Supported domains

Distribution Domain Notes
Poisson 0 <= lambda <= 1e15 sequential needs lambda <= 700; ptrs needs lambda >= 10; auto picks
Binomial 0 <= n <= 2^53, 0 <= p <= 1 auto is exact; poisson_approx is an explicit approximation
Geometric 0 < p <= 1 success {1,2,...} or failure {0,1,...}; saturates at INT64_MAX
Multinomial p >= 0, sum within 1e-8 of 1 alias if r > n, else sequencing
Exponential / Cauchy / Laplace finite parameters, scale > 0 outputs are always finite
DL finite mu, finite sigma >= 0 Ziggurat: 8..65536 layers, table solved at creation

Precision note: for n or lambda beyond ~1e12 the acceptance tests are accurate to floating-point resolution, not beyond. Results are bit-reproducible for a given platform and build; libm differences between platforms can change the last bit of log/exp, which on rare occasions flips a rejection decision.

Thread safety

A generator is not synchronised: use one per thread (spawn() gives non-overlapping streams). Separate generators, tables and alias/composition engines share no mutable state and may be used concurrently. The Python callbacks used by rejection sampling run under the GIL.

Tests

pip install -e ".[test]" && pytest                   # chi-square / KS tests against exact PMFs and CDFs
cmake -S . -B build -DRL_SANITIZE=ON && cmake --build build && ctest --test-dir build

All tests use fixed seeds (deterministic) with ~5-sigma thresholds. The CI workflow covers Linux, macOS and Windows with Python 3.9, 3.12 and 3.13.

License

MIT - see LICENSE.

Metadata

Release files for dlrand 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dlrand 0.1.0
File Size Uploaded Provenance Actions
dlrand-0.1.0.tar.gz 68.3 kB With provenance Details for dlrand-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for dlrand 0.1.0
File Actions
dlrand-0.1.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for dlrand-0.1.0-py3-none-win_amd64.whl
dlrand-0.1.0-py3-none-musllinux_1_2_x86_64.whl Python 3 none Linux musl 1.2+ x86-64 Details for dlrand-0.1.0-py3-none-musllinux_1_2_x86_64.whl
dlrand-0.1.0-py3-none-musllinux_1_2_aarch64.whl Python 3 none Linux musl 1.2+ ARM64 Details for dlrand-0.1.0-py3-none-musllinux_1_2_aarch64.whl
dlrand-0.1.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details for dlrand-0.1.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
dlrand-0.1.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl Python 3 none Linux glibc 2.28+ x86-64, Linux glibc 2.5+ x86-64 Details for dlrand-0.1.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
dlrand-0.1.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for dlrand-0.1.0-py3-none-macosx_11_0_arm64.whl
dlrand-0.1.0-py3-none-macosx_10_13_x86_64.whl Python 3 none macOS 10.13+ x86-64 Details for dlrand-0.1.0-py3-none-macosx_10_13_x86_64.whl

Total release size: 627.1 kB

Release files / dlrand-0.1.0.tar.gz

Download URL dlrand-0.1.0.tar.gz
Size 68.3 kB
Tags Source
SHA-256 checksum
How to use checksums
dfa8f1ba4f2058fec237c4f49df914eaf4b21473071d21b5995b5e58c11ff388
BLAKE2b-256 checksum
How to use checksums
03a31154d1b565c6c67ca48201b02a4b9a0248225e33679dff990d82d363ab2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / dlrand-0.1.0-py3-none-win_amd64.whl

Download URL dlrand-0.1.0-py3-none-win_amd64.whl
Size 104.3 kB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
7e5a152ada2bfdb02211a2bcc18b56f3554253e160c3672409a69cf88f888197
BLAKE2b-256 checksum
How to use checksums
6d86a5a8d6cf9a0e891fe342e6ae79ca8491feb645228f41fbc66ac481744f26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / dlrand-0.1.0-py3-none-musllinux_1_2_x86_64.whl

Download URL dlrand-0.1.0-py3-none-musllinux_1_2_x86_64.whl
Size 96.5 kB
Tags Linux musl 1.2+ x86-64 Python 3
SHA-256 checksum
How to use checksums
d19c8eb56eb53a520814f5080e82dcd91383b7dc220935aefd347d10ea22d9be
BLAKE2b-256 checksum
How to use checksums
ae9a6fe84bd98a354487e1f41d3bf40f72221441e4795061e0980e298541b542
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / dlrand-0.1.0-py3-none-musllinux_1_2_aarch64.whl

Download URL dlrand-0.1.0-py3-none-musllinux_1_2_aarch64.whl
Size 94.0 kB
Tags Linux musl 1.2+ ARM64 Python 3
SHA-256 checksum
How to use checksums
cee4fe99bff834875fc7e3cfbb5fa8831e086d3d2ca8889c9f32ff487922c68a
BLAKE2b-256 checksum
How to use checksums
8ad80c1f60f687f88d269591f2f4525e0ef9dd65086845bf5a350132d81a3397
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / dlrand-0.1.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL dlrand-0.1.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 92.3 kB
Tags Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
c5aa7b2a47e7c87fb601cc954bf7dceb31e02f1634ad542c9cbd3a03c71aa446
BLAKE2b-256 checksum
How to use checksums
dcab46606e080448e29f631d69a37db3c152ccb0a3140662c794bb3f03d71691
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / dlrand-0.1.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL dlrand-0.1.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 93.6 kB
Tags Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64 Python 3
SHA-256 checksum
How to use checksums
3aaf5a3fd3f017d7e53eb19aa02df116877d5451f416db33214f4e4498631b94
BLAKE2b-256 checksum
How to use checksums
c23ecc346014d1349262170b24a7abb9ad0e2d4182709bc213b4199c8f16c2ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / dlrand-0.1.0-py3-none-macosx_11_0_arm64.whl

Download URL dlrand-0.1.0-py3-none-macosx_11_0_arm64.whl
Size 38.3 kB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
30233d624ffdbca78cbf2bd92321070178b4683d45e933f3b7895266334fdbd9
BLAKE2b-256 checksum
How to use checksums
f2ab76b36fea8b79a800a986fe896db23635b13d160e1760527fbc3abb6f7cb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / dlrand-0.1.0-py3-none-macosx_10_13_x86_64.whl

Download URL dlrand-0.1.0-py3-none-macosx_10_13_x86_64.whl
Size 39.9 kB
Tags Python 3 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
112d6bc8a7b27f01a6847d55b961096751f2476f400fb34e84681b76352cd268
BLAKE2b-256 checksum
How to use checksums
097291cc3f0638ca0f969ca6b002bda41884e836f266aae6f8a848f0dc88516e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for dlrand 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 8 files in this release have been uploaded with provenance metadata.

Select from 8 files

Attestation for dlrand-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

8 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page