A library meant for fast, random number generation with quick compile time, and minimal dependencies.
Generating a number with an initialized RNG
use ; let mut rng = new; println!;
Generating a number with a thread-local RNG
use Rng; let mut rng = tls_rng; println!;
Generating a number in a range
use ; let mut rng = new; println!; println!;
Shuffling a Vec
use ; let mut rng = new; let mut items = vec!; rng.shuffle;
Why should I use this over...
rand- The standard rand crate is a complex beast. It contains unsafe code in the core implementations, and while it has much more options than we do, that's kind of the point. We're straight to the point, while rand is everything and the kitchen sink.
randomize- These are all minimal, zero-dep implementations of the PCG family of RNGs (Pcg32 and Pcg64). While these are decent, they are much slower than wyrand (which beats the speed of these Pcg32 implementations while providing 64 random bits), and do not provide CSPRNGs.
getrandom- The getrandom crate just provides OS entropy sources. It is not meant for random number generation. In fact, we provide it as an optional entropy source.
|RNG||nanorand type||Output Size||Cryptographically Secure||Speed1||Notes||Original Implementation|
||64 bits (
||64 bits (
||512 bits (
||✅||204 MB/s (ChaCha8), 79 MB/s (ChaCha20)||Only works in Rust 1.47 or above||https://cr.yp.to/chacha.html|
1. Speed benchmarked on an M1 Macbook Air
Listed in order of priority
- If the
getrandomfeature is enabled, then
getrandom::getrandomwill be called.
- If the
rdseedfeature is enabled, and is running on an x86(-64) system with the RDSEED instruction, then we will attempt to source as much entropy as possible via our
- Linux and Android will attempt to use the
- macOS and iOS (Darwin-based systems) will use Security.framework's
- If we're targeting UWP, then the
BCryptGenRandomis used with system-preferred RNG (
- Otherwise, we'll use
std(default) - Enables Rust
stdlib features, such as seeding from OS entropy sources.
tls(default) - Enables a thread-local
WyRandRNG (see below). Requires
tlsto be enabled.
wyrand(default) - Enable the
pcg64(default) - Enable the
chacha- Enable the
ChaChaRNG. Requires Rust 1.47 or later.
rdseed- On x86 and x86-64 platforms, the
rdseedintrinsic will be used when OS entropy isn't available.
zeroize- Implement the Zeroize trait for all RNGs.
getrandom- Use the
getrandomcrate as an entropy source. Works on most systems, optional due to the fact that it brings in more dependencies.