Skip to content

Commit

Permalink
Implement Distribution for RangeTo(Inclusive) for unsigned ints
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralith committed Jul 2, 2019
1 parent b25b657 commit 64af289
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/distributions/uniform.rs
Expand Up @@ -111,7 +111,7 @@
use std::time::Duration;
#[cfg(not(feature = "std"))]
use core::time::Duration;
use core::ops::{Range, RangeInclusive};
use core::ops::{Range, RangeInclusive, RangeTo, RangeToInclusive};

use crate::Rng;
use crate::distributions::Distribution;
Expand Down Expand Up @@ -306,6 +306,28 @@ impl<T: SampleUniform> Distribution<T> for RangeInclusive<T> {
}
}

macro_rules! impl_distrib_range_to {
($($ty:ty),*) => {
$(
impl Distribution<$ty> for RangeTo<$ty> {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $ty {
<$ty as SampleUniform>::Sampler::sample_single(0, self.end, rng)
}
}
impl Distribution<$ty> for RangeToInclusive<$ty> {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $ty {
<$ty as SampleUniform>::Sampler::sample_single_inclusive(0, self.end, rng)
}
}
)*
}
}

impl_distrib_range_to!(usize, u8, u16, u32, u64);
#[cfg(not(target_os = "emscripten"))]
impl_distrib_range_to!(u128);


/// Helper trait similar to [`Borrow`] but implemented
/// only for SampleUniform and references to SampleUniform in
/// order to resolve ambiguity issues.
Expand Down Expand Up @@ -1294,10 +1316,14 @@ mod tests {
fn test_std_range_distribution() {
let mut rng = crate::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);
let x = rng.sample(-5..5);
assert!(x >= -5 && x < 5);
let x = rng.sample(-5..=5);
assert!(x >= -5 && x <= 5);
let x = rng.sample(..10u8);
assert!(x < 10);
let x = rng.sample(..=10u8);
assert!(x <= 10);
}
}

Expand Down

0 comments on commit 64af289

Please sign in to comment.