core: Rework some rand functions to be more consistent

Changed the gen_x_from functions to gen_x_range and made them use the range
[start, end) like other core functions.
This commit is contained in:
Brian Anderson 2012-05-21 18:57:28 -07:00
parent d18da37c9c
commit 2bec7eafb1

View File

@ -30,11 +30,11 @@ impl extensions for rng {
self.gen_i64() as int self.gen_i64() as int
} }
#[doc = "Return an int randomly chosen from the range [start, end], \ #[doc = "Return an int randomly chosen from the range [start, end), \
failing if start > end"] failing if start >= end"]
fn gen_int_from(start: int, end: int) -> int { fn gen_int_range(start: int, end: int) -> int {
assert start <= end; assert start < end;
start + int::abs(self.gen_int() % (end - start + 1)) start + int::abs(self.gen_int() % (end - start))
} }
#[doc = "Return a random i8"] #[doc = "Return a random i8"]
@ -62,11 +62,11 @@ impl extensions for rng {
self.gen_u64() as uint self.gen_u64() as uint
} }
#[doc = "Return a uint randomly chosen from the range [start, end], \ #[doc = "Return a uint randomly chosen from the range [start, end), \
failing if start > end"] failing if start >= end"]
fn gen_uint_from(start: uint, end: uint) -> uint { fn gen_uint_range(start: uint, end: uint) -> uint {
assert start <= end; assert start < end;
start + (self.gen_uint() % (end - start + 1u)) start + (self.gen_uint() % (end - start))
} }
#[doc = "Return a random u8"] #[doc = "Return a random u8"]
@ -130,7 +130,7 @@ impl extensions for rng {
if n == 0u { if n == 0u {
true true
} else { } else {
self.gen_uint_from(1u, n) == 1u self.gen_uint_range(1u, n + 1u) == 1u
} }
} }
@ -166,7 +166,7 @@ impl extensions for rng {
if values.is_empty() { if values.is_empty() {
none none
} else { } else {
some(values[self.gen_uint_from(0u, values.len() - 1u)]) some(values[self.gen_uint_range(0u, values.len())])
} }
} }
@ -186,7 +186,7 @@ impl extensions for rng {
if total == 0u { if total == 0u {
ret none; ret none;
} }
let chosen = self.gen_uint_from(0u, total - 1u); let chosen = self.gen_uint_range(0u, total);
let mut so_far = 0u; let mut so_far = 0u;
for v.each {|item| for v.each {|item|
so_far += item.weight; so_far += item.weight;
@ -223,7 +223,7 @@ impl extensions for rng {
// invariant: elements with index >= i have been locked in place. // invariant: elements with index >= i have been locked in place.
i -= 1u; i -= 1u;
// lock element i in place. // lock element i in place.
vec::swap(values, i, self.gen_uint_from(0u, i)); vec::swap(values, i, self.gen_uint_range(0u, i + 1u));
} }
} }
@ -274,35 +274,35 @@ mod tests {
} }
#[test] #[test]
fn gen_int_from() { fn gen_int_range() {
let r = rand::rng(); let r = rand::rng();
let a = r.gen_int_from(-3, 42); let a = r.gen_int_range(-3, 42);
assert a >= -3 && a <= 42; assert a >= -3 && a < 42;
assert r.gen_int_from(0, 0) == 0; assert r.gen_int_range(0, 1) == 0;
assert r.gen_int_from(-12, -12) == -12; assert r.gen_int_range(-12, -11) == -12;
} }
#[test] #[test]
#[should_fail] #[should_fail]
#[ignore(cfg(target_os = "win3"))] #[ignore(cfg(target_os = "win3"))]
fn gen_int_from_fail() { fn gen_int_from_fail() {
rand::rng().gen_int_from(5, -2); rand::rng().gen_int_range(5, -2);
} }
#[test] #[test]
fn gen_uint_from() { fn gen_uint_range() {
let r = rand::rng(); let r = rand::rng();
let a = r.gen_uint_from(3u, 42u); let a = r.gen_uint_range(3u, 42u);
assert a >= 3u && a <= 42u; assert a >= 3u && a < 42u;
assert r.gen_uint_from(0u, 0u) == 0u; assert r.gen_uint_range(0u, 1u) == 0u;
assert r.gen_uint_from(12u, 12u) == 12u; assert r.gen_uint_range(12u, 13u) == 12u;
} }
#[test] #[test]
#[should_fail] #[should_fail]
#[ignore(cfg(target_os = "win3"))] #[ignore(cfg(target_os = "win3"))]
fn gen_uint_from_fail() { fn gen_uint_range_fail() {
rand::rng().gen_uint_from(5u, 2u); rand::rng().gen_uint_range(5u, 2u);
} }
#[test] #[test]