pub fn rng() -> ThreadRng
Expand description
Access a fast, pre-initialized generator
This is a handle to the local ThreadRng
.
See also crate::rngs
for alternatives.
§Example
use rand::prelude::*;
let mut numbers = [1, 2, 3, 4, 5];
numbers.shuffle(&mut rand::rng());
println!("Numbers: {numbers:?}");
// Using a local binding avoids an initialization-check on each usage:
let mut rng = rand::rng();
println!("True or false: {}", rng.random::<bool>());
println!("A simulated die roll: {}", rng.random_range(1..=6));
§Security
Refer to ThreadRng
.