Files
GopherGate/target/doc/rand/seq/trait.IndexedRandom.html
2026-02-26 12:00:21 -05:00

153 lines
33 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 indexable lists, providing random sampling methods."><title>IndexedRandom in rand::seq - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.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"href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-ca0dd0c4.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.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-263c88ec.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]--><rustdoc-topbar><h2><a href="#">IndexedRandom</a></h2></rustdoc-topbar><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.9.2</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Indexed<wbr>Random</a></h2><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.len" title="len">len</a></li></ul><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_array" title="choose_multiple_array">choose_multiple_array</a></li><li><a href="#method.choose_multiple_weighted" title="choose_multiple_weighted">choose_multiple_weighted</a></li><li><a href="#method.choose_weighted" title="choose_weighted">choose_weighted</a></li><li><a href="#method.is_empty" title="is_empty">is_empty</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-IndexedRandom-for-%5BT%5D" title="[T]">[T]</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" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../index.html">rand</a>::<wbr><a href="index.html">seq</a></div><h1>Trait <span class="trait">Indexed<wbr>Random</span>&nbsp;<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/slice.rs.html#25-236">Source</a> </span></div><pre class="rust item-decl"><code>pub trait IndexedRandom: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html" title="trait core::ops::index::Index">Index</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>&gt; {
// Required method
fn <a href="#tymethod.len" class="fn">len</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>;
// Provided methods
fn <a href="#method.is_empty" class="fn">is_empty</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a> { ... }
<span class="item-spacer"></span> fn <a href="#method.choose" class="fn">choose</a>&lt;R&gt;(&amp;self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</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.93.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;(
&amp;self,
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>,
amount: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>,
) -&gt; <a class="struct" href="struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a>&lt;'_, Self, Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>&gt; <a href="#" class="tooltip" data-notable-ty="SliceChooseIter&lt;&#39;_, Self, Self::Output&gt;"></a>
<span class="where">where Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.93.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_array" class="fn">choose_multiple_array</a>&lt;R, const N: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>&gt;(
&amp;self,
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>,
) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.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.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>; <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.array.html">N</a>]&gt;
<span class="where">where Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
<span class="item-spacer"></span> fn <a href="#method.choose_weighted" class="fn">choose_weighted</a>&lt;R, F, B, X&gt;(
&amp;self,
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>,
weight: F,
) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;&amp;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>, <a class="enum" href="../distr/weighted/enum.Error.html" title="enum rand::distr::weighted::Error">WeightError</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.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&amp;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>) -&gt; B,
B: <a class="trait" href="../distr/uniform/trait.SampleBorrow.html" title="trait rand::distr::uniform::SampleBorrow">SampleBorrow</a>&lt;X&gt;,
X: <a class="trait" href="../distr/uniform/trait.SampleUniform.html" title="trait rand::distr::uniform::SampleUniform">SampleUniform</a> + <a class="trait" href="../distr/weighted/trait.Weight.html" title="trait rand::distr::weighted::Weight">Weight</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;X&gt;</span> { ... }
<span class="item-spacer"></span> fn <a href="#method.choose_multiple_weighted" class="fn">choose_multiple_weighted</a>&lt;R, F, X&gt;(
&amp;self,
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>,
amount: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>,
weight: F,
) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="struct" href="struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a>&lt;'_, Self, Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>&gt;, <a class="enum" href="../distr/weighted/enum.Error.html" title="enum rand::distr::weighted::Error">WeightError</a>&gt;
<span class="where">where Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&amp;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>) -&gt; X,
X: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.f64.html">f64</a>&gt;</span> { ... }
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Extension trait on indexable lists, providing random sampling methods.</p>
<p>This trait is implemented on <code>[T]</code> slice types. Other types supporting
<a href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html" title="trait core::ops::index::Index"><code>std::ops::Index&lt;usize&gt;</code></a> may implement this (only <a href="trait.IndexedRandom.html#tymethod.len"><code>Self::len</code></a> must be
specified).</p>
</div></details><h2 id="required-methods" class="section-header">Required Methods<a href="#required-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="tymethod.len" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#27">Source</a><h4 class="code-header">fn <a href="#tymethod.len" class="fn">len</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a></h4></section></summary><div class="docblock"><p>The length</p>
</div></details></div><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.is_empty" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#31-33">Source</a><h4 class="code-header">fn <a href="#method.is_empty" class="fn">is_empty</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>True when the length is zero</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.choose" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#52-61">Source</a><h4 class="code-header">fn <a href="#method.choose" class="fn">choose</a>&lt;R&gt;(&amp;self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</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.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h4></section></summary><div class="docblock"><p>Uniformly sample one element</p>
<p>Returns a reference to one uniformly-sampled random element of
the slice, or <code>None</code> if the slice is empty.</p>
<p>For slices, complexity is <code>O(1)</code>.</p>
<h5 id="example"><a class="doc-anchor" href="#example">§</a>Example</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::seq::IndexedRandom;
<span class="kw">let </span>choices = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">8</span>, <span class="number">16</span>, <span class="number">32</span>];
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
<span class="macro">println!</span>(<span class="string">"{:?}"</span>, choices.choose(<span class="kw-2">&amp;mut </span>rng));
<span class="macro">assert_eq!</span>(choices[..<span class="number">0</span>].choose(<span class="kw-2">&amp;mut </span>rng), <span class="prelude-val">None</span>);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.choose_multiple" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#90-105">Source</a><h4 class="code-header">fn <a href="#method.choose_multiple" class="fn">choose_multiple</a>&lt;R&gt;(
&amp;self,
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>,
amount: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>,
) -&gt; <a class="struct" href="struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a>&lt;'_, Self, Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>&gt; <a href="#" class="tooltip" data-notable-ty="SliceChooseIter&lt;&#39;_, Self, Self::Output&gt;"></a><div class="where">where
Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h4></section></summary><div class="docblock"><p>Uniformly sample <code>amount</code> distinct elements from self</p>
<p>Chooses <code>amount</code> elements from the slice at random, without repetition,
and in random order. The returned iterator is appropriate both for
collection into a <code>Vec</code> and filling an existing buffer (see example).</p>
<p>In case this API is not sufficiently flexible, use <a href="index/fn.sample.html" title="fn rand::seq::index::sample"><code>index::sample</code></a>.</p>
<p>For slices, complexity is the same as <a href="index/fn.sample.html" title="fn rand::seq::index::sample"><code>index::sample</code></a>.</p>
<h5 id="example-1"><a class="doc-anchor" href="#example-1">§</a>Example</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::seq::IndexedRandom;
<span class="kw">let </span><span class="kw-2">mut </span>rng = <span class="kw-2">&amp;mut </span>rand::rng();
<span class="kw">let </span>sample = <span class="string">"Hello, audience!"</span>.as_bytes();
<span class="comment">// collect the results into a vector:
</span><span class="kw">let </span>v: Vec&lt;u8&gt; = sample.choose_multiple(<span class="kw-2">&amp;mut </span>rng, <span class="number">3</span>).cloned().collect();
<span class="comment">// store in a buffer:
</span><span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0u8</span>; <span class="number">5</span>];
<span class="kw">for </span>(b, slot) <span class="kw">in </span>sample.choose_multiple(<span class="kw-2">&amp;mut </span>rng, buf.len()).zip(buf.iter_mut()) {
<span class="kw-2">*</span>slot = <span class="kw-2">*</span>b;
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.choose_multiple_array" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#123-130">Source</a><h4 class="code-header">fn <a href="#method.choose_multiple_array" class="fn">choose_multiple_array</a>&lt;R, const N: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>&gt;(
&amp;self,
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>,
) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.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.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>; <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.array.html">N</a>]&gt;<div class="where">where
Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h4></section></summary><div class="docblock"><p>Uniformly sample a fixed-size array of distinct elements from self</p>
<p>Chooses <code>N</code> elements from the slice at random, without repetition,
and in random order.</p>
<p>For slices, complexity is the same as <a href="index/fn.sample_array.html" title="fn rand::seq::index::sample_array"><code>index::sample_array</code></a>.</p>
<h5 id="example-2"><a class="doc-anchor" href="#example-2">§</a>Example</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::seq::IndexedRandom;
<span class="kw">let </span><span class="kw-2">mut </span>rng = <span class="kw-2">&amp;mut </span>rand::rng();
<span class="kw">let </span>sample = <span class="string">"Hello, audience!"</span>.as_bytes();
<span class="kw">let </span>a: [u8; <span class="number">3</span>] = sample.choose_multiple_array(<span class="kw-2">&amp;mut </span>rng).unwrap();</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.choose_weighted" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#162-176">Source</a><h4 class="code-header">fn <a href="#method.choose_weighted" class="fn">choose_weighted</a>&lt;R, F, B, X&gt;(
&amp;self,
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>,
weight: F,
) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;&amp;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>, <a class="enum" href="../distr/weighted/enum.Error.html" title="enum rand::distr::weighted::Error">WeightError</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.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&amp;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>) -&gt; B,
B: <a class="trait" href="../distr/uniform/trait.SampleBorrow.html" title="trait rand::distr::uniform::SampleBorrow">SampleBorrow</a>&lt;X&gt;,
X: <a class="trait" href="../distr/uniform/trait.SampleUniform.html" title="trait rand::distr::uniform::SampleUniform">SampleUniform</a> + <a class="trait" href="../distr/weighted/trait.Weight.html" title="trait rand::distr::weighted::Weight">Weight</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;X&gt;,</div></h4></section></summary><div class="docblock"><p>Biased sampling for one element</p>
<p>Returns a reference to one element of the slice, sampled according
to the provided weights. Returns <code>None</code> only if the slice is empty.</p>
<p>The specified function <code>weight</code> maps each item <code>x</code> to a relative
likelihood <code>weight(x)</code>. The probability of each item being selected is
therefore <code>weight(x) / s</code>, where <code>s</code> is the sum of all <code>weight(x)</code>.</p>
<p>For slices of length <code>n</code>, complexity is <code>O(n)</code>.
For more information about the underlying algorithm,
see the <a href="../distr/weighted/struct.WeightedIndex.html" title="struct rand::distr::weighted::WeightedIndex"><code>WeightedIndex</code></a> distribution.</p>
<p>See also <a href="trait.IndexedMutRandom.html#method.choose_weighted_mut" title="method rand::seq::IndexedMutRandom::choose_weighted_mut"><code>choose_weighted_mut</code></a>.</p>
<h5 id="example-3"><a class="doc-anchor" href="#example-3">§</a>Example</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::prelude::<span class="kw-2">*</span>;
<span class="kw">let </span>choices = [(<span class="string">'a'</span>, <span class="number">2</span>), (<span class="string">'b'</span>, <span class="number">1</span>), (<span class="string">'c'</span>, <span class="number">1</span>), (<span class="string">'d'</span>, <span class="number">0</span>)];
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
<span class="comment">// 50% chance to print 'a', 25% chance to print 'b', 25% chance to print 'c',
// and 'd' will never be printed
</span><span class="macro">println!</span>(<span class="string">"{:?}"</span>, choices.choose_weighted(<span class="kw-2">&amp;mut </span>rng, |item| item.<span class="number">1</span>).unwrap().<span class="number">0</span>);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.choose_multiple_weighted" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#211-235">Source</a><h4 class="code-header">fn <a href="#method.choose_multiple_weighted" class="fn">choose_multiple_weighted</a>&lt;R, F, X&gt;(
&amp;self,
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>,
amount: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>,
weight: F,
) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="struct" href="struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a>&lt;'_, Self, Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>&gt;, <a class="enum" href="../distr/weighted/enum.Error.html" title="enum rand::distr::weighted::Error">WeightError</a>&gt;<div class="where">where
Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&amp;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>) -&gt; X,
X: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.f64.html">f64</a>&gt;,</div></h4></section></summary><div class="docblock"><p>Biased sampling of <code>amount</code> distinct elements</p>
<p>Similar to <a href="trait.IndexedRandom.html#method.choose_multiple" title="method rand::seq::IndexedRandom::choose_multiple"><code>choose_multiple</code></a>, but where the likelihood of each
elements inclusion in the output may be specified. Zero-weighted
elements are never returned; the result may therefore contain fewer
elements than <code>amount</code> even when <code>self.len() &gt;= amount</code>. The elements
are returned in an arbitrary, unspecified order.</p>
<p>The specified function <code>weight</code> maps each item <code>x</code> to a relative
likelihood <code>weight(x)</code>. The probability of each item being selected is
therefore <code>weight(x) / s</code>, where <code>s</code> is the sum of all <code>weight(x)</code>.</p>
<p>This implementation uses <code>O(length + amount)</code> space and <code>O(length)</code> time.
See <a href="index/fn.sample_weighted.html" title="fn rand::seq::index::sample_weighted"><code>index::sample_weighted</code></a> for details.</p>
<h5 id="example-4"><a class="doc-anchor" href="#example-4">§</a>Example</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::prelude::<span class="kw-2">*</span>;
<span class="kw">let </span>choices = [(<span class="string">'a'</span>, <span class="number">2</span>), (<span class="string">'b'</span>, <span class="number">1</span>), (<span class="string">'c'</span>, <span class="number">1</span>)];
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
<span class="comment">// First Draw * Second Draw = total odds
// -----------------------
// (50% * 50%) + (25% * 67%) = 41.7% chance that the output is `['a', 'b']` in some order.
// (50% * 50%) + (25% * 67%) = 41.7% chance that the output is `['a', 'c']` in some order.
// (25% * 33%) + (25% * 33%) = 16.6% chance that the output is `['b', 'c']` in some order.
</span><span class="macro">println!</span>(<span class="string">"{:?}"</span>, choices.choose_multiple_weighted(<span class="kw-2">&amp;mut </span>rng, <span class="number">2</span>, |item| item.<span class="number">1</span>).unwrap().collect::&lt;Vec&lt;<span class="kw">_</span>&gt;&gt;());</code></pre></div></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.93.1/reference/items/traits.html#dyn-compatibility">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="foreign-impls" class="section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor">§</a></h2><details class="toggle implementors-toggle"><summary><section id="impl-IndexedRandom-for-%5BT%5D" class="impl"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#364-368">Source</a><a href="#impl-IndexedRandom-for-%5BT%5D" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="trait.IndexedRandom.html" title="trait rand::seq::IndexedRandom">IndexedRandom</a> for <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.slice.html">[T]</a></h3></section></summary><div class="impl-items"><section id="method.len" class="method trait-impl"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#365-367">Source</a><a href="#method.len" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.len" class="fn">len</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a></h4></section></div></details><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"></div><script src="../../trait.impl/rand/seq/slice/trait.IndexedRandom.js" data-ignore-extern-crates="std" async></script><script type="text/json" id="notable-traits-data">{"SliceChooseIter<'_, Self, Self::Output>":"<h3>Notable traits for <code><a class=\"struct\" href=\"struct.SliceChooseIter.html\" title=\"struct rand::seq::SliceChooseIter\">SliceChooseIter</a>&lt;'a, S, T&gt;</code></h3><pre><code><div class=\"where\">impl&lt;'a, S: <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html\" title=\"trait core::ops::index::Index\">Index</a>&lt;<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.93.1/std/primitive.usize.html\">usize</a>, Output = T&gt; + ?<a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html\" title=\"trait core::marker::Sized\">Sized</a> + 'a, T: 'a&gt; <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html\" title=\"trait core::iter::traits::iterator::Iterator\">Iterator</a> for <a class=\"struct\" href=\"struct.SliceChooseIter.html\" title=\"struct rand::seq::SliceChooseIter\">SliceChooseIter</a>&lt;'a, S, T&gt;</div><div class=\"where\"> type <a href=\"https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item\" class=\"associatedtype\">Item</a> = <a class=\"primitive\" href=\"https://doc.rust-lang.org/1.93.1/std/primitive.reference.html\">&amp;'a T</a>;</div>"}</script></section></div></main></body></html>