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:

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 [0,1] (including both endpoints).

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 (0,1) (not including either endpoint).

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.

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 n elements from an iterator.

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. task_rng().gen::<int>().

weak_rng

Create a weak random number generator with a default algorithm and seed.