about summary refs log tree commit diff
path: root/src/libstd/rand
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstd/rand')
-rw-r--r--src/libstd/rand/rand_impls.rs15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/libstd/rand/rand_impls.rs b/src/libstd/rand/rand_impls.rs
index 22980a3e1c4..976eea7191a 100644
--- a/src/libstd/rand/rand_impls.rs
+++ b/src/libstd/rand/rand_impls.rs
@@ -14,7 +14,6 @@ use char;
 use int;
 use option::{Option, Some, None};
 use rand::{Rand,Rng};
-use u32;
 use uint;
 
 impl Rand for int {
@@ -96,21 +95,23 @@ impl Rand for u64 {
 }
 
 impl Rand for f32 {
+    /// A random `f32` in the range `[0, 1)`.
     #[inline]
     fn rand<R: Rng>(rng: &mut R) -> f32 {
-        rng.gen::<f64>() as f32
+        // weird, but this is the easiest way to get 2**32
+        static SCALE: f32 = 2.0 * (1u32 << 31) as f32;
+        rng.next_u32() as f32 / SCALE
     }
 }
 
-static SCALE : f64 = (u32::max_value as f64) + 1.0f64;
 impl Rand for f64 {
+    /// A random `f64` in the range `[0, 1)`.
     #[inline]
     fn rand<R: Rng>(rng: &mut R) -> f64 {
-        let u1 = rng.next_u32() as f64;
-        let u2 = rng.next_u32() as f64;
-        let u3 = rng.next_u32() as f64;
+        // weird, but this is the easiest way to get 2**64
+        static SCALE: f64 = 2.0 * (1u64 << 63) as f64;
 
-        ((u1 / SCALE + u2) / SCALE + u3) / SCALE
+        rng.next_u64() as f64 / SCALE
     }
 }