Random number generation
Example
use std::random::thread_rng;
// Roll two dice
let first: i32 = thread_rng().next(1..=6);
let second: i32 = thread_rng().next(1..=6);
println!("You've rolled {} and {}", first, second);
Run this example
This module consists of a protocol Rng which can be used to implement a random number generator that provides "raw randomness", Distribution protocol and various concrete implementations, such as Normal and Bernoulli that can be used to sample random values of a particular kind, and RngExt mixin which provides convenience methods for common use cases.
The module also provides a Pcg32, a decent general purpose pseudo-random generator, OsRng backed by the OS-provided random number generator, and thread_rng, a convenience thread-local random generator that does not need manual seeding.
Protocols
Structs
true with probability p and false with
probability 1 - p.
[0, 1) interval.
(0, 1] interval.
(0, 1) interval.
Functions
-
fn thread_rng() -> &mut DefaultRng
A thread-local RNG.
Lazily seeded from OsRng on first access from each thread.
Example
use std::random::thread_rng; println!("{}", thread_rng().next_u64());Run this exampleThe returned pointer is safe to store, but not safe to send across threads.
Types
-
type DefaultRng = Pcg32
Default general purpose random number generator.