Skip to content

libcore: make rand::random return a generic value implementing Rand. #5987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/libcore/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ impl Rand for i64 {
}
}

impl Rand for uint {
fn rand(rng: @rand::Rng) -> uint {
rng.gen_uint()
}
}

impl Rand for u8 {
fn rand(rng: @rand::Rng) -> u8 {
rng.gen_u8()
Expand Down Expand Up @@ -149,6 +155,7 @@ pub struct Weighted<T> {
}

pub trait RngUtil {
/// Return a random value for a Rand type
fn gen<T:Rand>(&self) -> T;
/**
* Return a random int
Expand Down Expand Up @@ -739,10 +746,11 @@ pub fn task_rng() -> @Rng {
}

/**
* Returns a random uint, using the task's based random number generator.
* Returns a random value of a Rand type, using the task's random number
* generator.
*/
pub fn random() -> uint {
task_rng().gen_uint()
pub fn random<T: Rand>() -> T {
task_rng().gen()
}


Expand Down Expand Up @@ -915,8 +923,10 @@ mod tests {

#[test]
fn random() {
// not sure how to test this aside from just getting a number
// not sure how to test this aside from just getting some values
let _n : uint = rand::random();
let _f : f32 = rand::random();
let _o : Option<Option<i8>> = rand::random();
}
}

Expand Down