Aporia
Aporia is a small, dependency-free Rust RNG library with multiple backends and a consistent, ergonomic API. It favors clarity, correctness, and reproducibility. Not intended for cryptography.
Aporia (ἀπορία): Greek for “difficulty,” “perplexity,” or “impasse.”
Features
- Multiple RNG backends:
- PCG (Permuted Congruential Generator)
- XorShift
- LCG (Linear Congruential Generator)
- MT19937_64 (64-bit Mersenne Twister)
- SplitMix64
- Xoshiro256** (StarStar variant)
- Consistent
Rngwrapper API across backends - Unbiased integer ranges via zone rejection
- Iterators over
u64/f64and afill_byteshelper no_stdsupport (with optionalstdfeature)
Installation
Default (with std feature enabled):
[]
= "0.1.2"
no_std (disable default std feature):
[]
= { = "0.1.2", = false }
Quick start
use ;
Ranges without bias
The Rng::gen_range(min, max) method returns Result<u64, AporiaError>. It uses the unbiased “zone” rejection method to avoid modulo bias.
use ;
If bounds are guaranteed valid at the call site, using expect is acceptable:
use ;
let mut rng = new;
// Bounds are known-valid here; expecting success is safe.
let v = rng.gen_range.expect;
Iterators and bytes
use ;
let mut rng = new;
// Take some u64s
let sum: u64 = rng.iter_u64.take.sum;
// Iterate f64s in [0,1)
let mut avg = 0.0;
for in rng.iter_f64.take.enumerate
// Fill a byte buffer
let mut buf = ;
rng.fill_bytes;
Backends at a glance
- XorShift: very fast, tiny state, simple
- PCG: fast, statistically strong for general use
- LCG: very simple, good for basic use
- MT19937_64: high quality, very long period, large state
- SplitMix64: very fast, good initializer for other RNGs
- Xoshiro256**: modern, high quality, very fast
Error handling
The crate uses a small custom error enum:
Examples of Result-based APIs:
Rng::gen_range(min, max) -> Result<u64, AporiaError>Rng::gen_range_f64(min, max) -> Result<f64, AporiaError>XorShift::try_new(seed) -> Result<XorShift, AporiaError>
When std feature is enabled (default), AporiaError implements std::error::Error.
no_std
- The crate supports
#![no_std]when built withdefault-features = false. - All core APIs are available; printing and OS entropy are not provided by this crate.
Stability
For a given backend and seed, sequences are intended to remain stable across patch/minor versions.
Contributing
Contributions are welcome. Please open an issue or PR for bugs or enhancements.