Expand description
Utilities for random number generation
Rand provides utilities to generate random numbers, to convert them to useful types and distributions, and some randomness-related algorithms.
§Quick Start
// The prelude import enables methods we use below, specifically
// Rng::random, Rng::sample, SliceRandom::shuffle and IndexedRandom::choose.
use rand::prelude::*;
// Get an RNG:
let mut rng = rand::rng();
// Try printing a random unicode code point (probably a bad idea)!
println!("char: '{}'", rng.random::<char>());
// Try printing a random alphanumeric value instead!
println!("alpha: '{}'", rng.sample(rand::distr::Alphanumeric) as char);
// Generate and shuffle a sequence:
let mut nums: Vec<i32> = (1..100).collect();
nums.shuffle(&mut rng);
// And take a random pick (yes, we didn't need to shuffle first!):
let _ = nums.choose(&mut rng);§The Book
For the user guide and further documentation, please read The Rust Rand Book.
Re-exports§
- pub use rand_core;
Modules§
- distr
- Generating random samples from probability distributions
- prelude
- Convenience re-export of common members
- rngs
- Random number generators and adapters
- seq
- Sequence-related functionality
Traits§
- CryptoRng 
- A marker trait over RngCorefor securely unpredictable RNGs
- Fill
- Types which may be filled with random data
- Rng
- User-level interface for RNGs
- RngCore
- Implementation-level interface for RNGs
- SeedableRng 
- A random number generator that can be explicitly seeded.
- TryCryptoRng 
- A marker trait over TryRngCorefor securely unpredictable RNGs
- TryRngCore 
- A potentially fallible variant of RngCore
Functions§
- fillthread_rng
- Fill any type implementing Fillwith random data
- randomthread_rng
- Generate a random value using the thread-local random number generator.
- random_bool thread_rng
- Return a bool with a probability pof being true.
- random_iter thread_rng
- Return an iterator over random()variates
- random_range thread_rng
- Generate a random value in the given range using the thread-local random number generator.
- random_ratio thread_rng
- Return a bool with a probability of numerator/denominatorof being true.
- rngthread_rng
- Access a fast, pre-initialized generator
- thread_rng Deprecated thread_rng
- Access the thread-local generator