Expand description
Minimum supported Rust version: 1.70.0
§Versatile Random Distributions (VRD)
A lightweight, no_std-friendly random number generator backed by
Xoshiro256++, with optional Mersenne Twister (MT19937) support.
§Features
- High performance: Xoshiro256++ default — 32-byte state, period 2^256 - 1, SplitMix64 seed whitening.
- Legacy reproducibility: opt-in MT19937 backend.
Random::new_mersenne_twister()requiresalloc + std;Random::new_mersenne_twister_with_seed(u32)isalloc-only. no_stdready: pure-core build withdefault-features = false, validated onthumbv7em-none-eabihf(Cortex-M) andwasm32-unknown-unknown(WebAssembly) in CI.- Unbiased sampling:
int,uint,random_range, and the publicboundeduse Lemire’s nearly-divisionless method. - Bit-precise floats:
float()carries 24 mantissa bits,double()carries 53. Always[0.0, 1.0). - Distributions:
uniform(low, high),normal,exponential,poisson—std-free vialibm. - Convenience helpers:
iter_u32/iter_u64/iter_bytesiterator adapters;uuid_v4_bytes(no_std) anduuid_v4(alloc);hex_tokenandbase64_tokenfor URL-safe random tokens. rand 0.10traits:TryRng, the blanket-implementedRng, andSeedableRng.
§Quickstart
use vrd::Random;
let mut rng = Random::from_u64_seed(42); // deterministic, allocation-free
let n: u32 = rng.rand(); // any u32
let _ = rng.u64(); // any u64
let _ = rng.int(1, 100); // i32 in [1, 100], uniform
let _ = rng.double(); // f64 in [0.0, 1.0)
let _ = rng.bool(0.5); // 50/50 coin
assert!(n > 0 || n == 0);Use Random::new() for entropy-seeded randomness on std targets,
Random::from_seed() / Random::from_u64_seed() for deterministic
/ no_std use, and Random::new_mersenne_twister() / Random::new_mersenne_twister_with_seed()
when you need bit-for-bit MT19937 reproducibility against existing
test vectors.
§Not a CSPRNG
Random is not cryptographically secure. For credentials,
session IDs, or anything an attacker would benefit from predicting,
use rand::rngs::OsRng or getrandom. A built-in ChaCha20-based
CSPRNG backend is tracked in
issue #90.
Re-exports§
pub use mersenne_twister::MersenneTwisterConfig;pub use mersenne_twister::MersenneTwisterError;pub use mersenne_twister::MersenneTwisterParams;pub use random::FloatExt;pub use random::Random;pub use random::RngBackend;
Modules§
- macros
- Convenience macros.
Convenience macros over the
crate::Randomfacade. - mersenne_
twister - Mersenne Twister configuration and constants. Mersenne Twister (MT19937) configuration types.
- random
- The core
Randomfacade. TheRandomfacade and the enum-dispatchedRngBackendbackends. - xoshiro
- Xoshiro256++ implementation. Xoshiro256++ pseudo-random number generator.
Macros§
- rand_
alphanumeric - Returns a random alphanumeric ASCII char.
- rand_
bool - Generates a random
boolwhose probability oftrueis the second argument. Panics if the probability is outside[0.0, 1.0]. - rand_
bytes - Returns a
Vec<u8>oflenrandom bytes. Requires theallocfeature. - rand_
char - Returns a random lowercase ASCII char (
'a'..='z'). - rand_
choose - Picks a random reference into the given slice (
Option<&T>). - rand_
double - Generates an
f64in[0.0, 1.0). - rand_
exponential - Exponential sample with the given rate.
- rand_
float - Generates an
f32in[0.0, 1.0). - rand_
int - Generates an unbiased
i32in the inclusive range[min, max]. - rand_
normal - Standard Box-Muller normal sample.
- rand_
poisson - Poisson sample with the given mean.
- rand_
range - Inclusive
[min, max]range fori32. - rand_
seed - Re-seeds the active backend.
- rand_
shuffle - Fisher-Yates shuffle in place.
- rand_
string - Returns a fresh lowercase ASCII
Stringoflengthchars. Requiresalloc. - rand_
twist - Forces a Mersenne-Twister twist; no-op on Xoshiro.
- rand_
uint - Generates an unbiased
u32in the inclusive range[min, max]. - rand_
weighted_ choice - Selects a reference into
$choicesweighted by$weights. - random_
range - Generates an unbiased
u32in the half-open range[min, max).
Enums§
- VrdError
- Crate-level error type for the
vrdlibrary.