Expand description
Provides simple and fast pseudo/crypto random number generation.
§But why?
Because rand is very cool and powerful, but kind of an enormous fucking pain in the ass
to use, and it’s far too large and involved for someone who just a needs to flip a coin once
every 7 minutes. But if you’re doing some crazy black magic computational sorcery, it almost
certainly has something you can use to complete your spell.
Other crates, like fastrand, tinyrand, or oorandom, fall somewhere between “I’m not sure I trust the
backing RNG” to “this API is literally just rand but less powerful”. I wanted something easy, but
also fast and statistically robust.
So here we are.
§Usage
Glob import the contents of the library and use new_rng to create new RNGs wherever
you need them. Then call whatever method you require on those instances. All methods available
are directly accessible through any generator instance via the dot operator, and are named
in a way that should make it easy to quickly identify what you need.
If you need cryptographic security, enable the secure library feature and use
new_rng_secure instead.
“How do I access the thread-local RNG?” There isn’t one, and unless Rust finds a way to significantly improve the performance of the TLS implementation, there probably won’t ever be. Create a local instance when and where you need one, and use it while you need it.
use ya_rand::*;
let mut rng = new_rng();
let max: u64 = 69;
// Yep, that's all there is to it.
let val: u64 = rng.bound(max);
assert!(val < max);§Features
- std -
Enabled by default, but can be disabled for compatibility with
no_stdenvironments. Enables normal and exponential distributions, error type conversions for getrandom, and SIMD optimizations in therand_chachacrate. - inline -
Marks each
Generator::u64implementation with #[inline]. Should generally increase runtime performance at the cost of binary size and maybe compile time. You’ll have to test your specific use case to determine how much this feature will impact you. - secure -
Enables infrastructure for cryptographically secure random number generation via the
rand_chachacrate. Noticeably increases compile time and binary size.
§Details
This crate uses the xoshiro family of pseudo-random number generators. These generators are very fast (sub-ns when inlined), of very high statistical quality, and small. They aren’t cryptograpically secure, but most users don’t need their RNG to be secure, they just need it to be random and fast. This crate is intended to satisfy those needs, while also being easy to use and simple to understand. It also happens to be small and relatively fast to compile.
All generators output a distinct u64 value on each call, and the various methods used for transforming
those outputs into more usable forms are all high-quality and well-understood. Placing an upper bound
on these values uses Lemire’s method. Doing this inclusively or within a given range are both
applications of this same method with simple intermediary steps to alter the bound and apply shifts
when needed. This approach is unbiased and quite fast, but for very large bounds performance might degrade
slightly, since the algorithm needs to sample the underlying RNG more times to get an unbiased result.
If you know your bounds ahead of time, passing them as constants can help this issue, since the initial
division can be done at compile time when the value is known. Even better, if your bound happens to be a
power of 2, always use Generator::bits, since it’s nothing more than a bitshift of the u64 provided
by the RNG, and will always be as fast as possible.
Floating point values (besides the normal and exponential distributions) are all generated to be uniform,
with all their values being equidistant within their provided interval. They are not maximally dense,
if that’s something you need you’ll have to generate those values yourself. This approach is very fast, and
endorsed by both Lemire and Vigna (the author of the RNGs used in this crate). The normal distribution
is generated using the Marsaglia polar method, so it returns a pair of independently sampled f64 values.
Exponential variates are generated using this approach.
§Security
If you’re in the market for secure random number generation, you’ll want to enable the secure
feature, which provides SecureRng and the SecureGenerator trait. It functions identically to the
other provided RNGs, but with the addition of SecureGenerator::fill_bytes. The current implementation
uses ChaCha with 8 rounds via the rand_chacha crate. Unfortunately, this crate brings in a million other
dependencies and completely balloons compile times. In the future I’d like to look into doing a custom
implementation of ChaCha, but no timeline on that. Why only 8 rounds? Because people who are very
passionate about cryptography are convinced that’s enough, and I have zero reason to doubt them, nor any
capacity to prove them wrong. See the top of page 14 of the Too Much Crypto paper.
The security promises made to the user are identical to those made by ChaCha as an algorithm. It is up to you to determine if those guarantees meet the demands of your use case.
§Safety
In the pursuit of consistent performance and no runtime failures, there are no checks performed during
runtime in release mode. This means there are a couple areas where the end-user is able to receive garbage
after providing garbage. It is expected of the user to provide reasonable values where there is an input to
be given: values shouldn’t be on the verge of overflow and ranges should always have an end larger than their
start. There is minimal unsafe used, only in areas which directly benefit from it, and they are all brief
and easily determined to have no ill side-effects.
Structs§
- Secure
Rng - A cryptographically secure random number generator.
- Xoshiro256pp
- Rust implementation of the xoshiro256++ PRNG. This generator is fast, high-quality, and small, but not cryptographically secure.
- Xoshiro512pp
- Rust implementation of the xoshiro512++ PRNG. This generator is fast, high-quality, and small, but not cryptographically secure.
Traits§
Functions§
- new_rng
- The recommended way to create new PRNG instances.
- new_
rng_ secure - The recommended way to create new CRNG instances.
Type Aliases§
- Shiro
Rng - The recommended generator for all non-cryptographic purposes.