2012-03-15 20:58:14 -05:00
|
|
|
#[doc = "Random number generation"];
|
|
|
|
|
2012-05-20 08:06:54 -05:00
|
|
|
export rng, seed, seeded_rng, weighted, extensions;
|
2012-02-01 04:45:23 -06:00
|
|
|
|
|
|
|
enum rctx {}
|
|
|
|
|
2011-11-16 22:49:38 -06:00
|
|
|
#[abi = "cdecl"]
|
|
|
|
native mod rustrt {
|
2012-05-20 08:06:54 -05:00
|
|
|
fn rand_seed() -> [u8];
|
2012-02-01 04:45:23 -06:00
|
|
|
fn rand_new() -> *rctx;
|
2012-05-20 08:06:54 -05:00
|
|
|
fn rand_new_seeded(seed: [u8]) -> *rctx;
|
2012-02-01 04:45:23 -06:00
|
|
|
fn rand_next(c: *rctx) -> u32;
|
|
|
|
fn rand_free(c: *rctx);
|
2010-07-25 23:45:09 -05:00
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "A random number generator"]
|
2012-01-11 05:54:39 -06:00
|
|
|
iface rng {
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "Return the next random integer"]
|
2011-10-26 18:24:31 -05:00
|
|
|
fn next() -> u32;
|
2012-05-17 13:52:49 -05:00
|
|
|
}
|
|
|
|
|
2012-05-19 13:25:45 -05:00
|
|
|
#[doc = "A value with a particular weight compared to other values"]
|
|
|
|
type weighted<T> = { weight: uint, item: T };
|
|
|
|
|
2012-05-17 13:52:49 -05:00
|
|
|
#[doc = "Extension methods for random number generators"]
|
|
|
|
impl extensions for rng {
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-05-19 13:25:45 -05:00
|
|
|
#[doc = "Return a random int"]
|
|
|
|
fn gen_int() -> int {
|
|
|
|
self.gen_i64() as int
|
|
|
|
}
|
|
|
|
|
2012-05-21 20:57:28 -05:00
|
|
|
#[doc = "Return an int randomly chosen from the range [start, end), \
|
|
|
|
failing if start >= end"]
|
|
|
|
fn gen_int_range(start: int, end: int) -> int {
|
|
|
|
assert start < end;
|
|
|
|
start + int::abs(self.gen_int() % (end - start))
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random i8"]
|
|
|
|
fn gen_i8() -> i8 {
|
|
|
|
self.next() as i8
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random i16"]
|
|
|
|
fn gen_i16() -> i16 {
|
|
|
|
self.next() as i16
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random i32"]
|
|
|
|
fn gen_i32() -> i32 {
|
|
|
|
self.next() as i32
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random i64"]
|
|
|
|
fn gen_i64() -> i64 {
|
|
|
|
(self.next() as i64 << 32) | self.next() as i64
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random uint"]
|
|
|
|
fn gen_uint() -> uint {
|
2012-05-21 19:25:33 -05:00
|
|
|
self.gen_u64() as uint
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
2012-05-21 20:57:28 -05:00
|
|
|
#[doc = "Return a uint randomly chosen from the range [start, end), \
|
|
|
|
failing if start >= end"]
|
|
|
|
fn gen_uint_range(start: uint, end: uint) -> uint {
|
|
|
|
assert start < end;
|
|
|
|
start + (self.gen_uint() % (end - start))
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random u8"]
|
|
|
|
fn gen_u8() -> u8 {
|
|
|
|
self.next() as u8
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random u16"]
|
|
|
|
fn gen_u16() -> u16 {
|
|
|
|
self.next() as u16
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random u32"]
|
|
|
|
fn gen_u32() -> u32 {
|
|
|
|
self.next()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random u64"]
|
|
|
|
fn gen_u64() -> u64 {
|
|
|
|
(self.next() as u64 << 32) | self.next() as u64
|
|
|
|
}
|
|
|
|
|
2012-05-17 13:52:49 -05:00
|
|
|
#[doc = "Return a random float"]
|
|
|
|
fn gen_float() -> float {
|
2012-05-19 13:25:45 -05:00
|
|
|
self.gen_f64() as float
|
2012-05-17 13:52:49 -05:00
|
|
|
}
|
2011-11-08 22:35:15 -06:00
|
|
|
|
2012-05-19 13:25:45 -05:00
|
|
|
#[doc = "Return a random f32"]
|
|
|
|
fn gen_f32() -> f32 {
|
|
|
|
self.gen_f64() as f32
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random f64"]
|
|
|
|
fn gen_f64() -> f64 {
|
|
|
|
let u1 = self.next() as f64;
|
|
|
|
let u2 = self.next() as f64;
|
|
|
|
let u3 = self.next() as f64;
|
|
|
|
let scale = u32::max_value as f64;
|
|
|
|
ret ((u1 / scale + u2) / scale + u3) / scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random char"]
|
|
|
|
fn gen_char() -> char {
|
|
|
|
self.next() as char
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a char randomly chosen from chars, failing if chars is \
|
|
|
|
empty"]
|
|
|
|
fn gen_char_from(chars: str) -> char {
|
|
|
|
assert !chars.is_empty();
|
|
|
|
self.choose(str::chars(chars))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random bool"]
|
|
|
|
fn gen_bool() -> bool {
|
|
|
|
self.next() & 1u32 == 1u32
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a bool with a 1 in n chance of true"]
|
|
|
|
fn gen_weighted_bool(n: uint) -> bool {
|
|
|
|
if n == 0u {
|
|
|
|
true
|
|
|
|
} else {
|
2012-05-21 20:57:28 -05:00
|
|
|
self.gen_uint_range(1u, n + 1u) == 1u
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a random string of the specified length composed of A-Z, \
|
|
|
|
a-z, 0-9"]
|
2012-05-17 13:52:49 -05:00
|
|
|
fn gen_str(len: uint) -> str {
|
|
|
|
let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
|
|
|
"abcdefghijklmnopqrstuvwxyz" +
|
|
|
|
"0123456789";
|
|
|
|
let mut s = "";
|
|
|
|
let mut i = 0u;
|
|
|
|
while (i < len) {
|
2012-05-19 13:25:45 -05:00
|
|
|
s = s + str::from_char(self.gen_char_from(charset));
|
2012-05-17 13:52:49 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
2011-12-26 21:31:25 -06:00
|
|
|
|
2012-05-19 13:25:45 -05:00
|
|
|
#[doc = "Return a random byte string of the specified length"]
|
2012-05-17 13:52:49 -05:00
|
|
|
fn gen_bytes(len: uint) -> [u8] {
|
2012-05-19 13:25:45 -05:00
|
|
|
vec::from_fn(len) {|_i|
|
|
|
|
self.gen_u8()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Choose an item randomly, failing if values is empty"]
|
|
|
|
fn choose<T:copy>(values: [T]) -> T {
|
|
|
|
self.choose_option(values).get()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Choose some(item) randomly, returning none if values is empty"]
|
|
|
|
fn choose_option<T:copy>(values: [T]) -> option<T> {
|
|
|
|
if values.is_empty() {
|
|
|
|
none
|
|
|
|
} else {
|
2012-05-21 20:57:28 -05:00
|
|
|
some(values[self.gen_uint_range(0u, values.len())])
|
2012-05-17 13:52:49 -05:00
|
|
|
}
|
|
|
|
}
|
2012-05-19 13:25:45 -05:00
|
|
|
|
|
|
|
#[doc = "Choose an item respecting the relative weights, failing if \
|
|
|
|
the sum of the weights is 0"]
|
|
|
|
fn choose_weighted<T: copy>(v : [weighted<T>]) -> T {
|
|
|
|
self.choose_weighted_option(v).get()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Choose some(item) respecting the relative weights, returning \
|
|
|
|
none if the sum of the weights is 0"]
|
|
|
|
fn choose_weighted_option<T:copy>(v: [weighted<T>]) -> option<T> {
|
|
|
|
let mut total = 0u;
|
|
|
|
for v.each {|item|
|
|
|
|
total += item.weight;
|
|
|
|
}
|
|
|
|
if total == 0u {
|
|
|
|
ret none;
|
|
|
|
}
|
2012-05-21 20:57:28 -05:00
|
|
|
let chosen = self.gen_uint_range(0u, total);
|
2012-05-19 13:25:45 -05:00
|
|
|
let mut so_far = 0u;
|
|
|
|
for v.each {|item|
|
|
|
|
so_far += item.weight;
|
|
|
|
if so_far > chosen {
|
|
|
|
ret some(item.item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return a vec containing copies of the items, in order, where \
|
|
|
|
the weight of the item determines how many copies there are"]
|
|
|
|
fn weighted_vec<T:copy>(v: [weighted<T>]) -> [T] {
|
|
|
|
let mut r = [];
|
|
|
|
for v.each {|item|
|
|
|
|
uint::range(0u, item.weight) {|_i|
|
|
|
|
r += [item.item];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Shuffle a vec"]
|
|
|
|
fn shuffle<T:copy>(values: [T]) -> [T] {
|
|
|
|
let mut m = vec::to_mut(values);
|
|
|
|
self.shuffle_mut(m);
|
|
|
|
ret vec::from_mut(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Shuffle a mutable vec in place"]
|
|
|
|
fn shuffle_mut<T>(&values: [mut T]) {
|
|
|
|
let mut i = values.len();
|
|
|
|
while i >= 2u {
|
|
|
|
// invariant: elements with index >= i have been locked in place.
|
|
|
|
i -= 1u;
|
|
|
|
// lock element i in place.
|
2012-05-21 20:57:28 -05:00
|
|
|
vec::swap(values, i, self.gen_uint_range(0u, i + 1u));
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-11 05:54:39 -06:00
|
|
|
}
|
2011-07-29 06:31:44 -05:00
|
|
|
|
2012-05-20 08:06:54 -05:00
|
|
|
resource rand_res(c: *rctx) { rustrt::rand_free(c); }
|
2012-05-17 13:52:49 -05:00
|
|
|
|
2012-05-20 08:06:54 -05:00
|
|
|
impl of rng for @rand_res {
|
|
|
|
fn next() -> u32 { ret rustrt::rand_next(**self); }
|
|
|
|
}
|
2012-05-17 13:52:49 -05:00
|
|
|
|
2012-05-20 08:06:54 -05:00
|
|
|
#[doc = "Create a new random seed for seeded_rng"]
|
|
|
|
fn seed() -> [u8] {
|
|
|
|
rustrt::rand_seed()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Create a random number generator with a system specified seed"]
|
|
|
|
fn rng() -> rng {
|
2012-01-11 05:54:39 -06:00
|
|
|
@rand_res(rustrt::rand_new()) as rng
|
2010-07-25 23:45:09 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-05-20 08:06:54 -05:00
|
|
|
#[doc = "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."]
|
|
|
|
fn seeded_rng(seed: [u8]) -> rng {
|
|
|
|
@rand_res(rustrt::rand_new_seeded(seed)) as rng
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
2012-05-20 08:06:54 -05:00
|
|
|
#[test]
|
|
|
|
fn rng_seeded() {
|
|
|
|
let seed = rand::seed();
|
|
|
|
let ra = rand::seeded_rng(seed);
|
|
|
|
let rb = rand::seeded_rng(seed);
|
|
|
|
assert ra.gen_str(100u) == rb.gen_str(100u);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rng_seeded_custom_seed() {
|
|
|
|
// much shorter than generated seeds which are 1024 bytes
|
|
|
|
let seed = [2u8, 32u8, 4u8, 32u8, 51u8];
|
|
|
|
let ra = rand::seeded_rng(seed);
|
|
|
|
let rb = rand::seeded_rng(seed);
|
|
|
|
assert ra.gen_str(100u) == rb.gen_str(100u);
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
2012-05-21 20:57:28 -05:00
|
|
|
fn gen_int_range() {
|
2012-05-19 13:25:45 -05:00
|
|
|
let r = rand::rng();
|
2012-05-21 20:57:28 -05:00
|
|
|
let a = r.gen_int_range(-3, 42);
|
|
|
|
assert a >= -3 && a < 42;
|
|
|
|
assert r.gen_int_range(0, 1) == 0;
|
|
|
|
assert r.gen_int_range(-12, -11) == -12;
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
2012-05-21 23:30:26 -05:00
|
|
|
#[ignore(cfg(target_os = "win32"))]
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_int_from_fail() {
|
2012-05-21 20:57:28 -05:00
|
|
|
rand::rng().gen_int_range(5, -2);
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-05-21 20:57:28 -05:00
|
|
|
fn gen_uint_range() {
|
2012-05-19 13:25:45 -05:00
|
|
|
let r = rand::rng();
|
2012-05-21 20:57:28 -05:00
|
|
|
let a = r.gen_uint_range(3u, 42u);
|
|
|
|
assert a >= 3u && a < 42u;
|
|
|
|
assert r.gen_uint_range(0u, 1u) == 0u;
|
|
|
|
assert r.gen_uint_range(12u, 13u) == 12u;
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
2012-05-21 23:30:26 -05:00
|
|
|
#[ignore(cfg(target_os = "win32"))]
|
2012-05-21 20:57:28 -05:00
|
|
|
fn gen_uint_range_fail() {
|
|
|
|
rand::rng().gen_uint_range(5u, 2u);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-05-17 13:52:49 -05:00
|
|
|
fn gen_float() {
|
|
|
|
let r = rand::rng();
|
|
|
|
let a = r.gen_float();
|
|
|
|
let b = r.gen_float();
|
|
|
|
log(debug, (a, b));
|
|
|
|
}
|
|
|
|
|
2012-05-19 13:25:45 -05:00
|
|
|
#[test]
|
|
|
|
fn gen_weighted_bool() {
|
|
|
|
let r = rand::rng();
|
|
|
|
assert r.gen_weighted_bool(0u) == true;
|
|
|
|
assert r.gen_weighted_bool(1u) == true;
|
|
|
|
}
|
|
|
|
|
2012-05-17 13:52:49 -05:00
|
|
|
#[test]
|
|
|
|
fn gen_str() {
|
|
|
|
let r = rand::rng();
|
2012-01-17 21:05:07 -06:00
|
|
|
log(debug, r.gen_str(10u));
|
|
|
|
log(debug, r.gen_str(10u));
|
|
|
|
log(debug, r.gen_str(10u));
|
2012-05-17 13:52:49 -05:00
|
|
|
assert r.gen_str(0u).len() == 0u;
|
|
|
|
assert r.gen_str(10u).len() == 10u;
|
|
|
|
assert r.gen_str(16u).len() == 16u;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn gen_bytes() {
|
|
|
|
let r = rand::rng();
|
|
|
|
assert r.gen_bytes(0u).len() == 0u;
|
|
|
|
assert r.gen_bytes(10u).len() == 10u;
|
|
|
|
assert r.gen_bytes(16u).len() == 16u;
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-05-19 13:25:45 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn choose() {
|
|
|
|
let r = rand::rng();
|
|
|
|
assert r.choose([1, 1, 1]) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn choose_option() {
|
|
|
|
let r = rand::rng();
|
|
|
|
assert r.choose_option([]) == none::<int>;
|
|
|
|
assert r.choose_option([1, 1, 1]) == some(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn choose_weighted() {
|
|
|
|
let r = rand::rng();
|
|
|
|
assert r.choose_weighted([{weight: 1u, item: 42}]) == 42;
|
|
|
|
assert r.choose_weighted([
|
|
|
|
{weight: 0u, item: 42},
|
|
|
|
{weight: 1u, item: 43}
|
|
|
|
]) == 43;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn choose_weighted_option() {
|
|
|
|
let r = rand::rng();
|
|
|
|
assert r.choose_weighted_option([{weight: 1u, item: 42}]) == some(42);
|
|
|
|
assert r.choose_weighted_option([
|
|
|
|
{weight: 0u, item: 42},
|
|
|
|
{weight: 1u, item: 43}
|
|
|
|
]) == some(43);
|
|
|
|
assert r.choose_weighted_option([]) == none::<int>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn weighted_vec() {
|
|
|
|
let r = rand::rng();
|
|
|
|
let empty: [int] = [];
|
|
|
|
assert r.weighted_vec([]) == empty;
|
|
|
|
assert r.weighted_vec([
|
|
|
|
{weight: 0u, item: 3u},
|
|
|
|
{weight: 1u, item: 2u},
|
|
|
|
{weight: 2u, item: 1u}
|
|
|
|
]) == [2u, 1u, 1u];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shuffle() {
|
|
|
|
let r = rand::rng();
|
|
|
|
let empty: [int] = [];
|
|
|
|
assert r.shuffle([]) == empty;
|
|
|
|
assert r.shuffle([1, 1, 1]) == [1, 1, 1];
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-22 17:44:13 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|