Expand description
Utilities for random number generation
Rand provides utilities to generate random numbers, to convert them touseful 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.userand::prelude::*;// Get an RNG:letmutrng = 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)aschar);// Generate and shuffle a sequence:letmutnums: Vec<i32> = (1..100).collect();nums.shuffle(&mutrng);// And take a random pick (yes, we didn't need to shuffle first!):let _= nums.choose(&mutrng);
§The Book
For the user guide and further documentation, please readThe Rust Rand Book.
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§
- Crypto
Rng - A marker trait used to indicate that an
RngCore
implementation issupposed to be cryptographically secure. - Fill
- Types which may be filled with random data
- Rng
- User-level interface for RNGs
- RngCore
- Implementation-level interface for RNGs
- Seedable
Rng - A random number generator that can be explicitly seeded.
- TryCrypto
Rng - A marker trait used to indicate that a
TryRngCore
implementation issupposed to be cryptographically secure. - TryRng
Core - A potentially fallible variant of
RngCore
Functions§
- fill
thread_rng
- Fill any type implementing
Fill
with random data - random
thread_rng
- Generate a random value using the thread-local random number generator.
- random_
bool thread_rng
- Return a bool with a probability
p
of 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/denominator
of beingtrue. - rng
thread_rng
- Access a fast, pre-initialized generator
- thread_
rng Deprecated thread_rng
- Access the thread-local generator