Random number generation

Type weighted

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

A value with a particular weight compared to other values

Interface rng

A random number generator

Method next

fn next() -> u32

Return the next random integer

Implementation extensions 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

Method gen_f32

fn gen_f32() -> f32

Return a random f32

Method gen_f64

fn gen_f64() -> f64

Return a random f64

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 rng of rng for @rand_res

Method next

fn next() -> u32

Implementation rng of rng for xorshift_state

Method next

fn next() -> u32

Function rng

fn rng() -> rng

Create a random number generator with a system specified seed

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 xorshift

fn xorshift() -> rng