Module std::rand

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::<float>().

See the distributions submodule for sampling random numbers from distributions like normal and exponential.

Examples

use std::rand;
use std::rand::Rng;

fn main() {
    let mut rng = rand::rng();
    if rng.gen() { // bool
        printfln!("int: %d, uint: %u", rng.gen(), rng.gen())
    }
}
use std::rand;

fn main () {
    let tuple_ptr = rand::random::<~(f64, char)>();
    printfln!(tuple_ptr)
}

Modules

distributions

Sampling from random distributions

Structs

IsaacRng

A random number generator that uses the ISAAC algorithm.

Weighted

A value with a particular weight compared to other values

XorShiftRng

An Xorshift random number generator.

Traits

Rand

A type that can be randomly generated using an Rng

Rng

A random number generator

Functions

random

Returns a random value of a Rand type, using the task's random number generator.

rng

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

seed

Create a new random seed.

task_rng

Gives back a lazily initialized task-local random number generator, seeded by the system. Intended to be used in method chaining style, ie task_rng().gen::<int>().

weak_rng

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