Skip to content

Commit dbc1f1d

Browse files
authored
Merge pull request #317 from dhardy/doc
homepage, bench improvement and doc fix
2 parents df456c3 + b2ba486 commit dbc1f1d

File tree

5 files changed

+88
-36
lines changed

5 files changed

+88
-36
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ license = "MIT/Apache-2.0"
66
readme = "README.md"
77
repository = "https://github.com/rust-lang-nursery/rand"
88
documentation = "https://docs.rs/rand"
9-
homepage = "https://github.com/rust-lang-nursery/rand"
9+
homepage = "https://crates.io/crates/rand"
1010
description = """
1111
Random number generators and other randomness functionality.
1212
"""

benches/distributions.rs

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,46 @@ use test::{black_box, Bencher};
1212
use rand::{Rng, NewRng, XorShiftRng};
1313
use rand::distributions::*;
1414

15+
macro_rules! distr_int {
16+
($fnn:ident, $ty:ty, $distr:expr) => {
17+
#[bench]
18+
fn $fnn(b: &mut Bencher) {
19+
let mut rng = XorShiftRng::new();
20+
let distr = $distr;
21+
22+
b.iter(|| {
23+
let mut accum = 0 as $ty;
24+
for _ in 0..::RAND_BENCH_N {
25+
let x: $ty = distr.sample(&mut rng);
26+
accum = accum.wrapping_add(x);
27+
}
28+
black_box(accum);
29+
});
30+
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
31+
}
32+
}
33+
}
34+
35+
macro_rules! distr_float {
36+
($fnn:ident, $ty:ty, $distr:expr) => {
37+
#[bench]
38+
fn $fnn(b: &mut Bencher) {
39+
let mut rng = XorShiftRng::new();
40+
let distr = $distr;
41+
42+
b.iter(|| {
43+
let mut accum = 0.0;
44+
for _ in 0..::RAND_BENCH_N {
45+
let x: $ty = distr.sample(&mut rng);
46+
accum = accum + x;
47+
}
48+
black_box(accum);
49+
});
50+
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
51+
}
52+
}
53+
}
54+
1555
macro_rules! distr {
1656
($fnn:ident, $ty:ty, $distr:expr) => {
1757
#[bench]
@@ -31,62 +71,64 @@ macro_rules! distr {
3171
}
3272

3373
// range
34-
distr!(distr_range_i8, i8, Range::new(20i8, 100));
35-
distr!(distr_range_i16, i16, Range::new(-500i16, 2000));
36-
distr!(distr_range_i32, i32, Range::new(-200_000_000i32, 800_000_000));
37-
distr!(distr_range_i64, i64, Range::new(3i64, 12345678901234));
74+
distr_int!(distr_range_i8, i8, Range::new(20i8, 100));
75+
distr_int!(distr_range_i16, i16, Range::new(-500i16, 2000));
76+
distr_int!(distr_range_i32, i32, Range::new(-200_000_000i32, 800_000_000));
77+
distr_int!(distr_range_i64, i64, Range::new(3i64, 12345678901234));
3878
#[cfg(feature = "i128_support")]
39-
distr!(distr_range_i128, i128, Range::new(-12345678901234i128, 12345678901234567890));
79+
distr_int!(distr_range_i128, i128, Range::new(-12345678901234i128, 12345678901234567890));
4080

41-
distr!(distr_range_f32, f32, Range::new(2.26f32, 2.319));
42-
distr!(distr_range_f64, f64, Range::new(2.26f64, 2.319));
81+
distr_float!(distr_range_f32, f32, Range::new(2.26f32, 2.319));
82+
distr_float!(distr_range_f64, f64, Range::new(2.26f64, 2.319));
4383

4484
// uniform
45-
distr!(distr_uniform_i8, i8, Uniform);
46-
distr!(distr_uniform_i16, i16, Uniform);
47-
distr!(distr_uniform_i32, i32, Uniform);
48-
distr!(distr_uniform_i64, i64, Uniform);
85+
distr_int!(distr_uniform_i8, i8, Uniform);
86+
distr_int!(distr_uniform_i16, i16, Uniform);
87+
distr_int!(distr_uniform_i32, i32, Uniform);
88+
distr_int!(distr_uniform_i64, i64, Uniform);
4989
#[cfg(feature = "i128_support")]
50-
distr!(distr_uniform_i128, i128, Uniform);
90+
distr_int!(distr_uniform_i128, i128, Uniform);
5191

5292
distr!(distr_uniform_bool, bool, Uniform);
5393
distr!(distr_uniform_alphanumeric, char, Alphanumeric);
5494
distr!(distr_uniform_codepoint, char, Uniform);
5595

56-
distr!(distr_uniform_f32, f32, Uniform);
57-
distr!(distr_uniform_f64, f64, Uniform);
96+
distr_float!(distr_uniform_f32, f32, Uniform);
97+
distr_float!(distr_uniform_f64, f64, Uniform);
5898

5999
// distributions
60-
distr!(distr_exp, f64, Exp::new(2.71828 * 3.14159));
61-
distr!(distr_normal, f64, Normal::new(-2.71828, 3.14159));
62-
distr!(distr_log_normal, f64, LogNormal::new(-2.71828, 3.14159));
63-
distr!(distr_gamma_large_shape, f64, Gamma::new(10., 1.0));
64-
distr!(distr_gamma_small_shape, f64, Gamma::new(0.1, 1.0));
65-
distr!(distr_binomial, u64, Binomial::new(20, 0.7));
66-
distr!(distr_poisson, u64, Poisson::new(4.0));
100+
distr_float!(distr_exp, f64, Exp::new(2.71828 * 3.14159));
101+
distr_float!(distr_normal, f64, Normal::new(-2.71828, 3.14159));
102+
distr_float!(distr_log_normal, f64, LogNormal::new(-2.71828, 3.14159));
103+
distr_float!(distr_gamma_large_shape, f64, Gamma::new(10., 1.0));
104+
distr_float!(distr_gamma_small_shape, f64, Gamma::new(0.1, 1.0));
105+
distr_int!(distr_binomial, u64, Binomial::new(20, 0.7));
106+
distr_int!(distr_poisson, u64, Poisson::new(4.0));
67107

68108

69109
// construct and sample from a range
70110
macro_rules! gen_range_int {
71-
($fnn:ident, $ty:ty, $low:expr, $high:expr) => {
111+
($fnn:ident, $ty:ident, $low:expr, $high:expr) => {
72112
#[bench]
73113
fn $fnn(b: &mut Bencher) {
74114
let mut rng = XorShiftRng::new();
75-
let high = $high;
76115

77116
b.iter(|| {
117+
let mut high = $high;
118+
let mut accum: $ty = 0;
78119
for _ in 0..::RAND_BENCH_N {
79-
let x: $ty = rng.gen_range($low, high);
80-
black_box(x);
81-
black_box(high);
120+
accum = accum.wrapping_add(rng.gen_range($low, high));
121+
// force recalculation of range each time
122+
high = high.wrapping_add(1) & std::$ty::MAX;
82123
}
124+
black_box(accum);
83125
});
84126
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
85127
}
86128
}
87129
}
88130

89-
gen_range_int!(gen_range_i8, i8, 20i8, 100);
131+
gen_range_int!(gen_range_i8, i8, -20i8, 100);
90132
gen_range_int!(gen_range_i16, i16, -500i16, 2000);
91133
gen_range_int!(gen_range_i32, i32, -200_000_000i32, 800_000_000);
92134
gen_range_int!(gen_range_i64, i64, 3i64, 12345678901234);

benches/generators.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ macro_rules! gen_uint {
4545
fn $fnn(b: &mut Bencher) {
4646
let mut rng = $gen;
4747
b.iter(|| {
48+
let mut accum: $ty = 0;
4849
for _ in 0..RAND_BENCH_N {
49-
black_box(rng.gen::<$ty>());
50+
accum = accum.wrapping_add(rng.gen::<$ty>());
5051
}
52+
black_box(accum);
5153
});
5254
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
5355
}
@@ -126,9 +128,11 @@ macro_rules! chacha_rounds {
126128
let mut rng = ChaChaRng::new();
127129
rng.set_rounds($rounds);
128130
b.iter(|| {
131+
let mut accum: u32 = 0;
129132
for _ in 0..RAND_BENCH_N {
130-
black_box(rng.gen::<u32>());
133+
accum = accum.wrapping_add(rng.gen::<u32>());
131134
}
135+
black_box(accum);
132136
});
133137
b.bytes = size_of::<u32>() as u64 * RAND_BENCH_N;
134138
}
@@ -138,9 +142,11 @@ macro_rules! chacha_rounds {
138142
let mut rng = ChaChaRng::new();
139143
rng.set_rounds($rounds);
140144
b.iter(|| {
145+
let mut accum: u64 = 0;
141146
for _ in 0..RAND_BENCH_N {
142-
black_box(rng.gen::<u64>());
147+
accum = accum.wrapping_add(rng.gen::<u64>());
143148
}
149+
black_box(accum);
144150
});
145151
b.bytes = size_of::<u64>() as u64 * RAND_BENCH_N;
146152
}
@@ -178,9 +184,11 @@ macro_rules! reseeding_uint {
178184
RESEEDING_THRESHOLD,
179185
EntropyRng::new());
180186
b.iter(|| {
187+
let mut accum: $ty = 0;
181188
for _ in 0..RAND_BENCH_N {
182-
black_box(rng.gen::<$ty>());
189+
accum = accum.wrapping_add(rng.gen::<$ty>());
183190
}
191+
black_box(accum);
184192
});
185193
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
186194
}
@@ -197,9 +205,11 @@ macro_rules! threadrng_uint {
197205
fn $fnn(b: &mut Bencher) {
198206
let mut rng = thread_rng();
199207
b.iter(|| {
208+
let mut accum: $ty = 0;
200209
for _ in 0..RAND_BENCH_N {
201-
black_box(rng.gen::<$ty>());
210+
accum = accum.wrapping_add(rng.gen::<$ty>());
202211
}
212+
black_box(accum);
203213
});
204214
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
205215
}

rand-core/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ authors = ["The Rust Project Developers"]
55
license = "MIT/Apache-2.0"
66
readme = "README.md"
77
repository = "https://github.com/rust-lang-nursery/rand"
8-
documentation = "https://docs.rs/rand"
9-
homepage = "https://github.com/rust-lang-nursery/rand"
8+
documentation = "https://docs.rs/rand-core"
9+
homepage = "https://crates.io/crates/rand-core"
1010
description = """
1111
Core random number generator traits and tools for implementation.
1212
"""

rand-core/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ prefer to use the main [rand] crate.
1616

1717
[Documentation](https://docs.rs/rand-core)
1818

19-
[rand]: ..
19+
[rand]: ../README.md
2020

2121

2222
# License

0 commit comments

Comments
 (0)