pub trait SampleRange<T> {
// Required methods
fn sample_single<R>(self, rng: &mut R) -> Result<T, Error>
where R: Rng + ?Sized;
fn is_empty(&self) -> bool;
}Expand description
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§
Sourcefn sample_single<R>(self, rng: &mut R) -> Result<T, Error>
fn sample_single<R>(self, rng: &mut R) -> Result<T, Error>
Generate a sample from the given range.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.