Module std::rand[src]
Utilities for random number generation
The key functions are random() and Rng::gen(). These are polymorphic
and so can be used to generate any type that implements Rand. Type inference
means that often a simple call to rand::random() or rng.gen() will
suffice, but sometimes an annotation is required, e.g. rand::random::<f64>().
See the distributions submodule for sampling random numbers from
distributions like normal and exponential.
Task-local RNG
There is built-in support for a RNG associated with each task stored
in task-local storage. This RNG can be accessed via task_rng, or
used implicitly via random. This RNG is normally randomly seeded
from an operating-system source of randomness, e.g. /dev/urandom on
Unix systems, and will automatically reseed itself from this source
after generating 32 KiB of random data.
Cryptographic security
An application that requires an entropy source for cryptographic purposes
must use OsRng, which reads randomness from the source that the operating
system provides (e.g. /dev/urandom on Unixes or CryptGenRandom() on Windows).
The other random number generators provided by this module are not suitable
for such purposes.
Note: many Unix systems provide /dev/random as well as /dev/urandom.
This module uses /dev/urandom for the following reasons:
- On Linux,
/dev/randommay block if entropy pool is empty;/dev/urandomwill not block. This does not mean that/dev/randomprovides better output than/dev/urandom; the kernel internally runs a cryptographically secure pseudorandom number generator (CSPRNG) based on entropy pool for random number generation, so the "quality" of/dev/randomis not better than/dev/urandomin most cases. However, this means that/dev/urandomcan yield somewhat predictable randomness if the entropy pool is very small, such as immediately after first booting. If an application likely to be run soon after first booting, or on a system with very few entropy sources, one should consider using/dev/randomviaReaderRng. - On some systems (e.g. FreeBSD, OpenBSD and Mac OS X) there is no difference
between the two sources. (Also note that, on some systems e.g. FreeBSD, both
/dev/randomand/dev/urandommay block once if the CSPRNG has not seeded yet.)
Examples
fn main() { use std::rand; use std::rand::Rng; let mut rng = rand::task_rng(); if rng.gen() { // random bool println!("int: {}, uint: {}", rng.gen::<int>(), rng.gen::<uint>()) } }use std::rand; use std::rand::Rng; let mut rng = rand::task_rng(); if rng.gen() { // random bool println!("int: {}, uint: {}", rng.gen::<int>(), rng.gen::<uint>()) }fn main() { use std::rand; let tuple = rand::random::<(f64, char)>(); println!("{}", tuple) }
use std::rand; let tuple = rand::random::<(f64, char)>(); println!("{}", tuple)
Modules
| distributions | Sampling from random distributions. |
| os | Interfaces to the operating system provided random number generators. |
| reader | A wrapper around any Reader to treat it as an RNG. |
| reseeding | A wrapper around another RNG that reseeds it after it generates a certain number of random bytes. |
Structs
| Closed01 | A wrapper for generating floating point numbers uniformly in the
closed interval |
| Isaac64Rng | A random number generator that uses ISAAC-64[1], the 64-bit variant of the ISAAC algorithm. |
| IsaacRng | A random number generator that uses the ISAAC algorithm[1]. |
| Open01 | A wrapper for generating floating point numbers uniformly in the
open interval |
| OsRng | A random number generator that retrieves randomness straight from the operating system. Platform sources: |
| StdRng | The standard RNG. This is designed to be efficient on the current platform. |
| TaskRng | The task-local RNG. |
| XorShiftRng | An Xorshift[1] random number generator. |
Traits
| Rand | A type that can be randomly generated using an |
| Rng | A random number generator. |
| SeedableRng | A random number generator that can be explicitly seeded to produce the same stream of randomness multiple times. |
Functions
| random | Generate a random value using the task-local random number generator. |
| sample | Randomly sample up to |
| task_rng | Retrieve the lazily-initialized task-local random number
generator, seeded by the system. Intended to be used in method
chaining style, e.g. |
| weak_rng | Create a weak random number generator with a default algorithm and seed. |