diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-10-03 13:10:34 +1000 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-10-09 22:22:43 +1100 |
| commit | 9db32a2f1d2cfafc519941475f5e660a9ae076f0 (patch) | |
| tree | baa944042d89b671bbd42a0e0f625afd18b2606e | |
| parent | 98869799eb2604ecd7c947db117794df10890a2c (diff) | |
| download | rust-9db32a2f1d2cfafc519941475f5e660a9ae076f0.tar.gz rust-9db32a2f1d2cfafc519941475f5e660a9ae076f0.zip | |
std::rand: adjust the f32 & f64 Rand instances.
The f32 generator now just uses a single u32, and the f64 uses a single u64. This will make both significantly faster, especially on 64-bit platforms.
| -rw-r--r-- | src/libstd/rand/rand_impls.rs | 15 |
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 } } |
