Trait SampleRange

pub trait SampleRange<T>

Range that supports generating a single sample efficiently.

See RngExt::random_range for examples; the method provides a convenient interface over this type.

Examples

use rand::distr::uniform::SampleRange;
let mut rng = rand::rng();

// Range and InclusiveRange are supported for many types:
assert_eq!((9..10).sample_single(&mut rng), Ok(9));
assert_eq!(('a'..='a').sample_single(&mut rng), Ok('a'));

// RangeTo and RangeToInclusive are supported for unsigned integers:
assert_eq!((..1u8).sample_single(&mut rng), Ok(0));
assert!((..=10u32).sample_single(&mut rng).unwrap() <= 10);

Required Methods

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<T, Error>

Generate a sample from the given range.

fn is_empty(&self) -> bool

Check whether the range is empty.

Implementors