pub trait Rng { fn next(&mut self) -> u32; fn gen<T: Rand>(&mut self) -> T { ... } fn gen_vec<T: Rand>(&mut self, len: uint) -> ~[T] { ... } fn gen_integer_range<T: Rand + Int>(&mut self, low: T, high: T) -> T { ... } fn gen_weighted_bool(&mut self, n: uint) -> bool { ... } fn gen_ascii_str(&mut self, len: uint) -> ~str { ... } fn choose<T: Clone>(&mut self, values: &[T]) -> T { ... } fn choose_option<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T> { ... } fn choose_weighted<T: Clone>(&mut self, v: &[Weighted<T>]) -> T { ... } fn choose_weighted_option<T: Clone>(&mut self, v: &[Weighted<T>]) -> Option<T> { ... } fn weighted_vec<T: Clone>(&mut self, v: &[Weighted<T>]) -> ~[T] { ... } fn shuffle<T>(&mut self, values: ~[T]) -> ~[T] { ... } fn shuffle_mut<T>(&mut self, values: &mut [T]) { ... } fn sample<A, T: Iterator<A>>(&mut self, iter: T, n: uint) -> ~[A] { ... } }
A random number generator
fn next(&mut self) -> u32
Return the next random integer
fn gen<T: Rand>(&mut self) -> T
Return a random value of a Rand type.
use std::rand;
fn main() {
let rng = rand::task_rng();
let x: uint = rng.gen();
printfln!(x);
printfln!(rng.gen::<(float, bool)>());
}
fn gen_vec<T: Rand>(&mut self, len: uint) -> ~[T]
Return a random vector of the specified length.
use std::rand;
fn main() {
let rng = rand::task_rng();
let x: ~[uint] = rng.gen_vec(10);
printfln!(x);
printfln!(rng.gen_vec::<(float, bool)>(5));
}
fn gen_integer_range<T: Rand + Int>(&mut self, low: T, high: T) -> T
Generate a random primitive integer in the range [low
,
high
). Fails if low >= high
.
This gives a uniform distribution (assuming this RNG is itself
uniform), even for edge cases like gen_integer_range(0u8,
170)
, which a naive modulo operation would return numbers
less than 85 with double the probability to those greater than
85.
use std::rand;
fn main() {
let rng = rand::task_rng();
let n: uint = rng.gen_integer_range(0u, 10);
printfln!(n);
let m: i16 = rng.gen_integer_range(-40, 400);
printfln!(m);
}
fn gen_weighted_bool(&mut self, n: uint) -> bool
Return a bool with a 1 in n chance of true
use std::rand;
use std::rand::Rng;
fn main() {
let mut rng = rand::rng();
printfln!("%b", rng.gen_weighted_bool(3));
}
fn gen_ascii_str(&mut self, len: uint) -> ~str
Return a random string of the specified length composed of A-Z,a-z,0-9.
use std::rand;
fn main() {
println(rand::task_rng().gen_ascii_str(10));
}
fn choose<T: Clone>(&mut self, values: &[T]) -> T
Choose an item randomly, failing if values
is empty.
fn choose_option<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T>
Choose Some(&item)
randomly, returning None
if values is
empty.
use std::rand;
fn main() {
printfln!(rand::task_rng().choose_option([1,2,4,8,16,32]));
printfln!(rand::task_rng().choose_option([]));
}
fn choose_weighted<T: Clone>(&mut self, v: &[Weighted<T>]) -> T
Choose an item respecting the relative weights, failing if the sum of the weights is 0
use std::rand;
use std::rand::Rng;
fn main() {
let mut rng = rand::rng();
let x = [rand::Weighted {weight: 4, item: 'a'},
rand::Weighted {weight: 2, item: 'b'},
rand::Weighted {weight: 2, item: 'c'}];
printfln!("%c", rng.choose_weighted(x));
}
fn choose_weighted_option<T: Clone>(&mut self, v: &[Weighted<T>]) -> Option<T>
Choose Some(item) respecting the relative weights, returning none if the sum of the weights is 0
use std::rand;
use std::rand::Rng;
fn main() {
let mut rng = rand::rng();
let x = [rand::Weighted {weight: 4, item: 'a'},
rand::Weighted {weight: 2, item: 'b'},
rand::Weighted {weight: 2, item: 'c'}];
printfln!(rng.choose_weighted_option(x));
}
fn weighted_vec<T: Clone>(&mut self, 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
use std::rand;
use std::rand::Rng;
fn main() {
let mut rng = rand::rng();
let x = [rand::Weighted {weight: 4, item: 'a'},
rand::Weighted {weight: 2, item: 'b'},
rand::Weighted {weight: 2, item: 'c'}];
printfln!(rng.weighted_vec(x));
}
fn shuffle<T>(&mut self, values: ~[T]) -> ~[T]
Shuffle a vec
use std::rand;
fn main() {
printfln!(rand::task_rng().shuffle(~[1,2,3]));
}
fn shuffle_mut<T>(&mut self, values: &mut [T])
Shuffle a mutable vector in place.
use std::rand;
fn main() {
let rng = rand::task_rng();
let mut y = [1,2,3];
rng.shuffle_mut(y);
printfln!(y);
rng.shuffle_mut(y);
printfln!(y);
}
fn sample<A, T: Iterator<A>>(&mut self, iter: T, n: uint) -> ~[A]
Randomly sample up to n
elements from an iterator.
use std::rand;
fn main() {
let rng = rand::task_rng();
let sample = rng.sample(range(1, 100), 5);
printfln!(sample);
}
XorShiftRng
IsaacRng
impl<R: Rng> Rng for @mut R
Prefix searches with a type followed by a colon (e.g.
fn:
) to restrict the search to a given type.
Accepted types are: fn
, mod
,
struct
(or str
), enum
,
trait
, typedef
(or
tdef
).