Skip to content

Commit f7ebf1e

Browse files
committed
Deprecate Rng::sample and Rng::sample_iter in favor of Distribution::sample and Distribution::sample_iter
1 parent b9cc581 commit f7ebf1e

14 files changed

+113
-156
lines changed

benches/misc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn misc_bernoulli_const(b: &mut Bencher) {
6666
let d = rand::distributions::Bernoulli::new(0.18);
6767
let mut accum = true;
6868
for _ in 0..::RAND_BENCH_N {
69-
accum ^= rng.sample(d);
69+
accum ^= d.sample(&mut rng);
7070
}
7171
accum
7272
})
@@ -80,7 +80,7 @@ fn misc_bernoulli_var(b: &mut Bencher) {
8080
let mut p = 0.18;
8181
for _ in 0..::RAND_BENCH_N {
8282
let d = rand::distributions::Bernoulli::new(p);
83-
accum ^= rng.sample(d);
83+
accum ^= d.sample(&mut rng);
8484
p += 0.0001;
8585
}
8686
accum
@@ -95,7 +95,7 @@ macro_rules! sample_binomial {
9595
let (n, p) = ($n, $p);
9696
b.iter(|| {
9797
let d = rand::distributions::Binomial::new(n, p);
98-
rng.sample(d)
98+
d.sample(&mut rng)
9999
})
100100
}
101101
}

src/distributions/bernoulli.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ impl Distribution<bool> for Bernoulli {
120120

121121
#[cfg(test)]
122122
mod test {
123-
use Rng;
124123
use distributions::Distribution;
125124
use super::Bernoulli;
126125

@@ -130,10 +129,8 @@ mod test {
130129
let always_false = Bernoulli::new(0.0);
131130
let always_true = Bernoulli::new(1.0);
132131
for _ in 0..5 {
133-
assert_eq!(r.sample::<bool, _>(&always_false), false);
134-
assert_eq!(r.sample::<bool, _>(&always_true), true);
135-
assert_eq!(Distribution::<bool>::sample(&always_false, &mut r), false);
136-
assert_eq!(Distribution::<bool>::sample(&always_true, &mut r), true);
132+
assert_eq!(always_false.sample(&mut r), false);
133+
assert_eq!(always_true.sample(&mut r), true);
137134
}
138135
}
139136

src/distributions/binomial.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Distribution<u64> for Binomial {
6464
let mut result = 0;
6565
let d = Bernoulli::new(self.p);
6666
for _ in 0 .. self.n {
67-
result += rng.sample(d) as u32;
67+
result += d.sample(rng) as u32;
6868
}
6969
return result as u64;
7070
}
@@ -96,7 +96,7 @@ impl Distribution<u64> for Binomial {
9696
let mut comp_dev: f64;
9797
loop {
9898
// draw from the Cauchy distribution
99-
comp_dev = rng.sample(cauchy);
99+
comp_dev = cauchy.sample(rng);
100100
// shift the peak of the comparison ditribution
101101
lresult = expected + sq * comp_dev;
102102
// repeat the drawing until we are in the range of possible values
@@ -166,8 +166,8 @@ mod test {
166166
#[test]
167167
fn test_binomial_end_points() {
168168
let mut rng = ::test::rng(352);
169-
assert_eq!(rng.sample(Binomial::new(20, 0.0)), 0);
170-
assert_eq!(rng.sample(Binomial::new(20, 1.0)), 20);
169+
assert_eq!(Binomial::new(20, 0.0).sample(&mut rng), 0);
170+
assert_eq!(Binomial::new(20, 1.0).sample(&mut rng), 20);
171171
}
172172

173173
#[test]

src/distributions/exponential.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl Exp {
9292

9393
impl Distribution<f64> for Exp {
9494
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
95-
let n: f64 = rng.sample(Exp1);
95+
let n: f64 = Exp1.sample(rng);
9696
n * self.lambda_inverse
9797
}
9898
}

src/distributions/float.rs

+27-53
Original file line numberDiff line numberDiff line change
@@ -171,91 +171,65 @@ float_impls! { f64x8, u64x8, f64, u64, 52, 1023 }
171171
#[cfg(test)]
172172
mod tests {
173173
use Rng;
174-
use distributions::{Open01, OpenClosed01};
174+
use distributions::{Distribution, Open01, OpenClosed01};
175175
use rngs::mock::StepRng;
176176
#[cfg(feature="simd_support")]
177177
use core::simd::*;
178178

179179
const EPSILON32: f32 = ::core::f32::EPSILON;
180180
const EPSILON64: f64 = ::core::f64::EPSILON;
181181

182-
macro_rules! test_f32 {
183-
($fnn:ident, $ty:ident, $ZERO:expr, $EPSILON:expr) => {
182+
macro_rules! test_float {
183+
($fnn:ident, $ty:ident, $ZERO:expr, $EPSILON:expr, $ONE_BITS:expr) => {
184184
#[test]
185185
fn $fnn() {
186186
// Standard
187187
let mut zeros = StepRng::new(0, 0);
188188
assert_eq!(zeros.gen::<$ty>(), $ZERO);
189-
let mut one = StepRng::new(1 << 8 | 1 << (8 + 32), 0);
189+
let mut one = StepRng::new($ONE_BITS, 0);
190190
assert_eq!(one.gen::<$ty>(), $EPSILON / 2.0);
191191
let mut max = StepRng::new(!0, 0);
192192
assert_eq!(max.gen::<$ty>(), 1.0 - $EPSILON / 2.0);
193193

194194
// OpenClosed01
195195
let mut zeros = StepRng::new(0, 0);
196-
assert_eq!(zeros.sample::<$ty, _>(OpenClosed01),
197-
0.0 + $EPSILON / 2.0);
198-
let mut one = StepRng::new(1 << 8 | 1 << (8 + 32), 0);
199-
assert_eq!(one.sample::<$ty, _>(OpenClosed01), $EPSILON);
196+
assert_eq!(Distribution::<$ty>::sample(&OpenClosed01, &mut zeros),
197+
(0.0 + $EPSILON / 2.0) as $ty);
198+
let mut one = StepRng::new($ONE_BITS, 0);
199+
assert_eq!(Distribution::<$ty>::sample(&OpenClosed01, &mut one),
200+
$EPSILON);
200201
let mut max = StepRng::new(!0, 0);
201-
assert_eq!(max.sample::<$ty, _>(OpenClosed01), $ZERO + 1.0);
202+
assert_eq!(Distribution::<$ty>::sample(&OpenClosed01, &mut max),
203+
$ZERO + 1.0);
202204

203205
// Open01
204206
let mut zeros = StepRng::new(0, 0);
205-
assert_eq!(zeros.sample::<$ty, _>(Open01), 0.0 + $EPSILON / 2.0);
206-
let mut one = StepRng::new(1 << 9 | 1 << (9 + 32), 0);
207-
assert_eq!(one.sample::<$ty, _>(Open01), $EPSILON / 2.0 * 3.0);
207+
assert_eq!(Distribution::<$ty>::sample(&Open01, &mut zeros),
208+
0.0 + $EPSILON / 2.0);
209+
let mut one = StepRng::new($ONE_BITS << 1, 0);
210+
assert_eq!(Distribution::<$ty>::sample(&Open01, &mut one),
211+
$EPSILON / 2.0 * 3.0);
208212
let mut max = StepRng::new(!0, 0);
209-
assert_eq!(max.sample::<$ty, _>(Open01), 1.0 - $EPSILON / 2.0);
213+
assert_eq!(Distribution::<$ty>::sample(&Open01, &mut max),
214+
1.0 - $EPSILON / 2.0);
210215
}
211216
}
212217
}
213-
test_f32! { f32_edge_cases, f32, 0.0, EPSILON32 }
218+
test_float! { f32_edge_cases, f32, 0.0, EPSILON32, 1 << 8 | 1 << (8 + 32) }
214219
#[cfg(feature="simd_support")]
215-
test_f32! { f32x2_edge_cases, f32x2, f32x2::splat(0.0), f32x2::splat(EPSILON32) }
220+
test_float! { f32x2_edge_cases, f32x2, f32x2::splat(0.0), f32x2::splat(EPSILON32), 1 << 8 | 1 << (8 + 32) }
216221
#[cfg(feature="simd_support")]
217-
test_f32! { f32x4_edge_cases, f32x4, f32x4::splat(0.0), f32x4::splat(EPSILON32) }
222+
test_float! { f32x4_edge_cases, f32x4, f32x4::splat(0.0), f32x4::splat(EPSILON32), 1 << 8 | 1 << (8 + 32) }
218223
#[cfg(feature="simd_support")]
219-
test_f32! { f32x8_edge_cases, f32x8, f32x8::splat(0.0), f32x8::splat(EPSILON32) }
224+
test_float! { f32x8_edge_cases, f32x8, f32x8::splat(0.0), f32x8::splat(EPSILON32), 1 << 8 | 1 << (8 + 32) }
220225
#[cfg(feature="simd_support")]
221-
test_f32! { f32x16_edge_cases, f32x16, f32x16::splat(0.0), f32x16::splat(EPSILON32) }
222-
223-
macro_rules! test_f64 {
224-
($fnn:ident, $ty:ident, $ZERO:expr, $EPSILON:expr) => {
225-
#[test]
226-
fn $fnn() {
227-
// Standard
228-
let mut zeros = StepRng::new(0, 0);
229-
assert_eq!(zeros.gen::<$ty>(), $ZERO);
230-
let mut one = StepRng::new(1 << 11, 0);
231-
assert_eq!(one.gen::<$ty>(), $EPSILON / 2.0);
232-
let mut max = StepRng::new(!0, 0);
233-
assert_eq!(max.gen::<$ty>(), 1.0 - $EPSILON / 2.0);
226+
test_float! { f32x16_edge_cases, f32x16, f32x16::splat(0.0), f32x16::splat(EPSILON32), 1 << 8 | 1 << (8 + 32) }
234227

235-
// OpenClosed01
236-
let mut zeros = StepRng::new(0, 0);
237-
assert_eq!(zeros.sample::<$ty, _>(OpenClosed01),
238-
0.0 + $EPSILON / 2.0);
239-
let mut one = StepRng::new(1 << 11, 0);
240-
assert_eq!(one.sample::<$ty, _>(OpenClosed01), $EPSILON);
241-
let mut max = StepRng::new(!0, 0);
242-
assert_eq!(max.sample::<$ty, _>(OpenClosed01), $ZERO + 1.0);
243-
244-
// Open01
245-
let mut zeros = StepRng::new(0, 0);
246-
assert_eq!(zeros.sample::<$ty, _>(Open01), 0.0 + $EPSILON / 2.0);
247-
let mut one = StepRng::new(1 << 12, 0);
248-
assert_eq!(one.sample::<$ty, _>(Open01), $EPSILON / 2.0 * 3.0);
249-
let mut max = StepRng::new(!0, 0);
250-
assert_eq!(max.sample::<$ty, _>(Open01), 1.0 - $EPSILON / 2.0);
251-
}
252-
}
253-
}
254-
test_f64! { f64_edge_cases, f64, 0.0, EPSILON64 }
228+
test_float! { f64_edge_cases, f64, 0.0, EPSILON64, 1 << 11 }
255229
#[cfg(feature="simd_support")]
256-
test_f64! { f64x2_edge_cases, f64x2, f64x2::splat(0.0), f64x2::splat(EPSILON64) }
230+
test_float! { f64x2_edge_cases, f64x2, f64x2::splat(0.0), f64x2::splat(EPSILON64), 1 << 11 }
257231
#[cfg(feature="simd_support")]
258-
test_f64! { f64x4_edge_cases, f64x4, f64x4::splat(0.0), f64x4::splat(EPSILON64) }
232+
test_float! { f64x4_edge_cases, f64x4, f64x4::splat(0.0), f64x4::splat(EPSILON64), 1 << 11 }
259233
#[cfg(feature="simd_support")]
260-
test_f64! { f64x8_edge_cases, f64x8, f64x8::splat(0.0), f64x8::splat(EPSILON64) }
234+
test_float! { f64x8_edge_cases, f64x8, f64x8::splat(0.0), f64x8::splat(EPSILON64), 1 << 11 }
261235
}

src/distributions/gamma.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,22 @@ impl Distribution<f64> for Gamma {
142142
}
143143
impl Distribution<f64> for GammaSmallShape {
144144
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
145-
let u: f64 = rng.sample(Open01);
145+
let u: f64 = Open01.sample(rng);
146146

147147
self.large_shape.sample(rng) * u.powf(self.inv_shape)
148148
}
149149
}
150150
impl Distribution<f64> for GammaLargeShape {
151151
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
152152
loop {
153-
let x = rng.sample(StandardNormal);
153+
let x = StandardNormal.sample(rng);
154154
let v_cbrt = 1.0 + self.c * x;
155155
if v_cbrt <= 0.0 { // a^3 <= 0 iff a <= 0
156156
continue
157157
}
158158

159159
let v = v_cbrt * v_cbrt * v_cbrt;
160-
let u: f64 = rng.sample(Open01);
160+
let u: f64 = Open01.sample(rng);
161161

162162
let x_sqr = x * x;
163163
if u < 1.0 - 0.0331 * x_sqr * x_sqr ||
@@ -217,7 +217,7 @@ impl Distribution<f64> for ChiSquared {
217217
match self.repr {
218218
DoFExactlyOne => {
219219
// k == 1 => N(0,1)^2
220-
let norm = rng.sample(StandardNormal);
220+
let norm = StandardNormal.sample(rng);
221221
norm * norm
222222
}
223223
DoFAnythingElse(ref g) => g.sample(rng)
@@ -300,7 +300,7 @@ impl StudentT {
300300
}
301301
impl Distribution<f64> for StudentT {
302302
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
303-
let norm = rng.sample(StandardNormal);
303+
let norm = StandardNormal.sample(rng);
304304
norm * (self.dof / self.chi.sample(rng)).sqrt()
305305
}
306306
}

src/distributions/integer.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -122,27 +122,26 @@ simd_impl!(512, u8x64, i8x64, u16x32, i16x32, u32x16, i32x16, u64x8, i64x8,);
122122

123123
#[cfg(test)]
124124
mod tests {
125-
use Rng;
126-
use distributions::{Standard};
125+
use distributions::{Distribution, Standard};
127126

128127
#[test]
129128
fn test_integers() {
130-
let mut rng = ::test::rng(806);
129+
let rng = &mut ::test::rng(806);
131130

132-
rng.sample::<isize, _>(Standard);
133-
rng.sample::<i8, _>(Standard);
134-
rng.sample::<i16, _>(Standard);
135-
rng.sample::<i32, _>(Standard);
136-
rng.sample::<i64, _>(Standard);
131+
let _: isize = Standard.sample(rng);
132+
let _: i8 = Standard.sample(rng);
133+
let _: i16 = Standard.sample(rng);
134+
let _: i32 = Standard.sample(rng);
135+
let _: i64 = Standard.sample(rng);
137136
#[cfg(feature = "i128_support")]
138-
rng.sample::<i128, _>(Standard);
137+
let _: i128 = Standard.sample(rng);
139138

140-
rng.sample::<usize, _>(Standard);
141-
rng.sample::<u8, _>(Standard);
142-
rng.sample::<u16, _>(Standard);
143-
rng.sample::<u32, _>(Standard);
144-
rng.sample::<u64, _>(Standard);
139+
let _: usize = Standard.sample(rng);
140+
let _: u8 = Standard.sample(rng);
141+
let _: u16 = Standard.sample(rng);
142+
let _: u32 = Standard.sample(rng);
143+
let _: u64 = Standard.sample(rng);
145144
#[cfg(feature = "i128_support")]
146-
rng.sample::<u128, _>(Standard);
145+
let _: u128 = Standard.sample(rng);
147146
}
148147
}

src/distributions/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,20 @@ mod utils;
220220
/// [`sample_iter`]: trait.Distribution.html#method.sample_iter
221221
pub trait Distribution<T> {
222222
/// Generate a random value of `T`, using `rng` as the source of randomness.
223+
///
224+
/// ### Example
225+
///
226+
/// ```
227+
/// use rand::{thread_rng, Rng};
228+
/// use rand::distributions::Uniform;
229+
///
230+
/// let mut rng = thread_rng();
231+
/// let distr = Uniform::new(10u32, 15);
232+
/// for _ in 0..10 {
233+
/// let x = distr.sample(&mut rng);
234+
/// println!("{}", x);
235+
/// }
236+
/// ```
223237
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T;
224238

225239
/// Create an iterator that generates random values of `T`, using `rng` as

src/distributions/normal.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ impl Distribution<f64> for StandardNormal {
5656
let mut y = 0.0f64;
5757

5858
while -2.0 * y < x * x {
59-
let x_: f64 = rng.sample(Open01);
60-
let y_: f64 = rng.sample(Open01);
59+
let x_: f64 = Open01.sample(rng);
60+
let y_: f64 = Open01.sample(rng);
6161

6262
x = x_.ln() / ziggurat_tables::ZIG_NORM_R;
6363
y = y_.ln();
@@ -112,7 +112,7 @@ impl Normal {
112112
}
113113
impl Distribution<f64> for Normal {
114114
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
115-
let n = rng.sample(StandardNormal);
115+
let n = StandardNormal.sample(rng);
116116
self.mean + self.std_dev * n
117117
}
118118
}

src/distributions/other.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,22 @@ impl<T> Distribution<Wrapping<T>> for Standard where Standard: Distribution<T> {
178178

179179
#[cfg(test)]
180180
mod tests {
181-
use {Rng, RngCore, Standard};
182-
use distributions::Alphanumeric;
181+
use Standard;
182+
use distributions::{Distribution, Alphanumeric};
183183
#[cfg(all(not(feature="std"), feature="alloc"))] use alloc::string::String;
184184

185185
#[test]
186186
fn test_misc() {
187-
let rng: &mut RngCore = &mut ::test::rng(820);
187+
let rng = &mut ::test::rng(820);
188188

189-
rng.sample::<char, _>(Standard);
190-
rng.sample::<bool, _>(Standard);
189+
let _: char = Standard.sample(rng);
190+
let _: bool = Standard.sample(rng);
191191
}
192192

193193
#[cfg(feature="alloc")]
194194
#[test]
195195
fn test_chars() {
196+
use Rng;
196197
use core::iter;
197198
let mut rng = ::test::rng(805);
198199

@@ -211,7 +212,7 @@ mod tests {
211212
// take the rejection sampling path.
212213
let mut incorrect = false;
213214
for _ in 0..100 {
214-
let c = rng.sample(Alphanumeric);
215+
let c = Alphanumeric.sample(&mut rng);
215216
incorrect |= !((c >= '0' && c <= '9') ||
216217
(c >= 'A' && c <= 'Z') ||
217218
(c >= 'a' && c <= 'z') );

src/distributions/pareto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Pareto {
4646

4747
impl Distribution<f64> for Pareto {
4848
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
49-
let u: f64 = rng.sample(OpenClosed01);
49+
let u: f64 = OpenClosed01.sample(rng);
5050
self.scale * u.powf(self.inv_neg_shape)
5151
}
5252
}

src/distributions/poisson.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl Distribution<u64> for Poisson {
8282

8383
loop {
8484
// draw from the Cauchy distribution
85-
comp_dev = rng.sample(cauchy);
85+
comp_dev = cauchy.sample(rng);
8686
// shift the peak of the comparison ditribution
8787
result = self.sqrt_2lambda * comp_dev + self.lambda;
8888
// repeat the drawing until we are in the range of possible values

0 commit comments

Comments
 (0)