Utilities for random number generation
Rand provides utilities to generate random numbers, to convert them touseful types and distributions, and some randomness-related algorithms.
To get you started quickly, the easiest and highest-level way to geta random value is to userandom()
; alternatively you can usethread_rng()
. TheRng
trait provides a useful API on all RNGs, whilethedistributions
andseq
modules provide furtherfunctionality on top of RNGs.
userand::prelude::*;ifrand::random() {// generates a boolean// Try printing a random unicode code point (probably a bad idea)!println!("char: {}",rand::random::<char>());}letmutrng=rand::thread_rng();lety:f64=rng.gen();// generates a float between 0 and 1letmutnums:Vec<i32>= (1..100).collect();nums.shuffle(&mutrng);
For the user guide and futher documentation, please readThe Rust Rand Book.
distributions | Generating random samples from probability distributions. |
prelude | Convenience re-export of common members |
prng | [ Deprecated ]Pseudo-random number generators. |
rngs | Random number generators and adapters for common usage: |
seq | Functions for randomly accessing and sampling sequences. |
Error | Error type of random number generators |
ErrorKind | Error kind which can be matched over. |
AsByteSliceMut | Trait for casting types to byte slices |
CryptoRng | A marker trait used to indicate that an |
FromEntropy | A convenience extension to |
Rng | An automatically-implemented extension trait on |
RngCore | The core of a random number generator. |
SeedableRng | A random number generator that can be explicitly seeded. |
random | Generates a random value using the thread-local random number generator. |
thread_rng | Retrieve the lazily-initialized thread-local random number generator,seeded by the system. Intended to be used in method chaining style,e.g. |
Prefix searches with a type followed by a colon (e.g.,fn:
) to restrict the search to a given type.
Accepted types are:fn
,mod
,struct
,enum
,trait
,type
,macro
, andconst
.
Search functions by type signature (e.g.,vec -> usize
or* -> vec
)
Search multiple things at once by splitting your query with comma (e.g.,str,u8
orString,struct:Vec,test
)