A library meant for fast, random number generation with quick compile time, and minimal dependencies.
Examples
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!;
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.fastrand,oorandom,random-fast-rng, orrandomize- 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 Implementations
| RNG | nanorand type | Output Size | Cryptographically Secure | Speed1 | Notes | Original Implementation |
|---|---|---|---|---|---|---|
| wyrand | nanorand::WyRand, nanorand::tls::TlsWyRand | 64 bits (u64) |
🚫 | 8.6 GB/s | https://github.com/lemire/testingRNG/blob/master/source/wyrand.h | |
| Pcg64 | nanorand::Pcg64 | 64 bits (u64) |
🚫 | 2.3 GB/s | https://github.com/rkern/pcg64 | |
| ChaCha | nanorand::ChaCha | 512 bits ([u32; 16]) |
✅ | 140 MB/s (ChaCha8), 70 MB/s (ChaCha20) | Only works in Rust 1.47 or above | https://cr.yp.to/chacha.html |
1. Speed benchmarked on an Intel Core i7 8086k processor running at 5.1 GHz
Entropy Sources
Listed in order of priority
- If the
getrandomfeature is enabled, then getrandom::getrandom will 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 rdseed_entropy function - Unix-like systems (Linux, macOS, iOS, BSD, Android)
- We will first attempt to source random bytes from
/dev/urandom - If that fails, we will resort to
/dev/random - Windows
- If we're targeting UWP, then the
BCryptGenRandomis used with system-preferred RNG (BCRYPT_USE_SYSTEM_PREFERRED_RNG). - Otherwise, we'll use
RtlGenRandom. - If all else fails, and the
stdfeature is enabled, we'll resort to pulling bytes from the current system unix time ([entropy::emergency_system_time_entropy]), and screwing with them via XOR and endianness operations.
Feature Flags
std(default) - Enables Ruststdlib features, such as seeding from OS entropy sources.tls(default) - Enables a thread-local WyRand RNG (see below). Requirestlsto be enabled.wyrand(default) - Enable the wyrand RNG.pcg64(default) - Enable the Pcg64 RNG.chacha- Enable the ChaCha RNG. Requires Rust 1.47 or later.rdseed- On x86/x86_64 platforms, therdseedintrinsic will be used when OS entropy isn't available.zeroize- Implement the Zeroize trait for all RNGs.getrandom- Use thegetrandomcrate as an entropy source. Works on most systems, optional due to the fact that it brings in more dependencies.