Skip to content

Commit 0df5fd1

Browse files
committed
Rename Sample → Distribution. Make WeightedChoice own its arguments.
The change to WeightedChoice is debatable, but since the argument must normally be constructed specially anyway, the reference model makes little sense. See issues rust-random#90, rust-random#142 and PR rust-random#152.
1 parent 92ee778 commit 0df5fd1

File tree

11 files changed

+73
-61
lines changed

11 files changed

+73
-61
lines changed

benches/distributions/exponential.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::mem::size_of;
22
use test::Bencher;
33
use rand;
44
use rand::dist::exponential::Exp;
5-
use rand::dist::Sample;
5+
use rand::dist::Distribution;
66

77
#[bench]
88
fn rand_exp(b: &mut Bencher) {

benches/distributions/gamma.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::mem::size_of;
22
use test::Bencher;
33
use rand;
4-
use rand::dist::Sample;
4+
use rand::dist::Distribution;
55
use rand::dist::gamma::Gamma;
66

77
#[bench]

benches/distributions/normal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::mem::size_of;
22
use test::Bencher;
33
use rand;
4-
use rand::dist::Sample;
4+
use rand::dist::Distribution;
55
use rand::dist::normal::Normal;
66

77
#[bench]

src/dist/exponential.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! The exponential distribution.
1212
1313
use {Rng};
14-
use dist::{ziggurat, ziggurat_tables, Sample, uniform01};
14+
use dist::{ziggurat, ziggurat_tables, Distribution, uniform01};
1515

1616
/// Generates Exp(1) random numbers.
1717
///
@@ -60,7 +60,7 @@ pub fn exp1<R: Rng>(rng: &mut R) -> f64 {
6060
/// # Example
6161
///
6262
/// ```rust
63-
/// use rand::dist::{Exp, Sample};
63+
/// use rand::dist::{Exp, Distribution};
6464
///
6565
/// let exp = Exp::new(2.0);
6666
/// let v = exp.sample(&mut rand::thread_rng());
@@ -82,15 +82,15 @@ impl Exp {
8282
}
8383
}
8484

85-
impl Sample<f64> for Exp {
85+
impl Distribution<f64> for Exp {
8686
fn sample<R: Rng>(&self, rng: &mut R) -> f64 {
8787
exp1(rng) * self.lambda_inverse
8888
}
8989
}
9090

9191
#[cfg(test)]
9292
mod test {
93-
use dist::{Sample};
93+
use dist::{Distribution};
9494
use super::Exp;
9595

9696
#[test]

src/dist/gamma.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use self::ChiSquaredRepr::*;
1717

1818
use {Rng};
1919
use dist::normal::standard_normal;
20-
use dist::{Sample, Exp, open01};
20+
use dist::{Distribution, Exp, open01};
2121

2222
/// The Gamma distribution `Gamma(shape, scale)` distribution.
2323
///
@@ -38,7 +38,7 @@ use dist::{Sample, Exp, open01};
3838
/// # Example
3939
///
4040
/// ```rust
41-
/// use rand::dist::{Sample, Gamma};
41+
/// use rand::dist::{Distribution, Gamma};
4242
///
4343
/// let gamma = Gamma::new(2.0, 5.0);
4444
/// let v = gamma.sample(&mut rand::thread_rng());
@@ -134,7 +134,7 @@ impl GammaLargeShape {
134134
}
135135

136136

137-
impl Sample<f64> for Gamma {
137+
impl Distribution<f64> for Gamma {
138138
fn sample<R: Rng>(&self, rng: &mut R) -> f64 {
139139
match self.repr {
140140
Small(ref g) => g.sample(rng),
@@ -143,14 +143,14 @@ impl Sample<f64> for Gamma {
143143
}
144144
}
145145
}
146-
impl Sample<f64> for GammaSmallShape {
146+
impl Distribution<f64> for GammaSmallShape {
147147
fn sample<R: Rng>(&self, rng: &mut R) -> f64 {
148148
let u: f64 = open01(rng);
149149

150150
self.large_shape.sample(rng) * u.powf(self.inv_shape)
151151
}
152152
}
153-
impl Sample<f64> for GammaLargeShape {
153+
impl Distribution<f64> for GammaLargeShape {
154154
fn sample<R: Rng>(&self, rng: &mut R) -> f64 {
155155
loop {
156156
let x = standard_normal(rng);
@@ -182,7 +182,7 @@ impl Sample<f64> for GammaLargeShape {
182182
/// # Example
183183
///
184184
/// ```rust
185-
/// use rand::dist::{ChiSquared, Sample};
185+
/// use rand::dist::{ChiSquared, Distribution};
186186
///
187187
/// let chi = ChiSquared::new(11.0);
188188
/// let v = chi.sample(&mut rand::thread_rng());
@@ -215,7 +215,7 @@ impl ChiSquared {
215215
ChiSquared { repr: repr }
216216
}
217217
}
218-
impl Sample<f64> for ChiSquared {
218+
impl Distribution<f64> for ChiSquared {
219219
fn sample<R: Rng>(&self, rng: &mut R) -> f64 {
220220
match self.repr {
221221
DoFExactlyOne => {
@@ -237,7 +237,7 @@ impl Sample<f64> for ChiSquared {
237237
/// # Example
238238
///
239239
/// ```rust
240-
/// use rand::dist::{FisherF, Sample};
240+
/// use rand::dist::{FisherF, Distribution};
241241
///
242242
/// let f = FisherF::new(2.0, 32.0);
243243
/// let v = f.sample(&mut rand::thread_rng());
@@ -266,7 +266,7 @@ impl FisherF {
266266
}
267267
}
268268
}
269-
impl Sample<f64> for FisherF {
269+
impl Distribution<f64> for FisherF {
270270
fn sample<R: Rng>(&self, rng: &mut R) -> f64 {
271271
self.numer.sample(rng) / self.denom.sample(rng) * self.dof_ratio
272272
}
@@ -278,7 +278,7 @@ impl Sample<f64> for FisherF {
278278
/// # Example
279279
///
280280
/// ```rust
281-
/// use rand::dist::{StudentT, Sample};
281+
/// use rand::dist::{StudentT, Distribution};
282282
///
283283
/// let t = StudentT::new(11.0);
284284
/// let v = t.sample(&mut rand::thread_rng());
@@ -301,7 +301,7 @@ impl StudentT {
301301
}
302302
}
303303
}
304-
impl Sample<f64> for StudentT {
304+
impl Distribution<f64> for StudentT {
305305
fn sample<R: Rng>(&self, rng: &mut R) -> f64 {
306306
let norm = standard_normal(rng);
307307
norm * (self.dof / self.chi.sample(rng)).sqrt()
@@ -310,7 +310,7 @@ impl Sample<f64> for StudentT {
310310

311311
#[cfg(test)]
312312
mod test {
313-
use dist::{Sample};
313+
use dist::{Distribution};
314314
use super::{ChiSquared, StudentT, FisherF};
315315

316316
#[test]

src/dist/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//!
1313
//! A distribution may have internal state describing the distribution of
1414
//! generated values; for example `Range` needs to know its upper and lower
15-
//! bounds. Distributions use the `Sample` trait to yield values: call
15+
//! bounds. Distributions use the `Distribution` trait to yield values: call
1616
//! `dist.sample(&mut rng)` to get a random variable.
1717
//!
1818
//! TODO: is it worth exposing both submodules and re-exporting their members?
@@ -33,7 +33,7 @@ pub mod exponential;
3333
pub mod weighted;
3434

3535
/// Types (distributions) that can be used to create a random instance of `T`.
36-
pub trait Sample<T> {
36+
pub trait Distribution<T> {
3737
/// Generate a random value of `T`, using `rng` as the
3838
/// source of randomness.
3939
fn sample<R: Rng>(&self, rng: &mut R) -> T;

src/dist/normal.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! The normal and derived distributions.
1212
1313
use {Rng};
14-
use dist::{ziggurat, ziggurat_tables, Sample, open01};
14+
use dist::{ziggurat, ziggurat_tables, Distribution, open01};
1515

1616
/// Generates N(0, 1) random numbers
1717
/// (a.k.a. a standard normal, or Gaussian).
@@ -76,7 +76,7 @@ pub fn standard_normal<R:Rng>(rng: &mut R) -> f64 {
7676
/// # Example
7777
///
7878
/// ```rust
79-
/// use rand::dist::{Normal, Sample};
79+
/// use rand::dist::{Normal, Distribution};
8080
///
8181
/// // mean 2, standard deviation 3
8282
/// let normal = Normal::new(2.0, 3.0);
@@ -105,7 +105,7 @@ impl Normal {
105105
}
106106
}
107107
}
108-
impl Sample<f64> for Normal {
108+
impl Distribution<f64> for Normal {
109109
fn sample<R: Rng>(&self, rng: &mut R) -> f64 {
110110
self.mean + self.std_dev * standard_normal(rng)
111111
}
@@ -120,7 +120,7 @@ impl Sample<f64> for Normal {
120120
/// # Example
121121
///
122122
/// ```rust
123-
/// use rand::dist::{LogNormal, Sample};
123+
/// use rand::dist::{LogNormal, Distribution};
124124
///
125125
/// // mean 2, standard deviation 3
126126
/// let log_normal = LogNormal::new(2.0, 3.0);
@@ -145,15 +145,15 @@ impl LogNormal {
145145
LogNormal { norm: Normal::new(mean, std_dev) }
146146
}
147147
}
148-
impl Sample<f64> for LogNormal {
148+
impl Distribution<f64> for LogNormal {
149149
fn sample<R: Rng>(&self, rng: &mut R) -> f64 {
150150
self.norm.sample(rng).exp()
151151
}
152152
}
153153

154154
#[cfg(test)]
155155
mod tests {
156-
use dist::{Sample};
156+
use dist::{Distribution};
157157
use super::{Normal, LogNormal};
158158

159159
#[test]

src/dist/range.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use std::num::Wrapping as w;
1616

1717
use Rng;
18-
use dist::{Sample, uniform01};
18+
use dist::{Distribution, uniform01};
1919

2020
/// Sample values uniformly between two bounds.
2121
///
@@ -34,7 +34,7 @@ use dist::{Sample, uniform01};
3434
/// # Example
3535
///
3636
/// ```rust
37-
/// use rand::dist::{Sample, Range};
37+
/// use rand::dist::{Distribution, Range};
3838
///
3939
/// fn main() {
4040
/// let between = Range::new(10, 10000);
@@ -62,7 +62,7 @@ impl<X: SampleRange + PartialOrd> Range<X> {
6262
}
6363
}
6464

65-
impl<T: SampleRange> Sample<T> for Range<T> {
65+
impl<T: SampleRange> Distribution<T> for Range<T> {
6666
fn sample<R: Rng>(&self, rng: &mut R) -> T {
6767
SampleRange::sample_range(self, rng)
6868
}
@@ -162,7 +162,7 @@ float_impl! { f64 }
162162

163163
#[cfg(test)]
164164
mod tests {
165-
use dist::{Sample};
165+
use dist::{Distribution};
166166
use super::Range as Range;
167167

168168
#[should_panic]

src/dist/uniform.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
//! Generating uniformly distributed numbers
1212
1313
use std::char;
14+
use std::fmt;
1415
use std::mem;
1516
use std::marker::PhantomData;
1617

1718
use Rng;
18-
use dist::Sample;
19+
use dist::Distribution;
1920

2021
// ----- convenience functions -----
2122

@@ -71,13 +72,13 @@ pub fn codepoint<R: Rng>(rng: &mut R) -> char {
7172
}
7273

7374

74-
// ----- Sample implementations -----
75+
// ----- Distribution implementations -----
7576
// TODO: do we want these? If so, implement for other ranges.
7677

7778
/// Sample values uniformly over the whole range supported by the type.
7879
///
7980
/// No internal state.
80-
#[derive(Clone, Copy, Debug, Default)]
81+
#[derive(Default)]
8182
pub struct Uniform<T: SampleUniform> {
8283
_marker: PhantomData<T>,
8384
}
@@ -92,7 +93,23 @@ impl<T: SampleUniform> Uniform<T> {
9293
}
9394
}
9495

95-
impl<T: SampleUniform> Sample<T> for Uniform<T> {
96+
impl<T: SampleUniform> Copy for Uniform<T> {}
97+
98+
// derive only auto-impls for types T: Clone, but we don't have that restriction
99+
impl<T: SampleUniform> Clone for Uniform<T> {
100+
fn clone(&self) -> Self {
101+
Uniform::new()
102+
}
103+
}
104+
105+
// derive only auto-impls for types T: Debug, but we don't have that restriction
106+
impl<T: SampleUniform> fmt::Debug for Uniform<T> {
107+
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
108+
write!(f, "Uniform {{}}")
109+
}
110+
}
111+
112+
impl<T: SampleUniform> Distribution<T> for Uniform<T> {
96113
fn sample<R: Rng>(&self, rng: &mut R) -> T {
97114
T::sample_uniform(rng)
98115
}
@@ -267,7 +284,7 @@ float_impls! { SCALE_F32, f32, 24, next_f32 }
267284
#[cfg(test)]
268285
mod tests {
269286
use {Rng, thread_rng};
270-
use dist::{uniform, Sample};
287+
use dist::{uniform, Distribution};
271288
use dist::uniform::{SampleUniform, Uniform};
272289
use dist::{uniform01, open01, closed01};
273290

0 commit comments

Comments
 (0)