blob: 3853b40f3b82e58f61e985e944793f67ad7e5874 [file] [log] [blame]
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Extension trait on iterators, providing random sampling methods."><meta name="keywords" content="rust, rustlang, rust-lang, IteratorRandom"><title>IteratorRandom in rand::seq - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="icon" href="https://www.rust-lang.org/favicon.ico"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../../rand/index.html"><div class="logo-container"><img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../rand/index.html"><div class="logo-container">
<img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></div></a><h2 class="location"><a href="#">IteratorRandom</a></h2><div class="sidebar-elems"><section><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.choose">choose</a></li><li><a href="#method.choose_multiple">choose_multiple</a></li><li><a href="#method.choose_multiple_fill">choose_multiple_fill</a></li><li><a href="#method.choose_stable">choose_stable</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In rand::seq</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Trait <a href="../index.html">rand</a>::<wbr><a href="index.html">seq</a>::<wbr><a class="trait" href="#">IteratorRandom</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../src/rand/seq/mod.rs.html#288-489">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust trait"><code>pub trait IteratorRandom: Iterator + Sized {
fn <a href="#method.choose" class="fnname">choose</a>&lt;R&gt;(self, rng: &amp;mut R) -&gt; Option&lt;Self::Item&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?Sized</span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.choose_stable" class="fnname">choose_stable</a>&lt;R&gt;(self, rng: &amp;mut R) -&gt; Option&lt;Self::Item&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?Sized</span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.choose_multiple_fill" class="fnname">choose_multiple_fill</a>&lt;R&gt;(self, rng: &amp;mut R, buf: &amp;mut [Self::Item]) -&gt; usize<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?Sized</span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.choose_multiple" class="fnname">choose_multiple</a>&lt;R&gt;(self, rng: &amp;mut R, amount: usize) -&gt; Vec&lt;Self::Item&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?Sized</span>,
{ ... }
}</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Extension trait on iterators, providing random sampling methods.</p>
<p>This trait is implemented on all iterators <code>I</code> where <code>I: Iterator + Sized</code>
and provides methods for
choosing one or more elements. You must <code>use</code> this trait:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::seq::IteratorRandom;
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::thread_rng();
<span class="kw">let </span>faces = <span class="string">&quot;😀😎😐😕😠😢&quot;</span>;
<span class="macro">println!</span>(<span class="string">&quot;I am {}!&quot;</span>, faces.chars().choose(<span class="kw-2">&amp;mut </span>rng).unwrap());</code></pre></div>
<p>Example output (non-deterministic):</p>
<div class="example-wrap"><pre class="language-none"><code>I am 😀!</code></pre></div></div></details><h2 id="provided-methods" class="small-section-header">Provided Methods<a href="#provided-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.choose" class="method has-srclink"><a class="srclink rightside" href="../../src/rand/seq/mod.rs.html#304-353">source</a><h4 class="code-header">fn <a href="#method.choose" class="fnname">choose</a>&lt;R&gt;(self, rng: &amp;mut R) -&gt; Option&lt;Self::Item&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?Sized,</span></h4></section></summary><div class="docblock"><p>Choose one element at random from the iterator.</p>
<p>Returns <code>None</code> if and only if the iterator is empty.</p>
<p>This method uses [<code>Iterator::size_hint</code>] for optimisation. With an
accurate hint and where [<code>Iterator::nth</code>] is a constant-time operation
this method can offer <code>O(1)</code> performance. Where no size hint is
available, complexity is <code>O(n)</code> where <code>n</code> is the iterator length.
Partial hints (where <code>lower &gt; 0</code>) also improve performance.</p>
<p>Note that the output values and the number of RNG samples used
depends on size hints. In particular, <code>Iterator</code> combinators that don’t
change the values yielded but change the size hints may result in
<code>choose</code> returning different elements. If you want consistent results
and RNG usage consider using <a href="trait.IteratorRandom.html#method.choose_stable" title="IteratorRandom::choose_stable"><code>IteratorRandom::choose_stable</code></a>.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.choose_stable" class="method has-srclink"><a class="srclink rightside" href="../../src/rand/seq/mod.rs.html#373-411">source</a><h4 class="code-header">fn <a href="#method.choose_stable" class="fnname">choose_stable</a>&lt;R&gt;(self, rng: &amp;mut R) -&gt; Option&lt;Self::Item&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?Sized,</span></h4></section></summary><div class="docblock"><p>Choose one element at random from the iterator.</p>
<p>Returns <code>None</code> if and only if the iterator is empty.</p>
<p>This method is very similar to <a href="trait.IteratorRandom.html#method.choose"><code>choose</code></a> except that the result
only depends on the length of the iterator and the values produced by
<code>rng</code>. Notably for any iterator of a given length this will make the
same requests to <code>rng</code> and if the same sequence of values are produced
the same index will be selected from <code>self</code>. This may be useful if you
need consistent results no matter what type of iterator you are working
with. If you do not need this stability prefer <a href="trait.IteratorRandom.html#method.choose"><code>choose</code></a>.</p>
<p>Note that this method still uses [<code>Iterator::size_hint</code>] to skip
constructing elements where possible, however the selection and <code>rng</code>
calls are the same in the face of this optimization. If you want to
force every element to be created regardless call <code>.inspect(|e| ())</code>.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.choose_multiple_fill" class="method has-srclink"><a class="srclink rightside" href="../../src/rand/seq/mod.rs.html#426-448">source</a><h4 class="code-header">fn <a href="#method.choose_multiple_fill" class="fnname">choose_multiple_fill</a>&lt;R&gt;(self, rng: &amp;mut R, buf: &amp;mut [Self::Item]) -&gt; usize<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?Sized,</span></h4></section></summary><div class="docblock"><p>Collects values at random from the iterator into a supplied buffer
until that buffer is filled.</p>
<p>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.</p>
<p>Returns the number of elements added to the buffer. This equals the length
of the buffer unless the iterator contains insufficient elements, in which
case this equals the number of elements available.</p>
<p>Complexity is <code>O(n)</code> where <code>n</code> is the length of the iterator.
For slices, prefer <a href="trait.SliceRandom.html#tymethod.choose_multiple" title="SliceRandom::choose_multiple"><code>SliceRandom::choose_multiple</code></a>.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.choose_multiple" class="method has-srclink"><a class="srclink rightside" href="../../src/rand/seq/mod.rs.html#466-488">source</a><h4 class="code-header">fn <a href="#method.choose_multiple" class="fnname">choose_multiple</a>&lt;R&gt;(self, rng: &amp;mut R, amount: usize) -&gt; Vec&lt;Self::Item&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?Sized,</span></h4></section></summary><div class="docblock"><p>Collects <code>amount</code> values at random from the iterator into a vector.</p>
<p>This is equivalent to <code>choose_multiple_fill</code> except for the result type.</p>
<p>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.</p>
<p>The length of the returned vector equals <code>amount</code> unless the iterator
contains insufficient elements, in which case it equals the number of
elements available.</p>
<p>Complexity is <code>O(n)</code> where <code>n</code> is the length of the iterator.
For slices, prefer <a href="trait.SliceRandom.html#tymethod.choose_multiple" title="SliceRandom::choose_multiple"><code>SliceRandom::choose_multiple</code></a>.</p>
</div></details></div><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div id="implementors-list"><section id="impl-IteratorRandom-for-I" class="impl has-srclink"><a class="srclink rightside" href="../../src/rand/seq/mod.rs.html#615">source</a><a href="#impl-IteratorRandom-for-I" class="anchor"></a><h3 class="code-header">impl&lt;I&gt; <a class="trait" href="trait.IteratorRandom.html" title="trait rand::seq::IteratorRandom">IteratorRandom</a> for I<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;I: Iterator + Sized,</span></h3></section></div><script src="../../implementors/rand/seq/trait.IteratorRandom.js" async></script></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="rand" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>