From aa763cdb238bb9c9f26d84a699d9f0de8289d8c4 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 21 Apr 2013 22:09:33 +1000 Subject: [PATCH] libcore: make rand::random return a generic value implementing `Rand`. --- src/libcore/rand.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/libcore/rand.rs b/src/libcore/rand.rs index 0a93a651a85..a2f103fdbc9 100644 --- a/src/libcore/rand.rs +++ b/src/libcore/rand.rs @@ -55,6 +55,12 @@ fn rand(rng: @rand::Rng) -> 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() @@ -149,6 +155,7 @@ pub struct Weighted { } pub trait RngUtil { + /// Return a random value for a Rand type fn gen(&self) -> T; /** * Return a random int @@ -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 { + task_rng().gen() } @@ -915,8 +923,10 @@ fn task_rng() { #[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> = rand::random(); } }