ya_rand/
lib.rs

1/*!
2# YA-Rand: Yet Another Rand
3
4Simple and fast pseudo/crypto random number generation.
5
6## Performance considerations
7
8The backing CRNG uses compile-time dispatch, so you'll only get the fastest implementation available to the
9machine if rust knows what kind of machine to compile for.
10
11Your best bet is to configure your global .cargo/config.toml with `rustflags = ["-C", "target-cpu=native"]`
12beneath the `[build]` directive.
13
14If you know the [x86 feature level] of the processor that will be executing your binaries,
15it maybe be better to instead configure this directive at the crate level.
16
17[x86 feature level]: https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels
18
19## But why?
20
21Because `rand` is very cool and extremely powerful, but kind of an enormous fucking pain in the ass
22to use, and it's far too large and involved for someone who just needs to flip a coin once
23every few minutes. But if you're doing some crazy black magic numerical sorcery, it almost certainly
24has something you can use to complete your spell. Don't be afraid to go there if you need to.
25
26Other crates, like `fastrand`, `tinyrand`, or `oorandom`, fall somewhere between "I'm not sure I trust
27the backing RNG" (state size is too small or algorithm is iffy) and "this API is literally just
28`rand` but far less powerful". I wanted something easy to use, but also fast and statistically robust.
29
30So here we are.
31
32## Usage
33
34Import the contents of the library and use [`new_rng`] to create new RNGs wherever
35you need them. Then call whatever method you require on those instances. All methods available
36are directly accessible through any generator instance via the dot operator, and are named
37in a way that should make it easy to quickly identify what you need. See below for a few examples.
38
39If you need cryptographic security, [`new_rng_secure`] will provide you with a [`SecureRng`] instance,
40suitable for use in secure contexts.
41
42"How do I access the thread-local RNG?" There isn't one, and unless Rust improves the performance and
43ergonomics of the TLS implementation, there probably won't ever be. Create a local instance when and
44where you need one and use it while you need it. If you need an RNG to stick around for a while, passing
45it between functions or storing it in structs is a perfectly valid solution.
46
47```
48use ya_rand::*;
49
50// **Correct** instantiation is very easy.
51// Seeds a PRNG instance using operating system entropy,
52// so you never have to worry about the quality of the
53// initial state.
54let mut rng = new_rng();
55
56// Generate a random number with a given upper bound.
57let max: u64 = 420;
58let val = rng.bound(max);
59assert!(val < max);
60
61// Generate a random number in a given range.
62let min: i64 = -69;
63let max: i64 = 69;
64let val = rng.range(min, max);
65assert!(min <= val && val < max);
66
67// Generate a random floating point value.
68let val = rng.f64();
69assert!(0.0 <= val && val < 1.0);
70
71// Generate a random ascii digit: '0'..='9' as a char.
72let digit = rng.ascii_digit();
73assert!(digit.is_ascii_digit());
74
75// Seeds a CRNG instance with OS entropy.
76let mut secure_rng = new_rng_secure();
77
78// We still have access to all the same methods...
79let val = rng.f64();
80assert!(0.0 <= val && val < 1.0);
81
82// ...but since the CRNG is secure, we also
83// get some nice extras.
84// Here, we generate a string of random hexidecimal
85// characters (base 16), with the shortest length guaranteed
86// to be secure.
87use ya_rand::encoding::*;
88let s = secure_rng.text::<Base16>(Base16::MIN_LEN);
89assert!(s.len() == Base16::MIN_LEN);
90```
91
92## Features
93
94* **std** -
95    Enabled by default, but can be disabled for use in `no_std` environments. Enables normal/exponential
96    distributions, error type conversions for getrandom, and the **alloc** feature.
97* **alloc** -
98    Enabled by default. Normally enabled through **std**, but can be enabled on it's own for use in
99    `no_std` environments which provide allocation primitives. Enables random generation of secure
100    `String` values when using [`SecureRng`].
101* **secure** -
102    Enabled by default. Provides [`SecureRng`], which implements [SecureGenerator]. The backing generator
103    is ChaCha with 8 rounds and a 64-bit counter.
104* **inline** -
105    Marks all [`Generator::u64`] implementations with `#[inline]`. Should generally increase
106    runtime performance at the cost of binary size and compile time.
107    You'll have to test your specific use case to determine if this feature is worth it for you;
108    all the RNGs provided tend to be plenty fast without additional inlining.
109
110## Details
111
112This crate primarily uses the [xoshiro] family for pseudo-random number generators. These generators are
113very fast, of [very high statistical quality], and small. They aren't cryptographically secure,
114but most users don't need their RNG to be secure, they just need it to be random and fast. The default
115generator is xoshiro256++, which should provide a large enough period for most users. The xoshiro512++
116generator is also provided in case you need a longer period.
117
118[xoshiro]: https://prng.di.unimi.it/
119[very high statistical quality]: https://vigna.di.unimi.it/ftp/papers/ScrambledLinear.pdf
120
121Since version 2.0, [`RomuTrio`] and [`RomuQuad`] from the [romurand] family are also provided. These are
122non-linear generators which can be ever-so-slightly faster than the xoshiro generators, particularly when
123the `inline` feature is enabled. But in practice this difference likely won't be measurable. Unless you're
124especially fond of the statistical properties of the romurand generators, this crates default generator
125should be more than enough.
126
127[romurand]: https://romu-random.org/
128
129All generators output a distinct `u64` value on each call, and the various methods used for transforming
130those outputs into more usable forms are all high-quality and well-understood. Placing an upper bound
131on these values uses [Lemire's method]. Both inclusive bounding and range-based bounding are applications
132of this method, with a few intermediary steps to adjust the bound and apply shifts as needed.
133This approach is unbiased and quite fast, but for very large bounds performance might degrade slightly,
134since the algorithm may need to sample the underlying RNG multiple times to get an unbiased result.
135But this is just a byproduct of how the underlying algorithm works, and isn't something you should ever be
136worried about when using the aforementioned methods, since these resamples are few and far between.
137If your bound happens to be a power of 2, always use [`Generator::bits`], since it's nothing more
138than a bit-shift of the original `u64` provided by the RNG, and will always be as fast as possible.
139
140Floating point values (besides the normal and exponential distributions) are uniformly distributed,
141with all the possible outputs being equidistant within the given interval. They are **not** maximally dense;
142if that's something you need, you'll have to generate those values yourself. This approach is very fast, and
143endorsed by both [Lemire] and [Vigna] (the author of the RNGs used in this crate). The normal distribution
144implementation uses the [Marsaglia polar method], returning pairs of independently sampled `f64` values.
145Exponential variates are generated using [this approach].
146
147[Lemire's method]: https://arxiv.org/abs/1805.10941
148[Lemire]: https://lemire.me/blog/2017/02/28/how-many-floating-point-numbers-are-in-the-interval-01/
149[Vigna]: https://prng.di.unimi.it/#remarks
150[Marsaglia polar method]: https://en.wikipedia.org/wiki/Marsaglia_polar_method
151[this approach]: https://en.wikipedia.org/wiki/Exponential_distribution#Random_variate_generation
152
153## Security
154
155If you're in the market for secure random number generation, this crate provides a secure generator backed
156by a highly optimized ChaCha8 implementation from the [`chachacha`] crate.
157It functions identically to the other provided RNGs, but with added functionality that wouldn't be safe to
158use on pseudo RNGs. Why only 8 rounds? Because people who are very passionate about cryptography are convinced
159that's enough, and I have zero reason to doubt them, nor any capacity to prove them wrong.
160See page 14 of the [`Too Much Crypto`] paper if you're interested in the justification.
161
162The security guarantees made to the user are identical to those made by ChaCha as an algorithm. It is up
163to you to determine if those guarantees meet the demands of your use case.
164
165I reserve the right to change the backing implementation at any time to another RNG which is at least as secure,
166without changing the API or bumping the major/minor version. Realistically, this just means I'm willing to bump
167this to ChaCha12 if ChaCha8 is ever compromised.
168
169[`Too Much Crypto`]: https://eprint.iacr.org/2019/1492
170
171## Safety
172
173Generators are seeded using entropy from the underlying OS, and have the potential to fail during creation.
174But in practice this is extraordinarily unlikely, and isn't something the end-user should ever worry about.
175Modern Windows versions (10 and newer) have a crypto subsystem that will never fail during runtime, and
176rust can trivially remove the failure branch when compiling binaries for those systems.
177*/
178
179#![allow(clippy::doc_overindented_list_items)]
180#![deny(missing_docs)]
181#![no_std]
182
183#[cfg(all(feature = "alloc", feature = "secure"))]
184extern crate alloc;
185
186#[cfg(all(feature = "alloc", feature = "secure"))]
187pub mod encoding;
188mod rng;
189mod romuquad;
190mod romutrio;
191#[cfg(feature = "secure")]
192mod secure;
193mod util;
194mod xoshiro256pp;
195mod xoshiro512pp;
196
197pub use self::rng::{Generator, SecureGenerator, SeedableGenerator};
198pub use romuquad::RomuQuad;
199pub use romutrio::RomuTrio;
200#[cfg(feature = "secure")]
201pub use secure::SecureRng;
202pub use xoshiro256pp::Xoshiro256pp;
203pub use xoshiro512pp::Xoshiro512pp;
204
205/// The recommended generator for all non-cryptographic purposes.
206pub type ShiroRng = Xoshiro256pp;
207
208/// The recommended way to create new PRNG instances.
209///
210/// Identical to calling [`ShiroRng::new`] or [`Xoshiro256pp::new`].
211#[inline]
212pub fn new_rng() -> ShiroRng {
213    ShiroRng::new()
214}
215
216/// The recommended way to create new CRNG instances.
217///
218/// Identical to calling [`SecureRng::new`].
219#[cfg(feature = "secure")]
220#[inline]
221pub fn new_rng_secure() -> SecureRng {
222    SecureRng::new()
223}
224
225#[cfg(test)]
226mod tests {
227    use super::encoding::*;
228    use super::*;
229    use alloc::collections::BTreeSet;
230
231    const ITERATIONS: usize = 12357;
232    const ITERATIONS_LONG: usize = 1 << 24;
233
234    #[test]
235    pub fn ascii_alphabetic() {
236        let mut rng = new_rng();
237        let mut vals = BTreeSet::new();
238        for _ in 0..ITERATIONS {
239            let result = rng.ascii_alphabetic();
240            assert!(result.is_ascii_alphabetic());
241            vals.insert(result);
242        }
243        assert!(vals.len() == 52);
244    }
245
246    #[test]
247    pub fn ascii_uppercase() {
248        let mut rng = new_rng();
249        let mut vals = BTreeSet::new();
250        for _ in 0..ITERATIONS {
251            let result = rng.ascii_uppercase();
252            assert!(result.is_ascii_uppercase());
253            vals.insert(result);
254        }
255        assert!(vals.len() == 26);
256    }
257
258    #[test]
259    pub fn ascii_lowercase() {
260        let mut rng = new_rng();
261        let mut vals = BTreeSet::new();
262        for _ in 0..ITERATIONS {
263            let result = rng.ascii_lowercase();
264            assert!(result.is_ascii_lowercase());
265            vals.insert(result);
266        }
267        assert!(vals.len() == 26);
268    }
269
270    #[test]
271    pub fn ascii_alphanumeric() {
272        let mut rng = new_rng();
273        let mut vals = BTreeSet::new();
274        for _ in 0..ITERATIONS {
275            let result = rng.ascii_alphanumeric();
276            assert!(result.is_ascii_alphanumeric());
277            vals.insert(result);
278        }
279        assert!(vals.len() == 62);
280    }
281
282    #[test]
283    pub fn ascii_digit() {
284        let mut rng = new_rng();
285        let mut vals = BTreeSet::new();
286        for _ in 0..ITERATIONS {
287            let result = rng.ascii_digit();
288            assert!(result.is_ascii_digit());
289            vals.insert(result);
290        }
291        assert!(vals.len() == 10);
292    }
293
294    #[test]
295    fn text_base64() {
296        test_text::<Base64>();
297    }
298
299    #[test]
300    fn text_base64_url() {
301        test_text::<Base64Url>();
302    }
303
304    #[test]
305    fn text_base62() {
306        test_text::<Base62>();
307    }
308
309    #[test]
310    fn text_base32() {
311        test_text::<Base32>();
312    }
313
314    #[test]
315    fn text_base32_hex() {
316        test_text::<Base32Hex>();
317    }
318
319    #[test]
320    fn text_base16() {
321        test_text::<Base16>();
322    }
323
324    fn test_text<E: Encoder>() {
325        let s = new_rng_secure().text::<E>(ITERATIONS);
326        let distinct_bytes = s.bytes().collect::<BTreeSet<_>>();
327        let distinct_chars = s.chars().collect::<BTreeSet<_>>();
328
329        let lengths_are_equal = ITERATIONS == s.len()
330            && E::CHARSET.len() == distinct_bytes.len()
331            && E::CHARSET.len() == distinct_chars.len();
332        assert!(lengths_are_equal);
333
334        let contains_all_values = E::CHARSET.iter().all(|c| distinct_bytes.contains(c));
335        assert!(contains_all_values);
336
337        // Pretty sure this isn't necessary because of the above length
338        // checks but extra testing is fine by me.
339        let all_values_are_ascii = distinct_chars.iter().all(|c| c.is_ascii());
340        assert!(all_values_are_ascii);
341    }
342
343    #[test]
344    fn wide_mul() {
345        const SHIFT: u32 = 48;
346        const EXPECTED_HIGH: u64 = 1 << ((SHIFT * 2) - u64::BITS);
347        const EXPECTED_LOW: u64 = 0;
348        let x = 1 << SHIFT;
349        let y = x;
350        // 2^48 * 2^48 = 2^96
351        let (high, low) = util::wide_mul(x, y);
352        assert!(high == EXPECTED_HIGH);
353        assert!(low == EXPECTED_LOW);
354    }
355
356    #[test]
357    fn f64() {
358        let mut rng = new_rng();
359        for _ in 0..ITERATIONS_LONG {
360            let val = rng.f64();
361            assert!((0.0..1.0).contains(&val));
362        }
363    }
364
365    #[test]
366    fn f32() {
367        let mut rng = new_rng();
368        for _ in 0..ITERATIONS_LONG {
369            let val = rng.f32();
370            assert!((0.0..1.0).contains(&val));
371        }
372    }
373
374    #[test]
375    fn f64_nonzero() {
376        let mut rng = new_rng();
377        for _ in 0..ITERATIONS_LONG {
378            let val = rng.f64_nonzero();
379            assert!(0.0 < val && val <= 1.0);
380        }
381    }
382
383    #[test]
384    fn f32_nonzero() {
385        let mut rng = new_rng();
386        for _ in 0..ITERATIONS_LONG {
387            let val = rng.f32_nonzero();
388            assert!(0.0 < val && val <= 1.0);
389        }
390    }
391
392    #[test]
393    fn f64_wide() {
394        let mut rng = new_rng();
395        for _ in 0..ITERATIONS_LONG {
396            let val = rng.f64_wide();
397            assert!(val.abs() < 1.0);
398        }
399    }
400
401    #[test]
402    fn f32_wide() {
403        let mut rng = new_rng();
404        for _ in 0..ITERATIONS_LONG {
405            let val = rng.f32_wide();
406            assert!(val.abs() < 1.0);
407        }
408    }
409}