diff --git a/src/distributions/uniform.rs b/src/distributions/uniform.rs index e08de821e7..2c34d77faa 100644 --- a/src/distributions/uniform.rs +++ b/src/distributions/uniform.rs @@ -290,6 +290,19 @@ impl From<::core::ops::RangeInclusive> for Uniform { } } +impl Distribution for ::core::ops::Range { + fn sample(&self, rng: &mut R) -> T { + T::Sampler::sample_single(&self.start, &self.end, rng) + } +} + +#[cfg(rustc_1_27)] +impl Distribution for ::core::ops::RangeInclusive { + fn sample(&self, rng: &mut R) -> T { + Uniform::new_inclusive(self.start(), self.end()).sample(rng) + } +} + /// Helper trait similar to [`Borrow`] but implemented /// only for SampleUniform and references to SampleUniform in /// order to resolve ambiguity issues. @@ -1279,6 +1292,17 @@ mod tests { assert_eq!(r.inner.scale, 5.0); } + #[test] + fn test_std_range_distribution() { + let mut rng = ::test::rng(474); + for _ in 0..100 { + let x = rng.sample(0..10); + assert!(x >= 0 && x < 10); + let x = rng.sample(0..=10); + assert!(x >= 0 && x <= 10); + } + } + #[cfg(rustc_1_27)] #[test] fn test_uniform_from_std_range_inclusive() {