1// Copyright 2021 Developers of the Rand project.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
89use crate::distributions::{Distribution, Uniform};
1011/// A distribution to sample items uniformly from a slice.
12///
13/// [`Slice::new`] constructs a distribution referencing a slice and uniformly
14/// samples references from the items in the slice. It may do extra work up
15/// front to make sampling of multiple values faster; if only one sample from
16/// the slice is required, [`SliceRandom::choose`] can be more efficient.
17///
18/// Steps are taken to avoid bias which might be present in naive
19/// implementations; for example `slice[rng.gen() % slice.len()]` samples from
20/// the slice, but may be more likely to select numbers in the low range than
21/// other values.
22///
23/// This distribution samples with replacement; each sample is independent.
24/// Sampling without replacement requires state to be retained, and therefore
25/// cannot be handled by a distribution; you should instead consider methods
26/// on [`SliceRandom`], such as [`SliceRandom::choose_multiple`].
27///
28/// # Example
29///
30/// ```
31/// use rand::Rng;
32/// use rand::distributions::Slice;
33///
34/// let vowels = ['a', 'e', 'i', 'o', 'u'];
35/// let vowels_dist = Slice::new(&vowels).unwrap();
36/// let rng = rand::thread_rng();
37///
38/// // build a string of 10 vowels
39/// let vowel_string: String = rng
40/// .sample_iter(&vowels_dist)
41/// .take(10)
42/// .collect();
43///
44/// println!("{}", vowel_string);
45/// assert_eq!(vowel_string.len(), 10);
46/// assert!(vowel_string.chars().all(|c| vowels.contains(&c)));
47/// ```
48///
49/// For a single sample, [`SliceRandom::choose`][crate::seq::SliceRandom::choose]
50/// may be preferred:
51///
52/// ```
53/// use rand::seq::SliceRandom;
54///
55/// let vowels = ['a', 'e', 'i', 'o', 'u'];
56/// let mut rng = rand::thread_rng();
57///
58/// println!("{}", vowels.choose(&mut rng).unwrap())
59/// ```
60///
61/// [`SliceRandom`]: crate::seq::SliceRandom
62/// [`SliceRandom::choose`]: crate::seq::SliceRandom::choose
63/// [`SliceRandom::choose_multiple`]: crate::seq::SliceRandom::choose_multiple
64#[derive(#[automatically_derived]
impl<'a, T: ::core::fmt::Debug> ::core::fmt::Debug for Slice<'a, T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "Slice",
"slice", &self.slice, "range", &&self.range)
}
}Debug, #[automatically_derived]
impl<'a, T: ::core::clone::Clone> ::core::clone::Clone for Slice<'a, T> {
#[inline]
fn clone(&self) -> Slice<'a, T> {
Slice {
slice: ::core::clone::Clone::clone(&self.slice),
range: ::core::clone::Clone::clone(&self.range),
}
}
}Clone, #[automatically_derived]
impl<'a, T: ::core::marker::Copy> ::core::marker::Copy for Slice<'a, T> { }Copy)]
65pub struct Slice<'a, T> {
66 slice: &'a [T],
67 range: Uniform<usize>,
68}
6970impl<'a, T> Slice<'a, T> {
71/// Create a new `Slice` instance which samples uniformly from the slice.
72 /// Returns `Err` if the slice is empty.
73pub fn new(slice: &'a [T]) -> Result<Self, EmptySlice> {
74match slice.len() {
750 => Err(EmptySlice),
76 len => Ok(Self {
77slice,
78 range: Uniform::new(0, len),
79 }),
80 }
81 }
82}
8384impl<'a, T> Distribution<&'a T> for Slice<'a, T> {
85fn sample<R: crate::Rng + ?Sized>(&self, rng: &mut R) -> &'a T {
86let idx = self.range.sample(rng);
8788if true {
if !(idx < self.slice.len()) {
{
::core::panicking::panic_fmt(format_args!("Uniform::new(0, {0}) somehow returned {1}",
self.slice.len(), idx));
}
};
};debug_assert!(
89 idx < self.slice.len(),
90"Uniform::new(0, {}) somehow returned {}",
91self.slice.len(),
92 idx
93 );
9495// Safety: at construction time, it was ensured that the slice was
96 // non-empty, and that the `Uniform` range produces values in range
97 // for the slice
98unsafe { self.slice.get_unchecked(idx) }
99 }
100}
101102/// Error type indicating that a [`Slice`] distribution was improperly
103/// constructed with an empty slice.
104#[derive(#[automatically_derived]
impl ::core::fmt::Debug for EmptySlice {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "EmptySlice")
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for EmptySlice {
#[inline]
fn clone(&self) -> EmptySlice { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for EmptySlice { }Copy)]
105pub struct EmptySlice;
106107impl core::fmt::Displayfor EmptySlice {
108fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
109f.write_fmt(format_args!("Tried to create a `distributions::Slice` with an empty slice"))write!(
110f,
111"Tried to create a `distributions::Slice` with an empty slice"
112)113 }
114}
115116#[cfg(feature = "std")]
117impl std::error::Error for EmptySlice {}