Skip to content

Commit 8fd4d0d

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

14 files changed

+127
-170
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use distributions::utils::ziggurat;
3434
/// use rand::prelude::*;
3535
/// use rand::distributions::Exp1;
3636
///
37-
/// let val: f64 = SmallRng::from_entropy().sample(Exp1);
37+
/// let val: f64 = Exp1.sample(&mut SmallRng::from_entropy());
3838
/// println!("{}", val);
3939
/// ```
4040
#[derive(Clone, Copy, Debug)]
@@ -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

+31-57
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ use core::simd::*;
3131
///
3232
/// # Example
3333
/// ```
34-
/// use rand::{thread_rng, Rng};
34+
/// use rand::prelude::*;
3535
/// use rand::distributions::OpenClosed01;
3636
///
37-
/// let val: f32 = thread_rng().sample(OpenClosed01);
37+
/// let val: f32 = OpenClosed01.sample(&mut thread_rng());
3838
/// println!("f32 from (0, 1): {}", val);
3939
/// ```
4040
///
@@ -57,10 +57,10 @@ pub struct OpenClosed01;
5757
///
5858
/// # Example
5959
/// ```
60-
/// use rand::{thread_rng, Rng};
60+
/// use rand::prelude::*;
6161
/// use rand::distributions::Open01;
6262
///
63-
/// let val: f32 = thread_rng().sample(Open01);
63+
/// let val: f32 = Open01.sample(&mut thread_rng());
6464
/// println!("f32 from (0, 1): {}", val);
6565
/// ```
6666
///
@@ -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

+17-3
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@
106106
//! Sampling from a distribution:
107107
//!
108108
//! ```
109-
//! use rand::{thread_rng, Rng};
109+
//! use rand::prelude::*;
110110
//! use rand::distributions::Exp;
111111
//!
112112
//! let exp = Exp::new(2.0);
113-
//! let v = thread_rng().sample(exp);
113+
//! let v = exp.sample(&mut thread_rng());
114114
//! println!("{} is from an Exp(2) distribution", v);
115115
//! ```
116116
//!
@@ -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::prelude::*;
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
@@ -329,7 +343,7 @@ impl<'a, D, R, T> Iterator for DistIter<'a, D, R, T>
329343
/// use rand::prelude::*;
330344
/// use rand::distributions::Standard;
331345
///
332-
/// let val: f32 = SmallRng::from_entropy().sample(Standard);
346+
/// let val: f32 = Standard.sample(&mut SmallRng::from_entropy());
333347
/// println!("f32 from [0, 1): {}", val);
334348
/// ```
335349
///

src/distributions/normal.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use distributions::utils::ziggurat;
3232
/// use rand::prelude::*;
3333
/// use rand::distributions::StandardNormal;
3434
///
35-
/// let val: f64 = SmallRng::from_entropy().sample(StandardNormal);
35+
/// let val: f64 = StandardNormal.sample(&mut SmallRng::from_entropy());
3636
/// println!("{}", val);
3737
/// ```
3838
#[derive(Clone, Copy, Debug)]
@@ -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
}

0 commit comments

Comments
 (0)