Random number generation

Type Weighted

type Weighted<T> = {weight: uint, item: T,}

A value with a particular weight compared to other values

Type XorShiftState

type XorShiftState = {mut x: u32, mut y: u32, mut z: u32, mut w: u32,}

Enum rctx

Struct RandRes

struct RandRes {
    c: *rctx,
}

Interface Rng

A random number generator

Method next

fn next() -> u32

Return the next random integer

Implementation for Rng

Extension methods for random number generators

Method gen_int

fn gen_int() -> int

Return a random int

Method gen_int_range

fn gen_int_range(start: int, end: int) -> int

Return an int randomly chosen from the range [start, end), failing if start >= end

Method gen_i8

fn gen_i8() -> i8

Return a random i8

Method gen_i16

fn gen_i16() -> i16

Return a random i16

Method gen_i32

fn gen_i32() -> i32

Return a random i32

Method gen_i64

fn gen_i64() -> i64

Return a random i64

Method gen_uint

fn gen_uint() -> uint

Return a random uint

Method gen_uint_range

fn gen_uint_range(start: uint, end: uint) -> uint

Return a uint randomly chosen from the range [start, end), failing if start >= end

Method gen_u8

fn gen_u8() -> u8

Return a random u8

Method gen_u16

fn gen_u16() -> u16

Return a random u16

Method gen_u32

fn gen_u32() -> u32

Return a random u32

Method gen_u64

fn gen_u64() -> u64

Return a random u64

Method gen_float

fn gen_float() -> float

Return a random float in the interval [0,1]

Method gen_f32

fn gen_f32() -> f32

Return a random f32 in the interval [0,1]

Method gen_f64

fn gen_f64() -> f64

Return a random f64 in the interval [0,1]

Method gen_char

fn gen_char() -> char

Return a random char

Method gen_char_from

fn gen_char_from(chars: &str) -> char

Return a char randomly chosen from chars, failing if chars is empty

Method gen_bool

fn gen_bool() -> bool

Return a random bool

Method gen_weighted_bool

fn gen_weighted_bool(n: uint) -> bool

Return a bool with a 1 in n chance of true

Method gen_str

fn gen_str(len: uint) -> ~str

Return a random string of the specified length composed of A-Z,a-z,0-9

Method gen_bytes

fn gen_bytes(len: uint) -> ~[u8]

Return a random byte string of the specified length

Method choose

fn choose<T: Copy>(values: &[T]) -> T

Choose an item randomly, failing if values is empty

Method choose_option

fn choose_option<T: Copy>(values: &[T]) -> Option<T>

Choose Some(item) randomly, returning None if values is empty

Method choose_weighted

fn choose_weighted<T: Copy>(v: &[Weighted<T>]) -> T

Choose an item respecting the relative weights, failing if the sum of the weights is 0

Method choose_weighted_option

fn choose_weighted_option<T: Copy>(v: &[Weighted<T>]) -> Option<T>

Choose Some(item) respecting the relative weights, returning none if the sum of the weights is 0

Method weighted_vec

fn weighted_vec<T: Copy>(v: &[Weighted<T>]) -> ~[T]

Return a vec containing copies of the items, in order, where the weight of the item determines how many copies there are

Method shuffle

fn shuffle<T: Copy>(values: &[T]) -> ~[T]

Shuffle a vec

Method shuffle_mut

fn shuffle_mut<T>(values: &[mut T])

Shuffle a mutable vec in place

Implementation of Rng for @RandRes

Method next

fn next() -> u32

Implementation of Rng for XorShiftState

Method next

fn next() -> u32

Function RandRes

fn RandRes(c: *rctx) -> RandRes

Function Rng

fn Rng() -> Rng

Create a random number generator with a system specified seed

Function random

fn random() -> uint

Returns a random uint, using the task's based random number generator.

Function seed

fn seed() -> ~[u8]

Create a new random seed for seeded_rng

Function seeded_rng

fn seeded_rng(seed: &~[u8]) -> Rng

Create a random number generator using the specified seed. A generator constructed with a given seed will generate the same sequence of values as all other generators constructed with the same seed. The seed may be any length.

Function seeded_xorshift

fn seeded_xorshift(x: u32, y: u32, z: u32, w: u32) -> Rng

Function task_rng

fn task_rng() -> 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().

Function tls_rng_state

fn tls_rng_state(_v: @RandRes)

Function xorshift

fn xorshift() -> Rng