diff --git a/src/distributions/exponential.rs b/src/distributions/exponential.rs index 6a7727066f4..9663f9cfc67 100644 --- a/src/distributions/exponential.rs +++ b/src/distributions/exponential.rs @@ -13,7 +13,7 @@ use std::num::Float; use {Rng, Rand}; -use distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample}; +use distributions::{ziggurat, ziggurat_tables, Distribution}; /// A wrapper around an `f64` to generate Exp(1) random numbers. /// @@ -60,10 +60,10 @@ impl Rand for Exp1 { /// # Example /// /// ```rust -/// use rand::distributions::{Exp, IndependentSample}; +/// use rand::distributions::{Exp, Distribution}; /// /// let exp = Exp::new(2.0); -/// let v = exp.ind_sample(&mut rand::thread_rng()); +/// let v = exp.sample(&mut rand::thread_rng()); /// println!("{} is from a Exp(2) distribution", v); /// ``` #[derive(Copy)] @@ -81,11 +81,10 @@ impl Exp { } } -impl Sample for Exp { - fn sample(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) } -} -impl IndependentSample for Exp { - fn ind_sample(&self, rng: &mut R) -> f64 { +impl Distribution for Exp { + type Output = f64; + + fn sample(&self, rng: &mut R) -> f64 { let Exp1(n) = rng.gen::(); n * self.lambda_inverse } @@ -93,16 +92,15 @@ impl IndependentSample for Exp { #[cfg(test)] mod test { - use distributions::{Sample, IndependentSample}; + use distributions::Distribution; use super::Exp; #[test] fn test_exp() { - let mut exp = Exp::new(10.0); + let exp = Exp::new(10.0); let mut rng = ::test::rng(); for _ in 0..1000 { assert!(exp.sample(&mut rng) >= 0.0); - assert!(exp.ind_sample(&mut rng) >= 0.0); } } #[test] @@ -124,12 +122,12 @@ mod bench { use self::test::Bencher; use std::mem::size_of; use super::Exp; - use distributions::Sample; + use distributions::Distribution; #[bench] fn rand_exp(b: &mut Bencher) { let mut rng = ::test::weak_rng(); - let mut exp = Exp::new(2.71828 * 3.14159); + let exp = Exp::new(2.71828 * 3.14159); b.iter(|| { for _ in 0..::RAND_BENCH_N { diff --git a/src/distributions/gamma.rs b/src/distributions/gamma.rs index 5f6aaa4c562..c9e51f0f85b 100644 --- a/src/distributions/gamma.rs +++ b/src/distributions/gamma.rs @@ -19,7 +19,7 @@ use std::num::Float; use {Rng, Open01}; use super::normal::StandardNormal; -use super::{IndependentSample, Sample, Exp}; +use super::{Distribution, Exp}; /// The Gamma distribution `Gamma(shape, scale)` distribution. /// @@ -40,10 +40,10 @@ use super::{IndependentSample, Sample, Exp}; /// # Example /// /// ```rust -/// use rand::distributions::{IndependentSample, Gamma}; +/// use rand::distributions::{Distribution, Gamma}; /// /// let gamma = Gamma::new(2.0, 5.0); -/// let v = gamma.ind_sample(&mut rand::thread_rng()); +/// let v = gamma.sample(&mut rand::thread_rng()); /// println!("{} is from a Gamma(2, 5) distribution", v); /// ``` /// @@ -128,34 +128,30 @@ impl GammaLargeShape { } } -impl Sample for Gamma { - fn sample(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) } -} -impl Sample for GammaSmallShape { - fn sample(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) } -} -impl Sample for GammaLargeShape { - fn sample(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) } -} +impl Distribution for Gamma { + type Output = f64; -impl IndependentSample for Gamma { - fn ind_sample(&self, rng: &mut R) -> f64 { + fn sample(&self, rng: &mut R) -> f64 { match self.repr { - Small(ref g) => g.ind_sample(rng), - One(ref g) => g.ind_sample(rng), - Large(ref g) => g.ind_sample(rng), + Small(ref g) => g.sample(rng), + One(ref g) => g.sample(rng), + Large(ref g) => g.sample(rng), } } } -impl IndependentSample for GammaSmallShape { - fn ind_sample(&self, rng: &mut R) -> f64 { +impl Distribution for GammaSmallShape { + type Output = f64; + + fn sample(&self, rng: &mut R) -> f64 { let Open01(u) = rng.gen::>(); - self.large_shape.ind_sample(rng) * u.powf(self.inv_shape) + self.large_shape.sample(rng) * u.powf(self.inv_shape) } } -impl IndependentSample for GammaLargeShape { - fn ind_sample(&self, rng: &mut R) -> f64 { +impl Distribution for GammaLargeShape { + type Output = f64; + + fn sample(&self, rng: &mut R) -> f64 { loop { let StandardNormal(x) = rng.gen::(); let v_cbrt = 1.0 + self.c * x; @@ -186,10 +182,10 @@ impl IndependentSample for GammaLargeShape { /// # Example /// /// ```rust -/// use rand::distributions::{ChiSquared, IndependentSample}; +/// use rand::distributions::{ChiSquared, Distribution}; /// /// let chi = ChiSquared::new(11.0); -/// let v = chi.ind_sample(&mut rand::thread_rng()); +/// let v = chi.sample(&mut rand::thread_rng()); /// println!("{} is from a χ²(11) distribution", v) /// ``` pub struct ChiSquared { @@ -217,18 +213,17 @@ impl ChiSquared { ChiSquared { repr: repr } } } -impl Sample for ChiSquared { - fn sample(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) } -} -impl IndependentSample for ChiSquared { - fn ind_sample(&self, rng: &mut R) -> f64 { +impl Distribution for ChiSquared { + type Output = f64; + + fn sample(&self, rng: &mut R) -> f64 { match self.repr { DoFExactlyOne => { // k == 1 => N(0,1)^2 let StandardNormal(norm) = rng.gen::(); norm * norm } - DoFAnythingElse(ref g) => g.ind_sample(rng) + DoFAnythingElse(ref g) => g.sample(rng) } } } @@ -242,10 +237,10 @@ impl IndependentSample for ChiSquared { /// # Example /// /// ```rust -/// use rand::distributions::{FisherF, IndependentSample}; +/// use rand::distributions::{FisherF, Distribution}; /// /// let f = FisherF::new(2.0, 32.0); -/// let v = f.ind_sample(&mut rand::thread_rng()); +/// let v = f.sample(&mut rand::thread_rng()); /// println!("{} is from an F(2, 32) distribution", v) /// ``` pub struct FisherF { @@ -270,12 +265,11 @@ impl FisherF { } } } -impl Sample for FisherF { - fn sample(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) } -} -impl IndependentSample for FisherF { - fn ind_sample(&self, rng: &mut R) -> f64 { - self.numer.ind_sample(rng) / self.denom.ind_sample(rng) * self.dof_ratio +impl Distribution for FisherF { + type Output = f64; + + fn sample(&self, rng: &mut R) -> f64 { + self.numer.sample(rng) / self.denom.sample(rng) * self.dof_ratio } } @@ -285,10 +279,10 @@ impl IndependentSample for FisherF { /// # Example /// /// ```rust -/// use rand::distributions::{StudentT, IndependentSample}; +/// use rand::distributions::{StudentT, Distribution}; /// /// let t = StudentT::new(11.0); -/// let v = t.ind_sample(&mut rand::thread_rng()); +/// let v = t.sample(&mut rand::thread_rng()); /// println!("{} is from a t(11) distribution", v) /// ``` pub struct StudentT { @@ -307,46 +301,42 @@ impl StudentT { } } } -impl Sample for StudentT { - fn sample(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) } -} -impl IndependentSample for StudentT { - fn ind_sample(&self, rng: &mut R) -> f64 { +impl Distribution for StudentT { + type Output = f64; + + fn sample(&self, rng: &mut R) -> f64 { let StandardNormal(norm) = rng.gen::(); - norm * (self.dof / self.chi.ind_sample(rng)).sqrt() + norm * (self.dof / self.chi.sample(rng)).sqrt() } } #[cfg(test)] mod test { - use distributions::{Sample, IndependentSample}; + use distributions::Distribution; use super::{ChiSquared, StudentT, FisherF}; #[test] fn test_chi_squared_one() { - let mut chi = ChiSquared::new(1.0); + let chi = ChiSquared::new(1.0); let mut rng = ::test::rng(); for _ in 0..1000 { chi.sample(&mut rng); - chi.ind_sample(&mut rng); } } #[test] fn test_chi_squared_small() { - let mut chi = ChiSquared::new(0.5); + let chi = ChiSquared::new(0.5); let mut rng = ::test::rng(); for _ in 0..1000 { chi.sample(&mut rng); - chi.ind_sample(&mut rng); } } #[test] fn test_chi_squared_large() { - let mut chi = ChiSquared::new(30.0); + let chi = ChiSquared::new(30.0); let mut rng = ::test::rng(); for _ in 0..1000 { chi.sample(&mut rng); - chi.ind_sample(&mut rng); } } #[test] @@ -357,21 +347,19 @@ mod test { #[test] fn test_f() { - let mut f = FisherF::new(2.0, 32.0); + let f = FisherF::new(2.0, 32.0); let mut rng = ::test::rng(); for _ in 0..1000 { f.sample(&mut rng); - f.ind_sample(&mut rng); } } #[test] fn test_t() { - let mut t = StudentT::new(11.0); + let t = StudentT::new(11.0); let mut rng = ::test::rng(); for _ in 0..1000 { t.sample(&mut rng); - t.ind_sample(&mut rng); } } } @@ -381,7 +369,7 @@ mod bench { extern crate test; use self::test::Bencher; use std::mem::size_of; - use distributions::IndependentSample; + use distributions::Distribution; use super::Gamma; @@ -392,7 +380,7 @@ mod bench { b.iter(|| { for _ in 0..::RAND_BENCH_N { - gamma.ind_sample(&mut rng); + gamma.sample(&mut rng); } }); b.bytes = size_of::() as u64 * ::RAND_BENCH_N; @@ -405,7 +393,7 @@ mod bench { b.iter(|| { for _ in 0..::RAND_BENCH_N { - gamma.ind_sample(&mut rng); + gamma.sample(&mut rng); } }); b.bytes = size_of::() as u64 * ::RAND_BENCH_N; diff --git a/src/distributions/mod.rs b/src/distributions/mod.rs index 703b31675a4..1d0e4576c11 100644 --- a/src/distributions/mod.rs +++ b/src/distributions/mod.rs @@ -11,11 +11,10 @@ //! Sampling from random distributions. //! //! This is a generalization of `Rand` to allow parameters to control the -//! exact properties of the generated values, e.g. the mean and standard -//! deviation of a normal distribution. The `Sample` trait is the most -//! general, and allows for generating values that change some state -//! internally. The `IndependentSample` trait is for generating values -//! that do not need to record state. +//! distribution of the generated values, e.g. the mean and standard deviation +//! of a normal distribution. The `Distribution` trait allows for generating values +//! from a distribution that is independent of the number of samples generated, +//! i.e. sampling "with replacement". use std::num::{Float, Int}; use std::marker; @@ -32,45 +31,35 @@ pub mod gamma; pub mod normal; pub mod exponential; -/// Types that can be used to create a random instance of `Support`. -pub trait Sample { - /// Generate a random value of `Support`, using `rng` as the - /// source of randomness. - fn sample(&mut self, rng: &mut R) -> Support; -} - -/// `Sample`s that do not require keeping track of state. +/// Type that can be used to create a random instance of `Output`. /// /// Since no state is recorded, each sample is (statistically) /// independent of all others, assuming the `Rng` used has this /// property. -// FIXME maybe having this separate is overkill (the only reason is to -// take &self rather than &mut self)? or maybe this should be the -// trait called `Sample` and the other should be `DependentSample`. -pub trait IndependentSample: Sample { - /// Generate a random value. - fn ind_sample(&self, &mut R) -> Support; +pub trait Distribution { + type Output; + /// Generate a random value of `Output`, using `rng` as the + /// source of randomness. + fn sample(&self, rng: &mut R) -> ::Output; } -/// A wrapper for generating types that implement `Rand` via the -/// `Sample` & `IndependentSample` traits. -pub struct RandSample { - _marker: marker::PhantomData Sup>, +/// A wrapper for generating types that implement `Distribution` via the +/// `Rand` trait. +pub struct RandDistribution { + _marker: marker::PhantomData T>, } -impl Sample for RandSample { - fn sample(&mut self, rng: &mut R) -> Sup { self.ind_sample(rng) } -} +impl Distribution for RandDistribution { + type Output = T; -impl IndependentSample for RandSample { - fn ind_sample(&self, rng: &mut R) -> Sup { + fn sample(&self, rng: &mut R) -> T { rng.gen() } } -impl RandSample { - pub fn new() -> RandSample { - RandSample { _marker: marker::PhantomData } +impl RandDistribution { + pub fn new() -> RandDistribution { + RandDistribution { _marker: marker::PhantomData } } } @@ -87,15 +76,14 @@ pub struct Weighted { /// Each item has an associated weight that influences how likely it /// is to be chosen: higher weight is more likely. /// -/// The `Clone` restriction is a limitation of the `Sample` and -/// `IndependentSample` traits. Note that `&T` is (cheaply) `Clone` for -/// all `T`, as is `u32`, so one can store references or indices into -/// another vector. +/// The `Clone` restriction is a limitation of the `Distribution` trait. Note that +/// `&T` is (cheaply) `Clone` for all `T`, as is `u32`, so one can store +/// references or indices into another vector. /// /// # Example /// /// ```rust -/// use rand::distributions::{Weighted, WeightedChoice, IndependentSample}; +/// use rand::distributions::{Weighted, WeightedChoice, Distribution}; /// /// let mut items = vec!(Weighted { weight: 2, item: 'a' }, /// Weighted { weight: 4, item: 'b' }, @@ -104,7 +92,7 @@ pub struct Weighted { /// let mut rng = rand::thread_rng(); /// for _ in 0..16 { /// // on average prints 'a' 4 times, 'b' 8 and 'c' twice. -/// println!("{}", wc.ind_sample(&mut rng)); +/// println!("{}", wc.sample(&mut rng)); /// } /// ``` pub struct WeightedChoice<'a, T:'a> { @@ -148,18 +136,16 @@ impl<'a, T: Clone> WeightedChoice<'a, T> { } } -impl<'a, T: Clone> Sample for WeightedChoice<'a, T> { - fn sample(&mut self, rng: &mut R) -> T { self.ind_sample(rng) } -} +impl<'a, T: Clone> Distribution for WeightedChoice<'a, T> { + type Output = T; -impl<'a, T: Clone> IndependentSample for WeightedChoice<'a, T> { - fn ind_sample(&self, rng: &mut R) -> T { + fn sample(&self, rng: &mut R) -> T { // we want to find the first element that has cumulative // weight > sample_weight, which we do by binary since the // cumulative weights of self.items are sorted. // choose a weight in [0, total_weight) - let sample_weight = self.weight_range.ind_sample(rng); + let sample_weight = self.weight_range.sample(rng); // short circuit when it's the first item if sample_weight < self.items[0].weight { @@ -265,7 +251,7 @@ fn ziggurat( mod tests { use {Rng, Rand}; - use super::{RandSample, WeightedChoice, Weighted, Sample, IndependentSample}; + use super::{RandDistribution, WeightedChoice, Weighted, Distribution}; #[derive(PartialEq, Debug)] struct ConstRand(usize); @@ -289,10 +275,9 @@ mod tests { #[test] fn test_rand_sample() { - let mut rand_sample = RandSample::::new(); + let rand_sample = RandDistribution::::new(); assert_eq!(rand_sample.sample(&mut ::test::rng()), ConstRand(0)); - assert_eq!(rand_sample.ind_sample(&mut ::test::rng()), ConstRand(0)); } #[test] fn test_weighted_choice() { @@ -310,7 +295,7 @@ mod tests { let mut rng = CountingRng { i: 0 }; for &val in expected.iter() { - assert_eq!(wc.ind_sample(&mut rng), val) + assert_eq!(wc.sample(&mut rng), val) } }} } diff --git a/src/distributions/normal.rs b/src/distributions/normal.rs index 6a410ef5eb3..a44f8c6394d 100644 --- a/src/distributions/normal.rs +++ b/src/distributions/normal.rs @@ -13,7 +13,7 @@ use std::num::Float; use {Rng, Rand, Open01}; -use distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample}; +use distributions::{ziggurat, ziggurat_tables, Distribution}; /// A wrapper around an `f64` to generate N(0, 1) random numbers /// (a.k.a. a standard normal, or Gaussian). @@ -76,11 +76,11 @@ impl Rand for StandardNormal { /// # Example /// /// ```rust -/// use rand::distributions::{Normal, IndependentSample}; +/// use rand::distributions::{Normal, Distribution}; /// /// // mean 2, standard deviation 3 /// let normal = Normal::new(2.0, 3.0); -/// let v = normal.ind_sample(&mut rand::thread_rng()); +/// let v = normal.sample(&mut rand::thread_rng()); /// println!("{} is from a N(2, 9) distribution", v) /// ``` #[derive(Copy)] @@ -104,11 +104,10 @@ impl Normal { } } } -impl Sample for Normal { - fn sample(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) } -} -impl IndependentSample for Normal { - fn ind_sample(&self, rng: &mut R) -> f64 { +impl Distribution for Normal { + type Output = f64; + + fn sample(&self, rng: &mut R) -> f64 { let StandardNormal(n) = rng.gen::(); self.mean + self.std_dev * n } @@ -123,11 +122,11 @@ impl IndependentSample for Normal { /// # Example /// /// ```rust -/// use rand::distributions::{LogNormal, IndependentSample}; +/// use rand::distributions::{LogNormal, Distribution}; /// /// // mean 2, standard deviation 3 /// let log_normal = LogNormal::new(2.0, 3.0); -/// let v = log_normal.ind_sample(&mut rand::thread_rng()); +/// let v = log_normal.sample(&mut rand::thread_rng()); /// println!("{} is from an ln N(2, 9) distribution", v) /// ``` #[derive(Copy)] @@ -147,27 +146,25 @@ impl LogNormal { LogNormal { norm: Normal::new(mean, std_dev) } } } -impl Sample for LogNormal { - fn sample(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) } -} -impl IndependentSample for LogNormal { - fn ind_sample(&self, rng: &mut R) -> f64 { - self.norm.ind_sample(rng).exp() +impl Distribution for LogNormal { + type Output = f64; + + fn sample(&self, rng: &mut R) -> f64 { + self.norm.sample(rng).exp() } } #[cfg(test)] mod tests { - use distributions::{Sample, IndependentSample}; + use distributions::Distribution; use super::{Normal, LogNormal}; #[test] fn test_normal() { - let mut norm = Normal::new(10.0, 10.0); + let norm = Normal::new(10.0, 10.0); let mut rng = ::test::rng(); for _ in 0..1000 { norm.sample(&mut rng); - norm.ind_sample(&mut rng); } } #[test] @@ -179,11 +176,10 @@ mod tests { #[test] fn test_log_normal() { - let mut lnorm = LogNormal::new(10.0, 10.0); + let lnorm = LogNormal::new(10.0, 10.0); let mut rng = ::test::rng(); for _ in 0..1000 { lnorm.sample(&mut rng); - lnorm.ind_sample(&mut rng); } } #[test] @@ -198,13 +194,13 @@ mod bench { extern crate test; use self::test::Bencher; use std::mem::size_of; - use distributions::{Sample}; + use distributions::Distribution; use super::Normal; #[bench] fn rand_normal(b: &mut Bencher) { let mut rng = ::test::weak_rng(); - let mut normal = Normal::new(-2.71828, 3.14159); + let normal = Normal::new(-2.71828, 3.14159); b.iter(|| { for _ in 0..::RAND_BENCH_N { diff --git a/src/distributions/range.rs b/src/distributions/range.rs index 4fe281c327a..20c9edcde56 100644 --- a/src/distributions/range.rs +++ b/src/distributions/range.rs @@ -16,12 +16,12 @@ use std::num::Int; use std::num::wrapping::Wrapping as w; use Rng; -use distributions::{Sample, IndependentSample}; +use distributions::Distribution; /// Sample values uniformly between two bounds. /// /// This gives a uniform distribution (assuming the RNG used to sample -/// it is itself uniform & the `SampleRange` implementation for the +/// it is itself uniform & the `RangeDistribution` implementation for the /// given type is correct), even for edge cases like `low = 0u8`, /// `high = 170u8`, for which a naive modulo operation would return /// numbers less than 85 with double the probability to those greater @@ -35,14 +35,14 @@ use distributions::{Sample, IndependentSample}; /// # Example /// /// ```rust -/// use rand::distributions::{IndependentSample, Range}; +/// use rand::distributions::{Distribution, Range}; /// /// fn main() { /// let between = Range::new(10, 10000); /// let mut rng = rand::thread_rng(); /// let mut sum = 0; /// for _ in 0..1000 { -/// sum += between.ind_sample(&mut rng); +/// sum += between.sample(&mut rng); /// } /// println!("{}", sum); /// } @@ -53,29 +53,27 @@ pub struct Range { accept_zone: X } -impl Range { +impl Range { /// Create a new `Range` instance that samples uniformly from /// `[low, high)`. Panics if `low >= high`. pub fn new(low: X, high: X) -> Range { assert!(low < high, "Range::new called with `low >= high`"); - SampleRange::construct_range(low, high) + RangeDistribution::construct_range(low, high) } } -impl Sample for Range { - #[inline] - fn sample(&mut self, rng: &mut R) -> Sup { self.ind_sample(rng) } -} -impl IndependentSample for Range { - fn ind_sample(&self, rng: &mut R) -> Sup { - SampleRange::sample_range(self, rng) +impl Distribution for Range { + type Output = Sup; + + fn sample(&self, rng: &mut R) -> Sup { + RangeDistribution::sample_range(self, rng) } } /// The helper trait for types that have a sensible way to sample /// uniformly between two values. This should not be used directly, /// and is only to facilitate `Range`. -pub trait SampleRange { +pub trait RangeDistribution { /// Construct the `Range` object that `sample_range` /// requires. This should not ever be called directly, only via /// `Range::new`, which will check that `low < high`, so this @@ -89,7 +87,7 @@ pub trait SampleRange { macro_rules! integer_impl { ($ty:ty, $unsigned:ty) => { - impl SampleRange for $ty { + impl RangeDistribution for $ty { // we play free and fast with unsigned vs signed here // (when $ty is signed), but that's fine, since the // contract of this macro is for $ty and $unsigned to be @@ -143,7 +141,7 @@ integer_impl! { usize, usize } macro_rules! float_impl { ($ty:ty) => { - impl SampleRange for $ty { + impl RangeDistribution for $ty { fn construct_range(low: $ty, high: $ty) -> Range<$ty> { Range { low: low, @@ -164,7 +162,7 @@ float_impl! { f64 } #[cfg(test)] mod tests { use std::num::Int; - use distributions::{Sample, IndependentSample}; + use distributions::Distribution; use super::Range as Range; #[should_panic] @@ -188,12 +186,10 @@ mod tests { (10, 127), (Int::min_value(), Int::max_value())]; for &(low, high) in v.iter() { - let mut sampler: Range<$ty> = Range::new(low, high); + let sampler: Range<$ty> = Range::new(low, high); for _ in 0..1000 { let v = sampler.sample(&mut rng); assert!(low <= v && v < high); - let v = sampler.ind_sample(&mut rng); - assert!(low <= v && v < high); } } )* @@ -214,12 +210,10 @@ mod tests { (1e-35, 1e-25), (-1e35, 1e35)]; for &(low, high) in v.iter() { - let mut sampler: Range<$ty> = Range::new(low, high); + let sampler: Range<$ty> = Range::new(low, high); for _ in 0..1000 { let v = sampler.sample(&mut rng); assert!(low <= v && v < high); - let v = sampler.ind_sample(&mut rng); - assert!(low <= v && v < high); } } )* diff --git a/src/lib.rs b/src/lib.rs index 2e9b494e4f9..d3f6d65ffa6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,7 +94,7 @@ //! and multiply this fraction by 4. //! //! ``` -//! use rand::distributions::{IndependentSample, Range}; +//! use rand::distributions::{Distribution, Range}; //! //! fn main() { //! let between = Range::new(-1f64, 1.); @@ -104,8 +104,8 @@ //! let mut in_circle = 0; //! //! for _ in 0..total { -//! let a = between.ind_sample(&mut rng); -//! let b = between.ind_sample(&mut rng); +//! let a = between.sample(&mut rng); +//! let b = between.sample(&mut rng); //! if a*a + b*b <= 1. { //! in_circle += 1; //! } @@ -137,7 +137,7 @@ //! //! ``` //! use rand::Rng; -//! use rand::distributions::{IndependentSample, Range}; +//! use rand::distributions::{Distribution, Range}; //! //! struct SimulationResult { //! win: bool, @@ -147,10 +147,10 @@ //! // Run a single simulation of the Monty Hall problem. //! fn simulate(random_door: &Range, rng: &mut R) //! -> SimulationResult { -//! let car = random_door.ind_sample(rng); +//! let car = random_door.sample(rng); //! //! // This is our initial choice -//! let mut choice = random_door.ind_sample(rng); +//! let mut choice = random_door.sample(rng); //! //! // The game host opens a door //! let open = game_host_open(car, choice, rng); @@ -247,8 +247,8 @@ use IsaacRng as IsaacWordRng; #[cfg(target_pointer_width = "64")] use Isaac64Rng as IsaacWordRng; -use distributions::{Range, IndependentSample}; -use distributions::range::SampleRange; +use distributions::{Range, Distribution}; +use distributions::range::RangeDistribution; pub mod distributions; pub mod isaac; @@ -438,9 +438,9 @@ pub trait Rng : Sized { /// let m: f64 = rng.gen_range(-40.0f64, 1.3e5f64); /// println!("{}", m); /// ``` - fn gen_range(&mut self, low: T, high: T) -> T { + fn gen_range(&mut self, low: T, high: T) -> T { assert!(low < high, "Rng.gen_range called with low >= high"); - Range::new(low, high).ind_sample(self) + Range::new(low, high).sample(self) } /// Return a bool with a 1 in n chance of true