Struct Choose

pub struct Choose<'a, T> { /* private fields */ }

A distribution to uniformly sample elements of a slice

Like IndexedRandom::choose, this uniformly samples elements of a slice without modification of the slice (so called "sampling with replacement"). This distribution object may be a little faster for repeated sampling (but slower for small numbers of samples).

Examples

Since this is a distribution, Rng::sample_iter and Distribution::sample_iter may be used, for example:

use rand::distr::{Distribution, slice::Choose};

let vowels = ['a', 'e', 'i', 'o', 'u'];
let vowels_dist = Choose::new(&vowels).unwrap();

// build a string of 10 vowels
let vowel_string: String = vowels_dist
    .sample_iter(&mut rand::rng())
    .take(10)
    .collect();

println!("{}", vowel_string);
assert_eq!(vowel_string.len(), 10);
assert!(vowel_string.chars().all(|c| vowels.contains(&c)));

For a single sample, IndexedRandom::choose may be preferred:

use rand::seq::IndexedRandom;

let vowels = ['a', 'e', 'i', 'o', 'u'];
let mut rng = rand::rng();

println!("{}", vowels.choose(&mut rng).unwrap());

Implementations

impl<'a, T> Choose<'a, T>

fn new(slice: &'a [T]) -> Result<Self, Empty>

Create a new Choose instance which samples uniformly from the slice.

Returns error Empty if the slice is empty.

fn num_choices(&self) -> NonZeroUsize

Returns the count of choices in this distribution

Trait Implementations

impl SampleString for Choose<'_, char>

fn append_string<R: Rng + ?Sized>(&self, rng: &mut R, string: &mut String, len: usize)

impl<'a, T> Distribution<&'a T> for Choose<'a, T>

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> &'a T

impl<'a, T: Clone> Clone for Choose<'a, T>

fn clone(&self) -> Self

impl<'a, T: Copy> Copy for Choose<'a, T>

impl<'a, T: Debug> Debug for Choose<'a, T>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Auto Trait Implementations

impl<'a, T> Freeze for Choose<'a, T> where &'a [T]: Freeze,

impl<'a, T> RefUnwindSafe for Choose<'a, T> where &'a [T]: RefUnwindSafe,

impl<'a, T> Send for Choose<'a, T> where &'a [T]: Send,

impl<'a, T> Sync for Choose<'a, T> where &'a [T]: Sync,

impl<'a, T> Unpin for Choose<'a, T> where &'a [T]: Unpin,

impl<'a, T> UnsafeUnpin for Choose<'a, T> where &'a [T]: UnsafeUnpin,

impl<'a, T> UnwindSafe for Choose<'a, T> where &'a [T]: UnwindSafe,

Blanket Implementations

impl<T> Any for Choose<'a, T> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Choose<'a, T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Choose<'a, T> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for Choose<'a, T> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for Choose<'a, T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Choose<'a, T> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Choose<'a, T> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Choose<'a, T> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Choose<'a, T> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>