[−][src]Trait rand::seq::IteratorRandom
Extension trait on iterators, providing random sampling methods.
Provided methods
fn choose<R: ?Sized>(self, rng: &mut R) -> Option<Self::Item> where
R: Rng,
R: Rng,
Choose one element at random from the iterator. If you have a slice,
it's significantly faster to call the choose
or choose_mut
functions using the slice instead.
Returns None
if and only if the iterator is empty.
Complexity is O(n)
, where n
is the length of the iterator.
This likely consumes multiple random numbers, but the exact number
is unspecified.
fn choose_multiple_fill<R: ?Sized>(
self,
rng: &mut R,
buf: &mut [Self::Item]
) -> usize where
R: Rng,
self,
rng: &mut R,
buf: &mut [Self::Item]
) -> usize where
R: Rng,
Collects amount
values at random from the iterator into a supplied
buffer.
Although the elements are selected randomly, the order of elements in the buffer is neither stable nor fully random. If random ordering is desired, shuffle the result.
Returns the number of elements added to the buffer. This equals amount
unless the iterator contains insufficient elements, in which case this
equals the number of elements available.
Complexity is O(n)
where n
is the length of the iterator.
fn choose_multiple<R: ?Sized>(
self,
rng: &mut R,
amount: usize
) -> Vec<Self::Item> where
R: Rng,
self,
rng: &mut R,
amount: usize
) -> Vec<Self::Item> where
R: Rng,
Collects amount
values at random from the iterator into a vector.
This is equivalent to choose_multiple_fill
except for the result type.
Although the elements are selected randomly, the order of elements in the buffer is neither stable nor fully random. If random ordering is desired, shuffle the result.
The length of the returned vector equals amount
unless the iterator
contains insufficient elements, in which case it equals the number of
elements available.
Complexity is O(n)
where n
is the length of the iterator.