Skip to main content

A random number generator - Tiny SplitMix64-based RNG utilities (non-cryptographic)

Project description

rndm

A tiny, dependency-free random number utility built on SplitMix64.

It provides:

  • Uniform integers without modulo bias (rejection sampling)
  • Fixed-precision uniform floats within a range
  • Optional independent streams via stream_id
  • Fast randbits(k) for bit generation

Not for cryptography. SplitMix64 is great for simulations, sampling, games, and testing—but not for security.

Install

From PyPI

pip install rndm

From source (GitHub)

pip install .

For development:

pip install -e ".[dev]"

Quick start

import rndm

# (optional) reseed; if you don't, it seeds automatically on first use
rndm.reseed()

# Uniform int in [0, 10] (inclusive by default)
x = rndm.time_based_random(0, 10)
print(x)

# Uniform int in [50, 1000), exclusive upper bound
y = rndm.time_based_random(50, 1000, inclusive=False)
print(y)

# Uniform float in [-1.0, 1.0] with precision inferred from inputs (1 decimal here)
z = rndm.time_based_random(-1.0, 1.0)
print(z)

# Force precision (e.g., 3 decimals)
w = rndm.time_based_random("0.000", "0.999", precision=3)
print(w)

# Independent streams
rndm.set_stream_id(123)
a1 = rndm.time_based_random(0, 10)
rndm.set_stream_id(456)
a2 = rndm.time_based_random(0, 10)

API

  • reseed(seed: int | None = None, stream_id: int | None = None) -> int
    Reseeds the generator. If seed is None, uses a mixed time-based seed. Returns the internal 64-bit state.

  • set_stream_id(stream_id: int) -> None
    Switches to a different independent stream (reseeds automatically).

  • randbits(k: int) -> int
    Returns an integer with k random bits.

  • time_based_random(min_value=0, max_value=1, inclusive=True, precision=None)

    • If both bounds are int, returns an int
    • Otherwise returns a float rounded to precision decimals (or inferred from inputs).
    • Uses rejection sampling (no % modulo reduction), so integer ranges are unbiased.

Benchmarks

The repository includes example benchmark outputs under https://github.com/bio-informatician/rndm/tree/main/benchmarks/:

  • Distribution plots: https://github.com/bio-informatician/rndm/tree/main/benchmarks/plots/
  • Bin counts (CSV): https://github.com/bio-informatician/rndm/tree/main/benchmarks/counts/
  • Summary table: summary

Example figures:

Chi-square summary

Speed summary

Development

Run tests:

pytest

Build a wheel + sdist:

python -m pip install --upgrade build
python -m build

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rndm-0.1.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rndm-0.1.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file rndm-0.1.2.tar.gz.

File metadata

  • Download URL: rndm-0.1.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.10

File hashes

Hashes for rndm-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f6e9d3cdbde5b14b45250f220dc5af09a7202e1255465334228c1d09672d2716
MD5 254455782403e14ac8e8957d2dcb1d9d
BLAKE2b-256 ab8d4d0fc1f296048047303f0ed95fdac2f819848b7afa09a60d7d133a5f3fdc

See more details on using hashes here.

File details

Details for the file rndm-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: rndm-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.10

File hashes

Hashes for rndm-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 aa0eb0bd41fb78b64eca34b801a818c855e76ed7e8efbefdc4df0134db29e996
MD5 f0766180b5597ef62d71c7d35e506e4e
BLAKE2b-256 c4d80bdb842386a5d9842df6e2537ec9cddb98b1ea07b7b3e800ed95868ab007

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page