diff --git a/Cargo.toml b/Cargo.toml index e01be354047..e75b9e30f91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/rust-lang-nursery/rand" documentation = "https://docs.rs/rand" -homepage = "https://github.com/rust-lang-nursery/rand" +homepage = "https://crates.io/crates/rand" description = """ Random number generators and other randomness functionality. """ diff --git a/benches/distributions.rs b/benches/distributions.rs index 0d756126370..7032b3561f0 100644 --- a/benches/distributions.rs +++ b/benches/distributions.rs @@ -12,6 +12,46 @@ use test::{black_box, Bencher}; use rand::{Rng, NewRng, XorShiftRng}; use rand::distributions::*; +macro_rules! distr_int { + ($fnn:ident, $ty:ty, $distr:expr) => { + #[bench] + fn $fnn(b: &mut Bencher) { + let mut rng = XorShiftRng::new(); + let distr = $distr; + + b.iter(|| { + let mut accum = 0 as $ty; + for _ in 0..::RAND_BENCH_N { + let x: $ty = distr.sample(&mut rng); + accum = accum.wrapping_add(x); + } + black_box(accum); + }); + b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N; + } + } +} + +macro_rules! distr_float { + ($fnn:ident, $ty:ty, $distr:expr) => { + #[bench] + fn $fnn(b: &mut Bencher) { + let mut rng = XorShiftRng::new(); + let distr = $distr; + + b.iter(|| { + let mut accum = 0.0; + for _ in 0..::RAND_BENCH_N { + let x: $ty = distr.sample(&mut rng); + accum = accum + x; + } + black_box(accum); + }); + b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N; + } + } +} + macro_rules! distr { ($fnn:ident, $ty:ty, $distr:expr) => { #[bench] @@ -31,62 +71,64 @@ macro_rules! distr { } // range -distr!(distr_range_i8, i8, Range::new(20i8, 100)); -distr!(distr_range_i16, i16, Range::new(-500i16, 2000)); -distr!(distr_range_i32, i32, Range::new(-200_000_000i32, 800_000_000)); -distr!(distr_range_i64, i64, Range::new(3i64, 12345678901234)); +distr_int!(distr_range_i8, i8, Range::new(20i8, 100)); +distr_int!(distr_range_i16, i16, Range::new(-500i16, 2000)); +distr_int!(distr_range_i32, i32, Range::new(-200_000_000i32, 800_000_000)); +distr_int!(distr_range_i64, i64, Range::new(3i64, 12345678901234)); #[cfg(feature = "i128_support")] -distr!(distr_range_i128, i128, Range::new(-12345678901234i128, 12345678901234567890)); +distr_int!(distr_range_i128, i128, Range::new(-12345678901234i128, 12345678901234567890)); -distr!(distr_range_f32, f32, Range::new(2.26f32, 2.319)); -distr!(distr_range_f64, f64, Range::new(2.26f64, 2.319)); +distr_float!(distr_range_f32, f32, Range::new(2.26f32, 2.319)); +distr_float!(distr_range_f64, f64, Range::new(2.26f64, 2.319)); // uniform -distr!(distr_uniform_i8, i8, Uniform); -distr!(distr_uniform_i16, i16, Uniform); -distr!(distr_uniform_i32, i32, Uniform); -distr!(distr_uniform_i64, i64, Uniform); +distr_int!(distr_uniform_i8, i8, Uniform); +distr_int!(distr_uniform_i16, i16, Uniform); +distr_int!(distr_uniform_i32, i32, Uniform); +distr_int!(distr_uniform_i64, i64, Uniform); #[cfg(feature = "i128_support")] -distr!(distr_uniform_i128, i128, Uniform); +distr_int!(distr_uniform_i128, i128, Uniform); distr!(distr_uniform_bool, bool, Uniform); distr!(distr_uniform_alphanumeric, char, Alphanumeric); distr!(distr_uniform_codepoint, char, Uniform); -distr!(distr_uniform_f32, f32, Uniform); -distr!(distr_uniform_f64, f64, Uniform); +distr_float!(distr_uniform_f32, f32, Uniform); +distr_float!(distr_uniform_f64, f64, Uniform); // distributions -distr!(distr_exp, f64, Exp::new(2.71828 * 3.14159)); -distr!(distr_normal, f64, Normal::new(-2.71828, 3.14159)); -distr!(distr_log_normal, f64, LogNormal::new(-2.71828, 3.14159)); -distr!(distr_gamma_large_shape, f64, Gamma::new(10., 1.0)); -distr!(distr_gamma_small_shape, f64, Gamma::new(0.1, 1.0)); -distr!(distr_binomial, u64, Binomial::new(20, 0.7)); -distr!(distr_poisson, u64, Poisson::new(4.0)); +distr_float!(distr_exp, f64, Exp::new(2.71828 * 3.14159)); +distr_float!(distr_normal, f64, Normal::new(-2.71828, 3.14159)); +distr_float!(distr_log_normal, f64, LogNormal::new(-2.71828, 3.14159)); +distr_float!(distr_gamma_large_shape, f64, Gamma::new(10., 1.0)); +distr_float!(distr_gamma_small_shape, f64, Gamma::new(0.1, 1.0)); +distr_int!(distr_binomial, u64, Binomial::new(20, 0.7)); +distr_int!(distr_poisson, u64, Poisson::new(4.0)); // construct and sample from a range macro_rules! gen_range_int { - ($fnn:ident, $ty:ty, $low:expr, $high:expr) => { + ($fnn:ident, $ty:ident, $low:expr, $high:expr) => { #[bench] fn $fnn(b: &mut Bencher) { let mut rng = XorShiftRng::new(); - let high = $high; b.iter(|| { + let mut high = $high; + let mut accum: $ty = 0; for _ in 0..::RAND_BENCH_N { - let x: $ty = rng.gen_range($low, high); - black_box(x); - black_box(high); + accum = accum.wrapping_add(rng.gen_range($low, high)); + // force recalculation of range each time + high = high.wrapping_add(1) & std::$ty::MAX; } + black_box(accum); }); b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N; } } } -gen_range_int!(gen_range_i8, i8, 20i8, 100); +gen_range_int!(gen_range_i8, i8, -20i8, 100); gen_range_int!(gen_range_i16, i16, -500i16, 2000); gen_range_int!(gen_range_i32, i32, -200_000_000i32, 800_000_000); gen_range_int!(gen_range_i64, i64, 3i64, 12345678901234); diff --git a/benches/generators.rs b/benches/generators.rs index 2d96b8719cb..525831376e6 100644 --- a/benches/generators.rs +++ b/benches/generators.rs @@ -45,9 +45,11 @@ macro_rules! gen_uint { fn $fnn(b: &mut Bencher) { let mut rng = $gen; b.iter(|| { + let mut accum: $ty = 0; for _ in 0..RAND_BENCH_N { - black_box(rng.gen::<$ty>()); + accum = accum.wrapping_add(rng.gen::<$ty>()); } + black_box(accum); }); b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N; } @@ -126,9 +128,11 @@ macro_rules! chacha_rounds { let mut rng = ChaChaRng::new(); rng.set_rounds($rounds); b.iter(|| { + let mut accum: u32 = 0; for _ in 0..RAND_BENCH_N { - black_box(rng.gen::()); + accum = accum.wrapping_add(rng.gen::()); } + black_box(accum); }); b.bytes = size_of::() as u64 * RAND_BENCH_N; } @@ -138,9 +142,11 @@ macro_rules! chacha_rounds { let mut rng = ChaChaRng::new(); rng.set_rounds($rounds); b.iter(|| { + let mut accum: u64 = 0; for _ in 0..RAND_BENCH_N { - black_box(rng.gen::()); + accum = accum.wrapping_add(rng.gen::()); } + black_box(accum); }); b.bytes = size_of::() as u64 * RAND_BENCH_N; } @@ -178,9 +184,11 @@ macro_rules! reseeding_uint { RESEEDING_THRESHOLD, EntropyRng::new()); b.iter(|| { + let mut accum: $ty = 0; for _ in 0..RAND_BENCH_N { - black_box(rng.gen::<$ty>()); + accum = accum.wrapping_add(rng.gen::<$ty>()); } + black_box(accum); }); b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N; } @@ -197,9 +205,11 @@ macro_rules! threadrng_uint { fn $fnn(b: &mut Bencher) { let mut rng = thread_rng(); b.iter(|| { + let mut accum: $ty = 0; for _ in 0..RAND_BENCH_N { - black_box(rng.gen::<$ty>()); + accum = accum.wrapping_add(rng.gen::<$ty>()); } + black_box(accum); }); b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N; } diff --git a/rand-core/Cargo.toml b/rand-core/Cargo.toml index 4577b4ea7ea..ba748f0a11e 100644 --- a/rand-core/Cargo.toml +++ b/rand-core/Cargo.toml @@ -5,8 +5,8 @@ authors = ["The Rust Project Developers"] license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/rust-lang-nursery/rand" -documentation = "https://docs.rs/rand" -homepage = "https://github.com/rust-lang-nursery/rand" +documentation = "https://docs.rs/rand-core" +homepage = "https://crates.io/crates/rand-core" description = """ Core random number generator traits and tools for implementation. """ diff --git a/rand-core/README.md b/rand-core/README.md index e7d67b7e14d..20823d1f37f 100644 --- a/rand-core/README.md +++ b/rand-core/README.md @@ -16,7 +16,7 @@ prefer to use the main [rand] crate. [Documentation](https://docs.rs/rand-core) -[rand]: .. +[rand]: ../README.md # License