Skip to main content

rand/
lib.rs

1// Copyright 2018 Developers of the Rand project.
2// Copyright 2013-2017 The Rust Project Developers.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! Utilities for random number generation
11//!
12//! Rand provides utilities to generate random numbers, to convert them to
13//! useful types and distributions, and some randomness-related algorithms.
14//!
15//! # Quick Start
16//!
17//! ```
18//! // The prelude import enables methods we use below, specifically
19//! // Rng::random, Rng::sample, SliceRandom::shuffle and IndexedRandom::choose.
20//! use rand::prelude::*;
21//!
22//! // Get an RNG:
23//! let mut rng = rand::rng();
24//!
25//! // Try printing a random unicode code point (probably a bad idea)!
26//! println!("char: '{}'", rng.random::<char>());
27//! // Try printing a random alphanumeric value instead!
28//! println!("alpha: '{}'", rng.sample(rand::distr::Alphanumeric) as char);
29//!
30//! // Generate and shuffle a sequence:
31//! let mut nums: Vec<i32> = (1..100).collect();
32//! nums.shuffle(&mut rng);
33//! // And take a random pick (yes, we didn't need to shuffle first!):
34//! let _ = nums.choose(&mut rng);
35//! ```
36//!
37//! # The Book
38//!
39//! For the user guide and further documentation, please read
40//! [The Rust Rand Book](https://rust-random.github.io/book).
41
42#![doc(
43    html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
44    html_favicon_url = "https://www.rust-lang.org/favicon.ico"
45)]
46#![deny(missing_docs)]
47#![deny(missing_debug_implementations)]
48#![doc(test(attr(allow(unused_variables), deny(warnings))))]
49#![no_std]
50#![cfg_attr(feature = "simd_support", feature(portable_simd))]
51#![cfg_attr(
52    all(feature = "simd_support", target_feature = "avx512bw"),
53    feature(stdarch_x86_avx512)
54)]
55#![cfg_attr(docsrs, feature(doc_cfg))]
56#![allow(
57    clippy::float_cmp,
58    clippy::neg_cmp_op_on_partial_ord,
59    clippy::nonminimal_bool
60)]
61#![deny(clippy::undocumented_unsafe_blocks)]
62
63#[cfg(feature = "alloc")]
64extern crate alloc;
65#[cfg(feature = "std")]
66extern crate std;
67
68// Re-export rand_core itself
69pub use rand_core;
70
71// Re-exports from rand_core
72pub use rand_core::{CryptoRng, Rng, SeedableRng, TryCryptoRng, TryRng};
73
74#[macro_use]
75mod log_macros;
76
77// Public modules
78pub mod distr;
79pub mod prelude;
80mod rng;
81pub mod rngs;
82pub mod seq;
83
84// Public exports
85#[cfg(feature = "thread_rng")]
86pub use crate::rngs::thread::rng;
87
88pub use rng::{Fill, RngExt};
89
90#[cfg(feature = "thread_rng")]
91use crate::distr::{Distribution, StandardUniform};
92
93/// Construct and seed an RNG
94///
95/// This method yields a seeded RNG, using [`rng`] ([`ThreadRng`]) if enabled or
96/// [`SysRng`] otherwise.
97///
98/// # Examples
99///
100/// ```
101/// let mut rng: rand::rngs::SmallRng = rand::make_rng();
102/// # let _ = rand::Rng::next_u32(&mut rng);
103/// ```
104///
105/// # Security
106///
107/// Refer to [`ThreadRng#Security`].
108///
109/// [`SysRng`]: crate::rngs::SysRng
110/// [`ThreadRng`]: crate::rngs::ThreadRng
111/// [`ThreadRng#Security`]: crate::rngs::ThreadRng#security
112#[cfg(feature = "sys_rng")]
113pub fn make_rng<R: SeedableRng>() -> R {
114    #[cfg(feature = "thread_rng")]
115    {
116        R::from_rng(&mut rng())
117    }
118
119    #[cfg(not(feature = "thread_rng"))]
120    {
121        R::try_from_rng(&mut rngs::SysRng).expect("unexpected failure from SysRng")
122    }
123}
124
125/// Adapter to support [`std::io::Read`] over a [`TryRng`]
126///
127/// # Examples
128///
129/// ```no_run
130/// use std::{io, io::Read};
131/// use std::fs::File;
132/// use rand::{rngs::SysRng, RngReader};
133///
134/// io::copy(
135///     &mut RngReader(SysRng).take(100),
136///     &mut File::create("/tmp/random.bytes").unwrap()
137/// ).unwrap();
138/// ```
139#[cfg(feature = "std")]
140pub struct RngReader<R: TryRng>(pub R);
141
142#[cfg(feature = "std")]
143impl<R: TryRng> std::io::Read for RngReader<R> {
144    #[inline]
145    fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
146        self.0
147            .try_fill_bytes(buf)
148            .map_err(|err| std::io::Error::other(std::format!("RNG error: {err}")))?;
149        Ok(buf.len())
150    }
151}
152
153#[cfg(feature = "std")]
154impl<R: TryRng> std::fmt::Debug for RngReader<R> {
155    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
156        f.debug_tuple("RngReader").finish()
157    }
158}
159
160/// Generate a random value using the thread-local random number generator.
161///
162/// This function is shorthand for <code>[rng()].[random()](RngExt::random)</code>:
163///
164/// -   See [`ThreadRng`] for documentation of the generator and security
165/// -   See [`StandardUniform`] for documentation of supported types and distributions
166///
167/// # Examples
168///
169/// ```
170/// let x = rand::random::<u8>();
171/// println!("{}", x);
172///
173/// let y = rand::random::<f64>();
174/// println!("{}", y);
175///
176/// if rand::random() { // generates a boolean
177///     println!("Better lucky than good!");
178/// }
179/// ```
180///
181/// If you're calling `random()` repeatedly, consider using a local `rng`
182/// handle to save an initialization-check on each usage:
183///
184/// ```
185/// use rand::RngExt; // provides the `random` method
186///
187/// let mut rng = rand::rng(); // a local handle to the generator
188///
189/// let mut v = vec![1, 2, 3];
190///
191/// for x in v.iter_mut() {
192///     *x = rng.random();
193/// }
194/// ```
195///
196/// [`StandardUniform`]: distr::StandardUniform
197/// [`ThreadRng`]: rngs::ThreadRng
198#[cfg(feature = "thread_rng")]
199#[inline]
200pub fn random<T>() -> T
201where
202    StandardUniform: Distribution<T>,
203{
204    rng().random()
205}
206
207/// Return an iterator over [`random()`] variates
208///
209/// This function is shorthand for
210/// <code>[rng()].[random_iter](RngExt::random_iter)()</code>.
211///
212/// # Example
213///
214/// ```
215/// let v: Vec<i32> = rand::random_iter().take(5).collect();
216/// println!("{v:?}");
217/// ```
218#[cfg(feature = "thread_rng")]
219#[inline]
220pub fn random_iter<T>() -> distr::Iter<StandardUniform, rngs::ThreadRng, T>
221where
222    StandardUniform: Distribution<T>,
223{
224    rng().random_iter()
225}
226
227/// Generate a random value in the given range using the thread-local random number generator.
228///
229/// This function is shorthand for
230/// <code>[rng()].[random_range](RngExt::random_range)(<var>range</var>)</code>.
231///
232/// # Example
233///
234/// ```
235/// let y: f32 = rand::random_range(0.0..=1e9);
236/// println!("{}", y);
237///
238/// let words: Vec<&str> = "Mary had a little lamb".split(' ').collect();
239/// println!("{}", words[rand::random_range(..words.len())]);
240/// ```
241/// Note that the first example can also be achieved (without `collect`'ing
242/// to a `Vec`) using [`seq::IteratorRandom::choose`].
243#[cfg(feature = "thread_rng")]
244#[inline]
245pub fn random_range<T, R>(range: R) -> T
246where
247    T: distr::uniform::SampleUniform,
248    R: distr::uniform::SampleRange<T>,
249{
250    rng().random_range(range)
251}
252
253/// Return a bool with a probability `p` of being true.
254///
255/// This function is shorthand for
256/// <code>[rng()].[random_bool](RngExt::random_bool)(<var>p</var>)</code>.
257///
258/// # Example
259///
260/// ```
261/// println!("{}", rand::random_bool(1.0 / 3.0));
262/// ```
263///
264/// # Panics
265///
266/// If `p < 0` or `p > 1`.
267#[cfg(feature = "thread_rng")]
268#[inline]
269#[track_caller]
270pub fn random_bool(p: f64) -> bool {
271    rng().random_bool(p)
272}
273
274/// Return a bool with a probability of `numerator/denominator` of being
275/// true.
276///
277/// That is, `random_ratio(2, 3)` has chance of 2 in 3, or about 67%, of
278/// returning true. If `numerator == denominator`, then the returned value
279/// is guaranteed to be `true`. If `numerator == 0`, then the returned
280/// value is guaranteed to be `false`.
281///
282/// See also the [`Bernoulli`] distribution, which may be faster if
283/// sampling from the same `numerator` and `denominator` repeatedly.
284///
285/// This function is shorthand for
286/// <code>[rng()].[random_ratio](RngExt::random_ratio)(<var>numerator</var>, <var>denominator</var>)</code>.
287///
288/// # Panics
289///
290/// If `denominator == 0` or `numerator > denominator`.
291///
292/// # Example
293///
294/// ```
295/// println!("{}", rand::random_ratio(2, 3));
296/// ```
297///
298/// [`Bernoulli`]: distr::Bernoulli
299#[cfg(feature = "thread_rng")]
300#[inline]
301#[track_caller]
302pub fn random_ratio(numerator: u32, denominator: u32) -> bool {
303    rng().random_ratio(numerator, denominator)
304}
305
306/// Fill any type implementing [`Fill`] with random data
307///
308/// This function is shorthand for
309/// <code>[rng()].[fill](RngExt::fill)(<var>dest</var>)</code>.
310///
311/// # Example
312///
313/// ```
314/// let mut arr = [0i8; 20];
315/// rand::fill(&mut arr[..]);
316/// ```
317///
318/// Note that you can instead use [`random()`] to generate an array of random
319/// data, though this is slower for small elements (smaller than the RNG word
320/// size).
321#[cfg(feature = "thread_rng")]
322#[inline]
323#[track_caller]
324pub fn fill<T: Fill>(dest: &mut [T]) {
325    Fill::fill_slice(dest, &mut rng())
326}
327
328#[cfg(test)]
329mod test {
330    use super::*;
331    use core::convert::Infallible;
332
333    /// Construct a deterministic RNG with the given seed
334    pub fn rng(seed: u64) -> impl Rng {
335        // For tests, we want a statistically good, fast, reproducible RNG.
336        // PCG32 will do fine, and will be easy to embed if we ever need to.
337        const INC: u64 = 11634580027462260723;
338        rand_pcg::Pcg32::new(seed, INC)
339    }
340
341    /// Construct a generator yielding a constant value
342    pub fn const_rng(x: u64) -> StepRng {
343        StepRng(x, 0)
344    }
345
346    /// Construct a generator yielding an arithmetic sequence
347    pub fn step_rng(x: u64, increment: u64) -> StepRng {
348        StepRng(x, increment)
349    }
350
351    #[derive(Clone)]
352    pub struct StepRng(u64, u64);
353    impl TryRng for StepRng {
354        type Error = Infallible;
355
356        fn try_next_u32(&mut self) -> Result<u32, Infallible> {
357            self.try_next_u64().map(|x| x as u32)
358        }
359
360        fn try_next_u64(&mut self) -> Result<u64, Infallible> {
361            let res = self.0;
362            self.0 = self.0.wrapping_add(self.1);
363            Ok(res)
364        }
365
366        fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Infallible> {
367            rand_core::utils::fill_bytes_via_next_word(dst, || self.try_next_u64())
368        }
369    }
370
371    #[cfg(feature = "std")]
372    #[test]
373    fn rng_reader() {
374        use std::io::Read;
375
376        let mut rng = StepRng(255, 1);
377        let mut buf = [0u8; 24];
378        let expected = [
379            255, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
380        ];
381
382        RngReader(&mut rng).read_exact(&mut buf).unwrap();
383        assert_eq!(&buf, &expected);
384
385        RngReader(StepRng(255, 1)).read_exact(&mut buf).unwrap();
386        assert_eq!(&buf, &expected);
387    }
388
389    #[test]
390    #[cfg(feature = "thread_rng")]
391    fn test_random() {
392        let _n: u64 = random();
393        let _f: f32 = random();
394        #[allow(clippy::type_complexity)]
395        let _many: (
396            (),
397            [(u32, bool); 3],
398            (u8, i8, u16, i16, u32, i32, u64, i64),
399            (f32, (f64, (f64,))),
400        ) = random();
401    }
402
403    #[test]
404    #[cfg(feature = "thread_rng")]
405    fn test_range() {
406        let _n: usize = random_range(42..=43);
407        let _f: f32 = random_range(42.0..43.0);
408    }
409}