mpvrc/rand/seq/trait.IteratorRandom.html

77 lines
No EOL
18 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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."><title>IteratorRandom in rand::seq - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="rand" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.1 (e71f9a9a9 2025-01-27)" data-channel="1.84.1" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-893ab5e7.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" title="show sidebar"></button><a class="logo-container" href="../../rand/index.html"><img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../../rand/index.html"><img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></a><h2><a href="../../rand/index.html">rand</a><span class="version">0.8.5</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Iterator<wbr>Random</a></h2><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.choose" title="choose">choose</a></li><li><a href="#method.choose_multiple" title="choose_multiple">choose_multiple</a></li><li><a href="#method.choose_multiple_fill" title="choose_multiple_fill">choose_multiple_fill</a></li><li><a href="#method.choose_stable" title="choose_stable">choose_stable</a></li></ul><h3><a href="#dyn-compatibility">Dyn Compatibility</a></h3><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2><a href="index.html">In rand::<wbr>seq</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="../index.html">rand</a>::<wbr><a href="index.html">seq</a></span><h1>Trait <span class="trait">IteratorRandom</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/rand/seq/mod.rs.html#288-489">Source</a> </span></div><pre class="rust item-decl"><code>pub trait IteratorRandom: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html" title="trait core::iter::traits::iterator::Iterator">Iterator</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> {
// Provided methods
fn <a href="#method.choose" class="fn">choose</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&amp;mut R</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>&gt;
<span class="where">where R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
<span class="item-spacer"></span> fn <a href="#method.choose_stable" class="fn">choose_stable</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&amp;mut R</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>&gt;
<span class="where">where R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
<span class="item-spacer"></span> fn <a href="#method.choose_multiple_fill" class="fn">choose_multiple_fill</a>&lt;R&gt;(
self,
rng: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&amp;mut R</a>,
buf: &amp;mut [Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>],
) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>
<span class="where">where R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
<span class="item-spacer"></span> fn <a href="#method.choose_multiple" class="fn">choose_multiple</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&amp;mut R</a>, amount: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>) -&gt; <a class="struct" href="https://doc.rust-lang.org/1.84.1/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>&gt;
<span class="where">where R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
}</code></pre><details class="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">"😀😎😐😕😠😢"</span>;
<span class="macro">println!</span>(<span class="string">"I am {}!"</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="section-header">Provided Methods<a href="#provided-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="method.choose" class="method"><a class="src rightside" href="../../src/rand/seq/mod.rs.html#304-353">Source</a><h4 class="code-header">fn <a href="#method.choose" class="fn">choose</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&amp;mut R</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>&gt;<div class="where">where
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></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 <a href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html#method.size_hint" title="method core::iter::traits::iterator::Iterator::size_hint"><code>Iterator::size_hint</code></a> for optimisation. With an
accurate hint and where <a href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html#method.nth" title="method core::iter::traits::iterator::Iterator::nth"><code>Iterator::nth</code></a> 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 dont
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="method rand::seq::IteratorRandom::choose_stable"><code>IteratorRandom::choose_stable</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.choose_stable" class="method"><a class="src rightside" href="../../src/rand/seq/mod.rs.html#373-411">Source</a><h4 class="code-header">fn <a href="#method.choose_stable" class="fn">choose_stable</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&amp;mut R</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.84.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>&gt;<div class="where">where
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></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" title="method rand::seq::IteratorRandom::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" title="method rand::seq::IteratorRandom::choose"><code>choose</code></a>.</p>
<p>Note that this method still uses <a href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html#method.size_hint" title="method core::iter::traits::iterator::Iterator::size_hint"><code>Iterator::size_hint</code></a> 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="toggle method-toggle" open><summary><section id="method.choose_multiple_fill" class="method"><a class="src rightside" href="../../src/rand/seq/mod.rs.html#426-448">Source</a><h4 class="code-header">fn <a href="#method.choose_multiple_fill" class="fn">choose_multiple_fill</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&amp;mut R</a>, buf: &amp;mut [Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>]) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a><div class="where">where
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></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="method rand::seq::SliceRandom::choose_multiple"><code>SliceRandom::choose_multiple</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.choose_multiple" class="method"><a class="src rightside" href="../../src/rand/seq/mod.rs.html#466-488">Source</a><h4 class="code-header">fn <a href="#method.choose_multiple" class="fn">choose_multiple</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.reference.html">&amp;mut R</a>, amount: <a class="primitive" href="https://doc.rust-lang.org/1.84.1/std/primitive.usize.html">usize</a>) -&gt; <a class="struct" href="https://doc.rust-lang.org/1.84.1/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>&gt;<div class="where">where
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></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="method rand::seq::SliceRandom::choose_multiple"><code>SliceRandom::choose_multiple</code></a>.</p>
</div></details></div><h2 id="dyn-compatibility" class="section-header">Dyn Compatibility<a href="#dyn-compatibility" class="anchor">§</a></h2><div class="dyn-compatibility-info"><p>This trait is <b>not</b> <a href="https://doc.rust-lang.org/1.84.1/reference/items/traits.html#object-safety">dyn compatible</a>.</p><p><i>In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.</i></p></div><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-IteratorRandom-for-I" class="impl"><a class="src 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<div class="where">where
I: <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/iter/traits/iterator/trait.Iterator.html" title="trait core::iter::traits::iterator::Iterator">Iterator</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></div><script src="../../trait.impl/rand/seq/trait.IteratorRandom.js" async></script></section></div></main></body></html>