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. IfseedisNone, 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 withkrandom bits. -
time_based_random(min_value=0, max_value=1, inclusive=True, precision=None)- If both bounds are
int, returns anint - Otherwise returns a
floatrounded toprecisiondecimals (or inferred from inputs). - Uses rejection sampling (no
%modulo reduction), so integer ranges are unbiased.
- If both bounds are
Benchmarks
The repository includes example benchmark outputs under https://raw.githubusercontent.com/bio-informatician/rndm/refs/heads/main/benchmarks/:
- Distribution plots:
https://raw.githubusercontent.com/bio-informatician/rndm/refs/heads/main/benchmarks/plots/ - Bin counts (CSV):
https://raw.githubusercontent.com/bio-informatician/rndm/refs/heads/main/benchmarks/counts/ - Summary table:
https://raw.githubusercontent.com/bio-informatician/rndm/refs/heads/main/benchmarks/summary.csv
Example figures:
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rndm-0.1.1.tar.gz.
File metadata
- Download URL: rndm-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b1f6a8d163a0aa4c4cb287bb1960c9d249f7c8c2e4288fd2c50849cb69051d3
|
|
| MD5 |
c4b74926635e7a70436fdf8df5061faa
|
|
| BLAKE2b-256 |
f549146c889f6821de1d33c340c6728e219faf122ccba01c6e677b7873adf777
|
File details
Details for the file rndm-0.1.1-py3-none-any.whl.
File metadata
- Download URL: rndm-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45735d2b73a59373156bb84dff95e683bcb6e25a871f883d16f9c0b009928499
|
|
| MD5 |
2fd0d3ec87ce41a668fe355633d24a57
|
|
| BLAKE2b-256 |
e49466db2e787c1e3ea6b3012895c204d8e877ebf395e57b066986c8b54fe068
|