nanorand/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]
2#![forbid(missing_docs)]
3#![warn(
4 clippy::perf,
5 clippy::complexity,
6 clippy::style,
7 clippy::correctness,
8 clippy::missing_const_for_fn
9)]
10
11//! A library meant for fast, random number generation with quick compile time, and minimal dependencies.
12//!
13//! # Examples
14//! ## Generating a number with an initialized RNG
15//! ```rust
16//! use nanorand::{Rng, WyRand};
17//!
18//! let mut rng = WyRand::new();
19//! println!("Random number: {}", rng.generate::<u64>());
20//! ```
21//! ## Generating a number with a thread-local RNG
22//! ```rust
23//! use nanorand::Rng;
24//!
25//! let mut rng = nanorand::tls_rng();
26//! println!("Random number: {}", rng.generate::<u64>());
27//! ```
28//! ## Generating a number in a range
29//! ```rust
30//! use nanorand::{Rng, WyRand};
31//!
32//! let mut rng = WyRand::new();
33//! println!("Random number between 1 and 100: {}", rng.generate_range(1_u64..=100));
34//! println!("Random number between -100 and 50: {}", rng.generate_range(-100_i64..=50));
35//! ```
36//! ### Buffering random bytes
37//! ```rust
38//! use nanorand::{Rng, BufferedRng, WyRand};
39//!
40//! let mut thingy = [0u8; 5];
41//! let mut rng = BufferedRng::new(WyRand::new());
42//! rng.fill(&mut thingy);
43//! // As WyRand generates 8 bytes of output, and our target is only 5 bytes,
44//! // 3 bytes will remain in the buffer.
45//! assert_eq!(rng.buffered(), 3);
46//! ```
47//! ## Shuffling a Vec
48//! ```rust
49//! use nanorand::{Rng, WyRand};
50//!
51//! let mut rng = WyRand::new();
52//! let mut items = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
53//! rng.shuffle(&mut items);
54//! ```
55//!
56//! ## Why should I use this over...
57//!
58//! * `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.
59//! * `fastrand`, `oorandom`, `random-fast-rng`, or `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.
60//! * `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.
61//!
62//! ## RNG Implementations
63//!
64//! **RNG**|**nanorand type**|**Output Size**|**Cryptographically Secure**|**Speed**<sup>1</sup>|**Notes**|**Original Implementation**
65//! :-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:
66//! wyrand|[`nanorand::WyRand`](rand/wyrand/struct.WyRand.html), [`nanorand::tls::TlsWyRand`](tls/fn.tls_rng.html)|64 bits (`u64`)|🚫|16.4 GB/s||[https://github.com/lemire/testingRNG/blob/master/source/wyrand.h](https://github.com/lemire/testingRNG/blob/master/source/wyrand.h)
67//! Pcg64|[`nanorand::Pcg64`](rand/pcg64/struct.Pcg64.html)|64 bits (`u64`)|🚫|1.6 GB/s||[https://github.com/rkern/pcg64](https://github.com/rkern/pcg64)
68//! ChaCha|[`nanorand::ChaCha`](rand/chacha/struct.ChaCha.html)|512 bits (`[u32; 16]`)|✅|204 MB/s (ChaCha8), 79 MB/s (ChaCha20)|Only works in Rust 1.47 or above|[https://cr.yp.to/chacha.html](https://cr.yp.to/chacha.html)
69//!
70//! <sup>1. Speed benchmarked on an M1 Macbook Air</sup>
71//!
72//! ## Entropy Sources
73//! _Listed in order of priority_
74//!
75//! * If the `getrandom` feature is enabled, then [`getrandom::getrandom`](https://docs.rs/getrandom/*/getrandom/fn.getrandom.html) will be called, and no other entropy sources will be used.
76//! * If the `rdseed` feature is enabled, and is running on an x86(-64) system with the [RDSEED](https://en.wikipedia.org/wiki/RDRAND) instruction, then
77//! we will attempt to source as much entropy as possible via our [`rdseed_entropy`](entropy::rdseed_entropy) function
78//! * Linux and Android will attempt to use the [`getrandom`](https://man7.org/linux/man-pages/man2/getrandom.2.html) syscall.
79//! * macOS and iOS (Darwin-based systems) will use Security.framework's [`SecRandomCopyBytes`](https://developer.apple.com/documentation/security/1399291-secrandomcopybytes).
80//! * Windows
81//! * If we're targeting UWP, then the [`BCryptGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom) is used with system-preferred RNG (`BCRYPT_USE_SYSTEM_PREFERRED_RNG`).
82//! * Otherwise, we'll use [`RtlGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom).
83//!
84//! ## Feature Flags
85//!
86//! * `alloc` (default) - Enables Rust `alloc` lib features, such as a buffering Rng wrapper.
87//! * `std` (default) - Enables Rust `std` lib features, such as seeding from OS entropy sources. Requires `alloc` to be enabled.
88//! * `tls` (default) - Enables a thread-local [`WyRand`](rand/wyrand/struct.WyRand.html) RNG (see below). Requires `std` to be enabled.
89//! * `wyrand` (default) - Enable the [`WyRand`](rand/wyrand/struct.WyRand.html) RNG.
90//! * `pcg64` (default) - Enable the [`Pcg64`](rand/pcg64/struct.Pcg64.html) RNG.
91//! * `chacha` - Enable the [`ChaCha`](rand/chacha/struct.ChaCha.html) RNG. Requires Rust 1.47 or later.
92//! * `rdseed` - On x86 and x86-64 platforms, the `rdseed` intrinsic will be used when OS entropy isn't available.
93//! * `zeroize` - Implement the [Zeroize](https://crates.io/crates/zeroize) trait for all RNGs.
94//! * `getrandom` - Use the [`getrandom`](https://crates.io/crates/getrandom) crate as an entropy source. Works on most systems, optional due to the fact that it brings in more dependencies.
95//!
96//! ## MSRV
97//! The minimum supported Rust version for the latest version of nanorand is **Rust 1.56.0**, released October 21st, 2021.
98
99#[cfg(feature = "alloc")]
100extern crate alloc;
101
102#[cfg(feature = "alloc")]
103pub use buffer::BufferedRng;
104pub use gen::*;
105pub use rand::*;
106#[cfg(feature = "tls")]
107pub use tls::tls_rng;
108
109#[cfg(feature = "alloc")]
110/// Provides a buffered wrapper for RNGs, preventing bits from being wasted.
111pub mod buffer;
112/// Implementation of cryptography, for CSPRNGs.
113pub mod crypto;
114/// Sources for obtaining entropy.
115pub mod entropy;
116/// Traits for generating types from an RNG.
117pub mod gen;
118/// RNG algorithms.
119pub mod rand;
120#[cfg(feature = "tls")]
121/// Provides a thread-local [`WyRand`] RNG.
122pub mod tls;