about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-04-21 12:00:52 -0700
committerbors <bors@rust-lang.org>2013-04-21 12:00:52 -0700
commit535244cde4640986f29978c5f9c35fd3b40f58db (patch)
tree1c35429aec82206b626f07ae51fbe638c07935b7 /src
parent8942099614dba3a52e3fc0805ed6c0c37cb371a4 (diff)
parentaa763cdb238bb9c9f26d84a699d9f0de8289d8c4 (diff)
auto merge of #5987 : huonw/rust/generic-random, r=catamorphism
With this patch `rand::random` can be used to generate anything that implements `Rand`.
Diffstat (limited to 'src')
-rw-r--r--src/libcore/rand.rs18
1 files 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 @@ 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()
@@ -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
@@ -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()
 }
 
 
@@ -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();
     }
 }