diff --git a/benches/misc.rs b/benches/misc.rs index 93a5c506fdc..3b9a9b3e7d2 100644 --- a/benches/misc.rs +++ b/benches/misc.rs @@ -66,7 +66,7 @@ fn misc_bernoulli_const(b: &mut Bencher) { let d = rand::distributions::Bernoulli::new(0.18); let mut accum = true; for _ in 0..::RAND_BENCH_N { - accum ^= rng.sample(d); + accum ^= d.sample(&mut rng); } accum }) @@ -80,7 +80,7 @@ fn misc_bernoulli_var(b: &mut Bencher) { let mut p = 0.18; for _ in 0..::RAND_BENCH_N { let d = rand::distributions::Bernoulli::new(p); - accum ^= rng.sample(d); + accum ^= d.sample(&mut rng); p += 0.0001; } accum @@ -95,7 +95,7 @@ macro_rules! sample_binomial { let (n, p) = ($n, $p); b.iter(|| { let d = rand::distributions::Binomial::new(n, p); - rng.sample(d) + d.sample(&mut rng) }) } } diff --git a/src/distributions/bernoulli.rs b/src/distributions/bernoulli.rs index 76bf924730d..4666cf9e207 100644 --- a/src/distributions/bernoulli.rs +++ b/src/distributions/bernoulli.rs @@ -120,7 +120,6 @@ impl Distribution for Bernoulli { #[cfg(test)] mod test { - use Rng; use distributions::Distribution; use super::Bernoulli; @@ -130,10 +129,8 @@ mod test { let always_false = Bernoulli::new(0.0); let always_true = Bernoulli::new(1.0); for _ in 0..5 { - assert_eq!(r.sample::(&always_false), false); - assert_eq!(r.sample::(&always_true), true); - assert_eq!(Distribution::::sample(&always_false, &mut r), false); - assert_eq!(Distribution::::sample(&always_true, &mut r), true); + assert_eq!(always_false.sample(&mut r), false); + assert_eq!(always_true.sample(&mut r), true); } } diff --git a/src/distributions/binomial.rs b/src/distributions/binomial.rs index 1c232fa43c7..91a44300c99 100644 --- a/src/distributions/binomial.rs +++ b/src/distributions/binomial.rs @@ -64,7 +64,7 @@ impl Distribution for Binomial { let mut result = 0; let d = Bernoulli::new(self.p); for _ in 0 .. self.n { - result += rng.sample(d) as u32; + result += d.sample(rng) as u32; } return result as u64; } @@ -96,7 +96,7 @@ impl Distribution for Binomial { let mut comp_dev: f64; loop { // draw from the Cauchy distribution - comp_dev = rng.sample(cauchy); + comp_dev = cauchy.sample(rng); // shift the peak of the comparison ditribution lresult = expected + sq * comp_dev; // repeat the drawing until we are in the range of possible values @@ -166,8 +166,8 @@ mod test { #[test] fn test_binomial_end_points() { let mut rng = ::test::rng(352); - assert_eq!(rng.sample(Binomial::new(20, 0.0)), 0); - assert_eq!(rng.sample(Binomial::new(20, 1.0)), 20); + assert_eq!(Binomial::new(20, 0.0).sample(&mut rng), 0); + assert_eq!(Binomial::new(20, 1.0).sample(&mut rng), 20); } #[test] diff --git a/src/distributions/exponential.rs b/src/distributions/exponential.rs index 9214cbb09ef..35130001e68 100644 --- a/src/distributions/exponential.rs +++ b/src/distributions/exponential.rs @@ -34,7 +34,7 @@ use distributions::utils::ziggurat; /// use rand::prelude::*; /// use rand::distributions::Exp1; /// -/// let val: f64 = SmallRng::from_entropy().sample(Exp1); +/// let val: f64 = Exp1.sample(&mut SmallRng::from_entropy()); /// println!("{}", val); /// ``` #[derive(Clone, Copy, Debug)] @@ -92,7 +92,7 @@ impl Exp { impl Distribution for Exp { fn sample(&self, rng: &mut R) -> f64 { - let n: f64 = rng.sample(Exp1); + let n: f64 = Exp1.sample(rng); n * self.lambda_inverse } } diff --git a/src/distributions/float.rs b/src/distributions/float.rs index 0d418ebdc74..4713f66a7cd 100644 --- a/src/distributions/float.rs +++ b/src/distributions/float.rs @@ -31,10 +31,10 @@ use core::simd::*; /// /// # Example /// ``` -/// use rand::{thread_rng, Rng}; +/// use rand::prelude::*; /// use rand::distributions::OpenClosed01; /// -/// let val: f32 = thread_rng().sample(OpenClosed01); +/// let val: f32 = OpenClosed01.sample(&mut thread_rng()); /// println!("f32 from (0, 1): {}", val); /// ``` /// @@ -57,10 +57,10 @@ pub struct OpenClosed01; /// /// # Example /// ``` -/// use rand::{thread_rng, Rng}; +/// use rand::prelude::*; /// use rand::distributions::Open01; /// -/// let val: f32 = thread_rng().sample(Open01); +/// let val: f32 = Open01.sample(&mut thread_rng()); /// println!("f32 from (0, 1): {}", val); /// ``` /// @@ -171,7 +171,7 @@ float_impls! { f64x8, u64x8, f64, u64, 52, 1023 } #[cfg(test)] mod tests { use Rng; - use distributions::{Open01, OpenClosed01}; + use distributions::{Distribution, Open01, OpenClosed01}; use rngs::mock::StepRng; #[cfg(feature="simd_support")] use core::simd::*; @@ -179,83 +179,57 @@ mod tests { const EPSILON32: f32 = ::core::f32::EPSILON; const EPSILON64: f64 = ::core::f64::EPSILON; - macro_rules! test_f32 { - ($fnn:ident, $ty:ident, $ZERO:expr, $EPSILON:expr) => { + macro_rules! test_float { + ($fnn:ident, $ty:ident, $ZERO:expr, $EPSILON:expr, $ONE_BITS:expr) => { #[test] fn $fnn() { // Standard let mut zeros = StepRng::new(0, 0); assert_eq!(zeros.gen::<$ty>(), $ZERO); - let mut one = StepRng::new(1 << 8 | 1 << (8 + 32), 0); + let mut one = StepRng::new($ONE_BITS, 0); assert_eq!(one.gen::<$ty>(), $EPSILON / 2.0); let mut max = StepRng::new(!0, 0); assert_eq!(max.gen::<$ty>(), 1.0 - $EPSILON / 2.0); // OpenClosed01 let mut zeros = StepRng::new(0, 0); - assert_eq!(zeros.sample::<$ty, _>(OpenClosed01), - 0.0 + $EPSILON / 2.0); - let mut one = StepRng::new(1 << 8 | 1 << (8 + 32), 0); - assert_eq!(one.sample::<$ty, _>(OpenClosed01), $EPSILON); + assert_eq!(Distribution::<$ty>::sample(&OpenClosed01, &mut zeros), + (0.0 + $EPSILON / 2.0) as $ty); + let mut one = StepRng::new($ONE_BITS, 0); + assert_eq!(Distribution::<$ty>::sample(&OpenClosed01, &mut one), + $EPSILON); let mut max = StepRng::new(!0, 0); - assert_eq!(max.sample::<$ty, _>(OpenClosed01), $ZERO + 1.0); + assert_eq!(Distribution::<$ty>::sample(&OpenClosed01, &mut max), + $ZERO + 1.0); // Open01 let mut zeros = StepRng::new(0, 0); - assert_eq!(zeros.sample::<$ty, _>(Open01), 0.0 + $EPSILON / 2.0); - let mut one = StepRng::new(1 << 9 | 1 << (9 + 32), 0); - assert_eq!(one.sample::<$ty, _>(Open01), $EPSILON / 2.0 * 3.0); + assert_eq!(Distribution::<$ty>::sample(&Open01, &mut zeros), + 0.0 + $EPSILON / 2.0); + let mut one = StepRng::new($ONE_BITS << 1, 0); + assert_eq!(Distribution::<$ty>::sample(&Open01, &mut one), + $EPSILON / 2.0 * 3.0); let mut max = StepRng::new(!0, 0); - assert_eq!(max.sample::<$ty, _>(Open01), 1.0 - $EPSILON / 2.0); + assert_eq!(Distribution::<$ty>::sample(&Open01, &mut max), + 1.0 - $EPSILON / 2.0); } } } - test_f32! { f32_edge_cases, f32, 0.0, EPSILON32 } + test_float! { f32_edge_cases, f32, 0.0, EPSILON32, 1 << 8 | 1 << (8 + 32) } #[cfg(feature="simd_support")] - test_f32! { f32x2_edge_cases, f32x2, f32x2::splat(0.0), f32x2::splat(EPSILON32) } + test_float! { f32x2_edge_cases, f32x2, f32x2::splat(0.0), f32x2::splat(EPSILON32), 1 << 8 | 1 << (8 + 32) } #[cfg(feature="simd_support")] - test_f32! { f32x4_edge_cases, f32x4, f32x4::splat(0.0), f32x4::splat(EPSILON32) } + test_float! { f32x4_edge_cases, f32x4, f32x4::splat(0.0), f32x4::splat(EPSILON32), 1 << 8 | 1 << (8 + 32) } #[cfg(feature="simd_support")] - test_f32! { f32x8_edge_cases, f32x8, f32x8::splat(0.0), f32x8::splat(EPSILON32) } + test_float! { f32x8_edge_cases, f32x8, f32x8::splat(0.0), f32x8::splat(EPSILON32), 1 << 8 | 1 << (8 + 32) } #[cfg(feature="simd_support")] - test_f32! { f32x16_edge_cases, f32x16, f32x16::splat(0.0), f32x16::splat(EPSILON32) } - - macro_rules! test_f64 { - ($fnn:ident, $ty:ident, $ZERO:expr, $EPSILON:expr) => { - #[test] - fn $fnn() { - // Standard - let mut zeros = StepRng::new(0, 0); - assert_eq!(zeros.gen::<$ty>(), $ZERO); - let mut one = StepRng::new(1 << 11, 0); - assert_eq!(one.gen::<$ty>(), $EPSILON / 2.0); - let mut max = StepRng::new(!0, 0); - assert_eq!(max.gen::<$ty>(), 1.0 - $EPSILON / 2.0); + test_float! { f32x16_edge_cases, f32x16, f32x16::splat(0.0), f32x16::splat(EPSILON32), 1 << 8 | 1 << (8 + 32) } - // OpenClosed01 - let mut zeros = StepRng::new(0, 0); - assert_eq!(zeros.sample::<$ty, _>(OpenClosed01), - 0.0 + $EPSILON / 2.0); - let mut one = StepRng::new(1 << 11, 0); - assert_eq!(one.sample::<$ty, _>(OpenClosed01), $EPSILON); - let mut max = StepRng::new(!0, 0); - assert_eq!(max.sample::<$ty, _>(OpenClosed01), $ZERO + 1.0); - - // Open01 - let mut zeros = StepRng::new(0, 0); - assert_eq!(zeros.sample::<$ty, _>(Open01), 0.0 + $EPSILON / 2.0); - let mut one = StepRng::new(1 << 12, 0); - assert_eq!(one.sample::<$ty, _>(Open01), $EPSILON / 2.0 * 3.0); - let mut max = StepRng::new(!0, 0); - assert_eq!(max.sample::<$ty, _>(Open01), 1.0 - $EPSILON / 2.0); - } - } - } - test_f64! { f64_edge_cases, f64, 0.0, EPSILON64 } + test_float! { f64_edge_cases, f64, 0.0, EPSILON64, 1 << 11 } #[cfg(feature="simd_support")] - test_f64! { f64x2_edge_cases, f64x2, f64x2::splat(0.0), f64x2::splat(EPSILON64) } + test_float! { f64x2_edge_cases, f64x2, f64x2::splat(0.0), f64x2::splat(EPSILON64), 1 << 11 } #[cfg(feature="simd_support")] - test_f64! { f64x4_edge_cases, f64x4, f64x4::splat(0.0), f64x4::splat(EPSILON64) } + test_float! { f64x4_edge_cases, f64x4, f64x4::splat(0.0), f64x4::splat(EPSILON64), 1 << 11 } #[cfg(feature="simd_support")] - test_f64! { f64x8_edge_cases, f64x8, f64x8::splat(0.0), f64x8::splat(EPSILON64) } + test_float! { f64x8_edge_cases, f64x8, f64x8::splat(0.0), f64x8::splat(EPSILON64), 1 << 11 } } diff --git a/src/distributions/gamma.rs b/src/distributions/gamma.rs index f02cf3b21e2..7c24cd7e8a0 100644 --- a/src/distributions/gamma.rs +++ b/src/distributions/gamma.rs @@ -142,7 +142,7 @@ impl Distribution for Gamma { } impl Distribution for GammaSmallShape { fn sample(&self, rng: &mut R) -> f64 { - let u: f64 = rng.sample(Open01); + let u: f64 = Open01.sample(rng); self.large_shape.sample(rng) * u.powf(self.inv_shape) } @@ -150,14 +150,14 @@ impl Distribution for GammaSmallShape { impl Distribution for GammaLargeShape { fn sample(&self, rng: &mut R) -> f64 { loop { - let x = rng.sample(StandardNormal); + let x = StandardNormal.sample(rng); let v_cbrt = 1.0 + self.c * x; if v_cbrt <= 0.0 { // a^3 <= 0 iff a <= 0 continue } let v = v_cbrt * v_cbrt * v_cbrt; - let u: f64 = rng.sample(Open01); + let u: f64 = Open01.sample(rng); let x_sqr = x * x; if u < 1.0 - 0.0331 * x_sqr * x_sqr || @@ -217,7 +217,7 @@ impl Distribution for ChiSquared { match self.repr { DoFExactlyOne => { // k == 1 => N(0,1)^2 - let norm = rng.sample(StandardNormal); + let norm = StandardNormal.sample(rng); norm * norm } DoFAnythingElse(ref g) => g.sample(rng) @@ -300,7 +300,7 @@ impl StudentT { } impl Distribution for StudentT { fn sample(&self, rng: &mut R) -> f64 { - let norm = rng.sample(StandardNormal); + let norm = StandardNormal.sample(rng); norm * (self.dof / self.chi.sample(rng)).sqrt() } } diff --git a/src/distributions/integer.rs b/src/distributions/integer.rs index 82efd9ba795..867402675f6 100644 --- a/src/distributions/integer.rs +++ b/src/distributions/integer.rs @@ -122,27 +122,26 @@ simd_impl!(512, u8x64, i8x64, u16x32, i16x32, u32x16, i32x16, u64x8, i64x8,); #[cfg(test)] mod tests { - use Rng; - use distributions::{Standard}; + use distributions::{Distribution, Standard}; #[test] fn test_integers() { - let mut rng = ::test::rng(806); + let rng = &mut ::test::rng(806); - rng.sample::(Standard); - rng.sample::(Standard); - rng.sample::(Standard); - rng.sample::(Standard); - rng.sample::(Standard); + let _: isize = Standard.sample(rng); + let _: i8 = Standard.sample(rng); + let _: i16 = Standard.sample(rng); + let _: i32 = Standard.sample(rng); + let _: i64 = Standard.sample(rng); #[cfg(feature = "i128_support")] - rng.sample::(Standard); + let _: i128 = Standard.sample(rng); - rng.sample::(Standard); - rng.sample::(Standard); - rng.sample::(Standard); - rng.sample::(Standard); - rng.sample::(Standard); + let _: usize = Standard.sample(rng); + let _: u8 = Standard.sample(rng); + let _: u16 = Standard.sample(rng); + let _: u32 = Standard.sample(rng); + let _: u64 = Standard.sample(rng); #[cfg(feature = "i128_support")] - rng.sample::(Standard); + let _: u128 = Standard.sample(rng); } } diff --git a/src/distributions/mod.rs b/src/distributions/mod.rs index 03d2d58e7bc..ae89f876713 100644 --- a/src/distributions/mod.rs +++ b/src/distributions/mod.rs @@ -106,11 +106,11 @@ //! Sampling from a distribution: //! //! ``` -//! use rand::{thread_rng, Rng}; +//! use rand::prelude::*; //! use rand::distributions::Exp; //! //! let exp = Exp::new(2.0); -//! let v = thread_rng().sample(exp); +//! let v = exp.sample(&mut thread_rng()); //! println!("{} is from an Exp(2) distribution", v); //! ``` //! @@ -220,6 +220,20 @@ mod utils; /// [`sample_iter`]: trait.Distribution.html#method.sample_iter pub trait Distribution { /// Generate a random value of `T`, using `rng` as the source of randomness. + /// + /// ### Example + /// + /// ``` + /// use rand::prelude::*; + /// use rand::distributions::Uniform; + /// + /// let mut rng = thread_rng(); + /// let distr = Uniform::new(10u32, 15); + /// for _ in 0..10 { + /// let x = distr.sample(&mut rng); + /// println!("{}", x); + /// } + /// ``` fn sample(&self, rng: &mut R) -> T; /// 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> /// use rand::prelude::*; /// use rand::distributions::Standard; /// -/// let val: f32 = SmallRng::from_entropy().sample(Standard); +/// let val: f32 = Standard.sample(&mut SmallRng::from_entropy()); /// println!("f32 from [0, 1): {}", val); /// ``` /// diff --git a/src/distributions/normal.rs b/src/distributions/normal.rs index a442b58b320..81b6f886a5c 100644 --- a/src/distributions/normal.rs +++ b/src/distributions/normal.rs @@ -32,7 +32,7 @@ use distributions::utils::ziggurat; /// use rand::prelude::*; /// use rand::distributions::StandardNormal; /// -/// let val: f64 = SmallRng::from_entropy().sample(StandardNormal); +/// let val: f64 = StandardNormal.sample(&mut SmallRng::from_entropy()); /// println!("{}", val); /// ``` #[derive(Clone, Copy, Debug)] @@ -56,8 +56,8 @@ impl Distribution for StandardNormal { let mut y = 0.0f64; while -2.0 * y < x * x { - let x_: f64 = rng.sample(Open01); - let y_: f64 = rng.sample(Open01); + let x_: f64 = Open01.sample(rng); + let y_: f64 = Open01.sample(rng); x = x_.ln() / ziggurat_tables::ZIG_NORM_R; y = y_.ln(); @@ -112,7 +112,7 @@ impl Normal { } impl Distribution for Normal { fn sample(&self, rng: &mut R) -> f64 { - let n = rng.sample(StandardNormal); + let n = StandardNormal.sample(rng); self.mean + self.std_dev * n } } diff --git a/src/distributions/other.rs b/src/distributions/other.rs index dc5be044303..56b24f758bb 100644 --- a/src/distributions/other.rs +++ b/src/distributions/other.rs @@ -25,12 +25,12 @@ use distributions::{Distribution, Standard, Uniform}; /// /// ``` /// use std::iter; -/// use rand::{Rng, thread_rng}; +/// use rand::prelude::*; /// use rand::distributions::Alphanumeric; /// /// let mut rng = thread_rng(); /// let chars: String = iter::repeat(()) -/// .map(|()| rng.sample(Alphanumeric)) +/// .map(|()| Alphanumeric.sample(&mut rng)) /// .take(7) /// .collect(); /// println!("Random chars: {}", chars); @@ -178,21 +178,22 @@ impl Distribution> for Standard where Standard: Distribution { #[cfg(test)] mod tests { - use {Rng, RngCore, Standard}; - use distributions::Alphanumeric; + use Standard; + use distributions::{Distribution, Alphanumeric}; #[cfg(all(not(feature="std"), feature="alloc"))] use alloc::string::String; #[test] fn test_misc() { - let rng: &mut RngCore = &mut ::test::rng(820); + let rng = &mut ::test::rng(820); - rng.sample::(Standard); - rng.sample::(Standard); + let _: char = Standard.sample(rng); + let _: bool = Standard.sample(rng); } #[cfg(feature="alloc")] #[test] fn test_chars() { + use Rng; use core::iter; let mut rng = ::test::rng(805); @@ -211,7 +212,7 @@ mod tests { // take the rejection sampling path. let mut incorrect = false; for _ in 0..100 { - let c = rng.sample(Alphanumeric); + let c = Alphanumeric.sample(&mut rng); incorrect |= !((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ); diff --git a/src/distributions/pareto.rs b/src/distributions/pareto.rs index ba628e061d5..8fdc7be6d6e 100644 --- a/src/distributions/pareto.rs +++ b/src/distributions/pareto.rs @@ -20,7 +20,7 @@ use distributions::{Distribution, OpenClosed01}; /// use rand::prelude::*; /// use rand::distributions::Pareto; /// -/// let val: f64 = SmallRng::from_entropy().sample(Pareto::new(1., 2.)); +/// let val: f64 = Pareto::new(1., 2.).sample(&mut SmallRng::from_entropy()); /// println!("{}", val); /// ``` #[derive(Clone, Copy, Debug)] @@ -46,7 +46,7 @@ impl Pareto { impl Distribution for Pareto { fn sample(&self, rng: &mut R) -> f64 { - let u: f64 = rng.sample(OpenClosed01); + let u: f64 = OpenClosed01.sample(rng); self.scale * u.powf(self.inv_neg_shape) } } diff --git a/src/distributions/poisson.rs b/src/distributions/poisson.rs index 1742f9e78e6..0125a3226de 100644 --- a/src/distributions/poisson.rs +++ b/src/distributions/poisson.rs @@ -82,7 +82,7 @@ impl Distribution for Poisson { loop { // draw from the Cauchy distribution - comp_dev = rng.sample(cauchy); + comp_dev = cauchy.sample(rng); // shift the peak of the comparison ditribution result = self.sqrt_2lambda * comp_dev + self.lambda; // repeat the drawing until we are in the range of possible values diff --git a/src/distributions/uniform.rs b/src/distributions/uniform.rs index bf68e45f51a..08ac3beaf42 100644 --- a/src/distributions/uniform.rs +++ b/src/distributions/uniform.rs @@ -28,7 +28,7 @@ //! # Example usage //! //! ``` -//! use rand::{Rng, thread_rng}; +//! use rand::prelude::*; //! use rand::distributions::Uniform; //! //! let mut rng = thread_rng(); @@ -37,7 +37,7 @@ //! // sample between 1 and 10 points //! for _ in 0..rng.gen_range(1, 11) { //! // sample a point from the square with sides -10 - 10 in two dimensions -//! let (x, y) = (rng.sample(side), rng.sample(side)); +//! let (x, y) = (side.sample(&mut rng), side.sample(&mut rng)); //! println!("Point: {}, {}", x, y); //! } //! ``` @@ -783,6 +783,7 @@ impl UniformSampler for UniformDuration { mod tests { use Rng; use rngs::mock::StepRng; + use distributions::Distribution; use distributions::uniform::Uniform; use distributions::utils::FloatAsSIMD; #[cfg(feature="simd_support")] use core::simd::*; @@ -798,7 +799,7 @@ mod tests { let mut rng = ::test::rng(804); let dist = Uniform::new_inclusive(10, 10); for _ in 0..20 { - assert_eq!(rng.sample(dist), 10); + assert_eq!(dist.sample(&mut rng), 10); } } @@ -810,7 +811,7 @@ mod tests { #[test] fn test_integers() { - let mut rng = ::test::rng(251); + let rng = &mut ::test::rng(251); macro_rules! t { ($($ty:ident),*) => {{ $( @@ -820,25 +821,25 @@ mod tests { for &(low, high) in v.iter() { let my_uniform = Uniform::new(low, high); for _ in 0..1000 { - let v: $ty = rng.sample(my_uniform); + let v: $ty = my_uniform.sample(rng); assert!(low <= v && v < high); } let my_uniform = Uniform::new_inclusive(low, high); for _ in 0..1000 { - let v: $ty = rng.sample(my_uniform); + let v: $ty = my_uniform.sample(rng); assert!(low <= v && v <= high); } let my_uniform = Uniform::new(&low, high); for _ in 0..1000 { - let v: $ty = rng.sample(my_uniform); + let v: $ty = my_uniform.sample(rng); assert!(low <= v && v < high); } let my_uniform = Uniform::new_inclusive(&low, &high); for _ in 0..1000 { - let v: $ty = rng.sample(my_uniform); + let v: $ty = my_uniform.sample(rng); assert!(low <= v && v <= high); } @@ -858,9 +859,9 @@ mod tests { #[test] fn test_floats() { - let mut rng = ::test::rng(252); - let mut zero_rng = StepRng::new(0, 0); - let mut max_rng = StepRng::new(0xffff_ffff_ffff_ffff, 0); + let rng = &mut ::test::rng(252); + let zero_rng = &mut StepRng::new(0, 0); + let max_rng = &mut StepRng::new(0xffff_ffff_ffff_ffff, 0); macro_rules! t { ($ty:ty, $f_scalar:ident, $bits_shifted:expr) => {{ let v: &[($f_scalar, $f_scalar)]= @@ -886,21 +887,21 @@ mod tests { let my_uniform = Uniform::new(low, high); let my_incl_uniform = Uniform::new_inclusive(low, high); for _ in 0..100 { - let v = rng.sample(my_uniform).extract(lane); + let v = my_uniform.sample(rng).extract(lane); assert!(low_scalar <= v && v < high_scalar); - let v = rng.sample(my_incl_uniform).extract(lane); + let v = my_incl_uniform.sample(rng).extract(lane); assert!(low_scalar <= v && v <= high_scalar); let v = rng.gen_range(low, high).extract(lane); assert!(low_scalar <= v && v < high_scalar); } - assert_eq!(rng.sample(Uniform::new_inclusive(low, low)).extract(lane), low_scalar); + assert_eq!(Uniform::new_inclusive(low, low).sample(rng).extract(lane), low_scalar); - assert_eq!(zero_rng.sample(my_uniform).extract(lane), low_scalar); - assert_eq!(zero_rng.sample(my_incl_uniform).extract(lane), low_scalar); + assert_eq!(my_uniform.sample(zero_rng).extract(lane), low_scalar); + assert_eq!(my_incl_uniform.sample(zero_rng).extract(lane), low_scalar); assert_eq!(zero_rng.gen_range(low, high).extract(lane), low_scalar); - assert!(max_rng.sample(my_uniform).extract(lane) < high_scalar); - assert!(max_rng.sample(my_incl_uniform).extract(lane) <= high_scalar); + assert!(my_uniform.sample(max_rng).extract(lane) < high_scalar); + assert!(my_incl_uniform.sample(max_rng).extract(lane) <= high_scalar); // Don't run this test for really tiny differences between high and low // since for those rounding might result in selecting high for a very @@ -914,11 +915,11 @@ mod tests { } } - assert_eq!(rng.sample(Uniform::new_inclusive(::core::$f_scalar::MAX, - ::core::$f_scalar::MAX)), + assert_eq!(Uniform::new_inclusive(::core::$f_scalar::MAX, + ::core::$f_scalar::MAX).sample(rng), ::core::$f_scalar::MAX); - assert_eq!(rng.sample(Uniform::new_inclusive(-::core::$f_scalar::MAX, - -::core::$f_scalar::MAX)), + assert_eq!(Uniform::new_inclusive(-::core::$f_scalar::MAX, + -::core::$f_scalar::MAX).sample(rng), -::core::$f_scalar::MAX); }} } @@ -1001,7 +1002,7 @@ mod tests { for &(low, high) in v.iter() { let my_uniform = Uniform::new(low, high); for _ in 0..1000 { - let v = rng.sample(my_uniform); + let v = my_uniform.sample(&mut rng); assert!(low <= v && v < high); } } @@ -1046,7 +1047,7 @@ mod tests { let uniform = Uniform::new(low, high); let mut rng = ::test::rng(804); for _ in 0..100 { - let x: MyF32 = rng.sample(uniform); + let x: MyF32 = uniform.sample(&mut rng); assert!(low <= x && x < high); } } diff --git a/src/lib.rs b/src/lib.rs index 695654ab002..fa57d2a9281 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -461,49 +461,20 @@ pub trait Rng: RngCore { /// Sample a new value, using the given distribution. /// - /// ### Example + /// Deprecated: use [`Distribution::sample`] instead. /// - /// ``` - /// use rand::{thread_rng, Rng}; - /// use rand::distributions::Uniform; - /// - /// let mut rng = thread_rng(); - /// let x = rng.sample(Uniform::new(10u32, 15)); - /// // Type annotation requires two types, the type and distribution; the - /// // distribution can be inferred. - /// let y = rng.sample::(Uniform::new(10, 15)); - /// ``` + /// [`Distribution::sample`]: distributions/trait.Distribution.html#method.sample + #[deprecated(since="0.6.0", note="use Distribution::sample instead")] fn sample>(&mut self, distr: D) -> T { distr.sample(self) } /// Create an iterator that generates values using the given distribution. /// - /// # Example + /// Deprecated: use [`Distribution::sample_iter`] instead. /// - /// ``` - /// use rand::{thread_rng, Rng}; - /// use rand::distributions::{Alphanumeric, Uniform, Standard}; - /// - /// let mut rng = thread_rng(); - /// - /// // Vec of 16 x f32: - /// let v: Vec = thread_rng().sample_iter(&Standard).take(16).collect(); - /// - /// // String: - /// let s: String = rng.sample_iter(&Alphanumeric).take(7).collect(); - /// - /// // Combined values - /// println!("{:?}", thread_rng().sample_iter(&Standard).take(5) - /// .collect::>()); - /// - /// // Dice-rolling: - /// let die_range = Uniform::new_inclusive(1, 6); - /// let mut roll_die = rng.sample_iter(&die_range); - /// while roll_die.next().unwrap() != 6 { - /// println!("Not a 6; rolling again!"); - /// } - /// ``` + /// [`Distribution::sample_iter`]: distributions/trait.Distribution.html#method.sample_iter + #[deprecated(since="0.6.0", note="use Distribution::sample_iter instead")] fn sample_iter<'a, T, D: Distribution>(&'a mut self, distr: &'a D) -> distributions::DistIter<'a, D, Self, T> where Self: Sized { @@ -597,7 +568,7 @@ pub trait Rng: RngCore { #[inline] fn gen_bool(&mut self, p: f64) -> bool { let d = distributions::Bernoulli::new(p); - self.sample(d) + d.sample(self) } /// Return a bool with a probability of `numerator/denominator` of being @@ -626,7 +597,7 @@ pub trait Rng: RngCore { #[inline] fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool { let d = distributions::Bernoulli::from_ratio(numerator, denominator); - self.sample(d) + d.sample(self) } /// Return a random element from `values`.