115 lines
52 KiB
HTML
115 lines
52 KiB
HTML
|
<!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="API documentation for the Rust `SliceRandom` trait in crate `rand`."><meta name="keywords" content="rust, rustlang, rust-lang, SliceRandom"><title>rand::seq::SliceRandom - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="shortcut icon" href="https://www.rust-lang.org/favicon.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc trait"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a 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><p class='location'>Trait SliceRandom</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#associated-types">Associated Types</a><div class="sidebar-links"><a href="#associatedtype.Item">Item</a></div><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.choose">choose</a><a href="#tymethod.choose_multiple">choose_multiple</a><a href="#tymethod.choose_mut">choose_mut</a><a href="#tymethod.choose_weighted">choose_weighted</a><a href="#tymethod.choose_weighted_mut">choose_weighted_mut</a><a href="#tymethod.partial_shuffle">partial_shuffle</a><a href="#tymethod.shuffle">shuffle</a></div><a class="sidebar-title" href="#foreign-impls">Implementations on Foreign Types</a><div class="sidebar-links"><a href="#impl-SliceRandom-for-%5BT%5D">[T]</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>rand</a>::<wbr><a href='index.html'>seq</a></p><script>window.sidebarCurrent = {name: 'SliceRandom', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../src/rand/seq/mod.rs.html#58-220' title='goto source code'>[src]</a></span><span class='in-band'>Trait <a href='../index.html'>rand</a>::<wbr><a href='index.html'>seq</a>::<wbr><a class="trait" href=''>SliceRandom</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><pre class='rust trait'>pub trait SliceRandom {
|
|||
|
type <a href='#associatedtype.Item' class="type">Item</a>;
|
|||
|
fn <a href='#tymethod.choose' class='fnname'>choose</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(&self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>><br> <span class="where">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a></span>;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.choose_mut' class='fnname'>choose_mut</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(&mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&mut Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>><br> <span class="where">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a></span>;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.choose_multiple' class='fnname'>choose_multiple</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> &self, <br> rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R, <br> amount: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br> ) -> <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a><Self, Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>><br> <span class="where">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a></span>;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.choose_weighted' class='fnname'>choose_weighted</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, F, B, X>(<br> &self, <br> rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R, <br> weight: F<br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>, <a class="enum" href="../../rand/distributions/weighted/enum.WeightedError.html" title="enum rand::distributions::weighted::WeightedError">WeightedError</a>><br> <span class="where">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>) -> B,<br> B: <a class="trait" href="../../rand/distributions/uniform/trait.SampleBorrow.html" title="trait rand::distributions::uniform::SampleBorrow">SampleBorrow</a><X>,<br> X: <a class="trait" href="../../rand/distributions/uniform/trait.SampleUniform.html" title="trait rand::distributions::uniform::SampleUniform">SampleUniform</a> + for<'a> <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/arith/trait.AddAssign.html" title="trait core::ops::arith::AddAssign">AddAssign</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&'a </a>X> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a><X> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/default/trait.Default.html" title="trait core::default::Default">Default</a></span>;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.choose_weighted_mut' class='fnname'>choose_weighted_mut</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, F, B, X>(<br> &mut self, <br> rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R, <br> weight: F<br> ) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&mut Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>, <a class="enum" href="../../rand/distributions/weighted/enum.WeightedError.html" title="enum rand::distributions::weighted::WeightedError">WeightedError</a>><br> <span class="where">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>) -> B,<br> B: <a class="trait" href="../../rand/distributions/uniform/trait.SampleBorrow.html" title="trait rand::distributions::uniform::SampleBorrow">SampleBorrow</a><X>,<br> X: <a class="trait" href="../../rand/distributions/uniform/trait.SampleUniform.html" title="trait rand::distributions::uniform::SampleUniform">SampleUniform</a> + for<'a> <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/arith/trait.AddAssign.html" title="trait core::ops::arith::AddAssign">AddAssign</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&'a </a>X> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a><X> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/default/trait.Default.html" title="trait core::default::Default">Default</a></span>;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.shuffle' class='fnname'>shuffle</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(&mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R)<br> <span class="where">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a></span>;
|
|||
|
<div class='item-spacer'></div> fn <a href='#tymethod.partial_shuffle' class='fnname'>partial_shuffle</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> &mut self, <br> rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R, <br> amount: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br> ) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [</a>Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [</a>Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a><br> <span class="where">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a></span>;
|
|||
|
}</pre></div><div class='docblock'><p>Extension trait on slices, providing random mutation and sampling methods.</p>
|
|||
|
<p>This trait is implemented on all <code>[T]</code> slice types, providing several
|
|||
|
methods for choosing and shuffling elements. You must <code>use</code> this trait:</p>
|
|||
|
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">seq</span>::<span class="ident">SliceRandom</span>;
|
|||
|
|
|||
|
<span class="kw">fn</span> <span class="ident">main</span>() {
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">rand</span>::<span class="ident">thread_rng</span>();
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">bytes</span> <span class="op">=</span> <span class="string">"Hello, random!"</span>.<span class="ident">to_string</span>().<span class="ident">into_bytes</span>();
|
|||
|
<span class="ident">bytes</span>.<span class="ident">shuffle</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">rng</span>);
|
|||
|
<span class="kw">let</span> <span class="ident">str</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from_utf8</span>(<span class="ident">bytes</span>).<span class="ident">unwrap</span>();
|
|||
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">str</span>);
|
|||
|
}</pre></div>
|
|||
|
<p>Example output (non-deterministic):</p>
|
|||
|
<pre><code class="language-none">l,nmroHado !le
|
|||
|
</code></pre>
|
|||
|
</div>
|
|||
|
<h2 id='associated-types' class='small-section-header'>Associated Types<a href='#associated-types' class='anchor'></a></h2><div class='methods'><h3 id='associatedtype.Item' class='method'><code id='Item.t'>type <a href='#associatedtype.Item' class="type">Item</a></code></h3><div class='docblock'><p>The element type.</p>
|
|||
|
</div></div><span class='loading-content'>Loading content...</span>
|
|||
|
<h2 id='required-methods' class='small-section-header'>Required methods<a href='#required-methods' class='anchor'></a></h2><div class='methods'><h3 id='tymethod.choose' class='method'><code id='choose.v'>fn <a href='#tymethod.choose' class='fnname'>choose</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(&self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>> <span class="where fmt-newline">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>, </span></code></h3><div class='docblock'><p>Returns a reference to one 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>
|
|||
|
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">thread_rng</span>;
|
|||
|
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">seq</span>::<span class="ident">SliceRandom</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">choices</span> <span class="op">=</span> [<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> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">thread_rng</span>();
|
|||
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="ident">choices</span>.<span class="ident">choose</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">rng</span>));
|
|||
|
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">choices</span>[..<span class="number">0</span>].<span class="ident">choose</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">rng</span>), <span class="prelude-val">None</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.choose_mut' class='method'><code id='choose_mut.v'>fn <a href='#tymethod.choose_mut' class='fnname'>choose_mut</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(&mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&mut Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>> <span class="where fmt-newline">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>, </span></code></h3><div class='docblock'><p>Returns a mutable reference to one 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>
|
|||
|
</div><h3 id='tymethod.choose_multiple' class='method'><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a><'a, S, T></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a><'a, S, T></h3><code class="content"><span class="where fmt-newline">impl<'a, S: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/index/trait.Index.html" title="trait core::ops::index::Index">Index</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, Output = T> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a, T: 'a> <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/iterator/trait.Iterator.html" title="trait core::iter::traits::iterator::Iterator">Iterator</a> for <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a><'a, S, T></span><span class="where fmt-newline"> type <a href='https://doc.rust-lang.org/nightly/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&'a </a>T;</span></code></div></div><code id='choose_multiple.v'>fn <a href='#tymethod.choose_multiple' class='fnname'>choose_multiple</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> &self, <br> rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R, <br> amount: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -> <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a><Self, Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>> <span class="where fmt-newline">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>, </span></code></h3><div class='docblock'><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 [<code>index::sample</code>].</p>
|
|||
|
<p>For slices, complexity is the same as [<code>index::sample</code>].</p>
|
|||
|
<h1 id="example-1" class="section-header"><a href="#example-1">Example</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">seq</span>::<span class="ident">SliceRandom</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">rand</span>::<span class="ident">thread_rng</span>();
|
|||
|
<span class="kw">let</span> <span class="ident">sample</span> <span class="op">=</span> <span class="string">"Hello, audience!"</span>.<span class="ident">as_bytes</span>();
|
|||
|
|
|||
|
<span class="comment">// collect the results into a vector:</span>
|
|||
|
<span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op"><</span><span class="ident">u8</span><span class="op">></span> <span class="op">=</span> <span class="ident">sample</span>.<span class="ident">choose_multiple</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">rng</span>, <span class="number">3</span>).<span class="ident">cloned</span>().<span class="ident">collect</span>();
|
|||
|
|
|||
|
<span class="comment">// store in a buffer:</span>
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buf</span> <span class="op">=</span> [<span class="number">0u8</span>; <span class="number">5</span>];
|
|||
|
<span class="kw">for</span> (<span class="ident">b</span>, <span class="ident">slot</span>) <span class="kw">in</span> <span class="ident">sample</span>.<span class="ident">choose_multiple</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">rng</span>, <span class="ident">buf</span>.<span class="ident">len</span>()).<span class="ident">zip</span>(<span class="ident">buf</span>.<span class="ident">iter_mut</span>()) {
|
|||
|
<span class="kw-2">*</span><span class="ident">slot</span> <span class="op">=</span> <span class="kw-2">*</span><span class="ident">b</span>;
|
|||
|
}</pre></div>
|
|||
|
</div><h3 id='tymethod.choose_weighted' class='method'><code id='choose_weighted.v'>fn <a href='#tymethod.choose_weighted' class='fnname'>choose_weighted</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, F, B, X>(<br> &self, <br> rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R, <br> weight: F<br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>, <a class="enum" href="../../rand/distributions/weighted/enum.WeightedError.html" title="enum rand::distributions::weighted::WeightedError">WeightedError</a>> <span class="where fmt-newline">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>) -> B,<br> B: <a class="trait" href="../../rand/distributions/uniform/trait.SampleBorrow.html" title="trait rand::distributions::uniform::SampleBorrow">SampleBorrow</a><X>,<br> X: <a class="trait" href="../../rand/distributions/uniform/trait.SampleUniform.html" title="trait rand::distributions::uniform::SampleUniform">SampleUniform</a> + for<'a> <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/arith/trait.AddAssign.html" title="trait core::ops::arith::AddAssign">AddAssign</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&'a </a>X> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a><X> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/default/trait.Default.html" title="trait core::default::Default">Default</a>, </span></code></h3><div class='docblock'><p>Similar to <a href="SliceRandom::choose"><code>choose</code></a>, but where the likelihood of each outcome may be
|
|||
|
specified.</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>.
|
|||
|
See also <a href="SliceRandom::choose_weighted_mut"><code>choose_weighted_mut</code></a>, <a href="crate::distributions::weighted"><code>distributions::weighted</code></a>.</p>
|
|||
|
<h1 id="example-2" class="section-header"><a href="#example-2">Example</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="ident">choices</span> <span class="op">=</span> [(<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> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">thread_rng</span>();
|
|||
|
<span class="comment">// 50% chance to print 'a', 25% chance to print 'b', 25% chance to print 'c'</span>
|
|||
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="ident">choices</span>.<span class="ident">choose_weighted</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">rng</span>, <span class="op">|</span><span class="ident">item</span><span class="op">|</span> <span class="ident">item</span>.<span class="number">1</span>).<span class="ident">unwrap</span>().<span class="number">0</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.choose_weighted_mut' class='method'><code id='choose_weighted_mut.v'>fn <a href='#tymethod.choose_weighted_mut' class='fnname'>choose_weighted_mut</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, F, B, X>(<br> &mut self, <br> rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R, <br> weight: F<br>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&mut Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>, <a class="enum" href="../../rand/distributions/weighted/enum.WeightedError.html" title="enum rand::distributions::weighted::WeightedError">WeightedError</a>> <span class="where fmt-newline">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>) -> B,<br> B: <a class="trait" href="../../rand/distributions/uniform/trait.SampleBorrow.html" title="trait rand::distributions::uniform::SampleBorrow">SampleBorrow</a><X>,<br> X: <a class="trait" href="../../rand/distributions/uniform/trait.SampleUniform.html" title="trait rand::distributions::uniform::SampleUniform">SampleUniform</a> + for<'a> <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/arith/trait.AddAssign.html" title="trait core::ops::arith::AddAssign">AddAssign</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&'a </a>X> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a><X> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/default/trait.Default.html" title="trait core::default::Default">Default</a>, </span></code></h3><div class='docblock'><p>Similar to <a href="SliceRandom::choose_mut"><code>choose_mut</code></a>, but where the likelihood of each outcome may
|
|||
|
be specified.</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>.
|
|||
|
See also <a href="SliceRandom::choose_weighted"><code>choose_weighted</code></a>, <a href="crate::distributions::weighted"><code>distributions::weighted</code></a>.</p>
|
|||
|
</div><h3 id='tymethod.shuffle' class='method'><code id='shuffle.v'>fn <a href='#tymethod.shuffle' class='fnname'>shuffle</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(&mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R) <span class="where fmt-newline">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>, </span></code></h3><div class='docblock'><p>Shuffle a mutable slice in place.</p>
|
|||
|
<p>For slices of length <code>n</code>, complexity is <code>O(n)</code>.</p>
|
|||
|
<h1 id="example-3" class="section-header"><a href="#example-3">Example</a></h1>
|
|||
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|||
|
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">seq</span>::<span class="ident">SliceRandom</span>;
|
|||
|
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">thread_rng</span>;
|
|||
|
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">thread_rng</span>();
|
|||
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">y</span> <span class="op">=</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];
|
|||
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Unshuffled: {:?}"</span>, <span class="ident">y</span>);
|
|||
|
<span class="ident">y</span>.<span class="ident">shuffle</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">rng</span>);
|
|||
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Shuffled: {:?}"</span>, <span class="ident">y</span>);</pre></div>
|
|||
|
</div><h3 id='tymethod.partial_shuffle' class='method'><code id='partial_shuffle.v'>fn <a href='#tymethod.partial_shuffle' class='fnname'>partial_shuffle</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(<br> &mut self, <br> rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R, <br> amount: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [</a>Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [</a>Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a> <span class="where fmt-newline">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>, </span></code></h3><div class='docblock'><p>Shuffle a slice in place, but exit early.</p>
|
|||
|
<p>Returns two mutable slices from the source slice. The first contains
|
|||
|
<code>amount</code> elements randomly permuted. The second has the remaining
|
|||
|
elements that are not fully shuffled.</p>
|
|||
|
<p>This is an efficient method to select <code>amount</code> elements at random from
|
|||
|
the slice, provided the slice may be mutated.</p>
|
|||
|
<p>If you only need to choose elements randomly and <code>amount > self.len()/2</code>
|
|||
|
then you may improve performance by taking
|
|||
|
<code>amount = values.len() - amount</code> and using only the second slice.</p>
|
|||
|
<p>If <code>amount</code> is greater than the number of elements in the slice, this
|
|||
|
will perform a full shuffle.</p>
|
|||
|
<p>For slices, complexity is <code>O(m)</code> where <code>m = amount</code>.</p>
|
|||
|
</div></div><span class='loading-content'>Loading content...</span>
|
|||
|
<h2 id='foreign-impls' class='small-section-header'>Implementations on Foreign Types<a href='#foreign-impls' class='anchor'></a></h2><h3 id='impl-SliceRandom-for-%5BT%5D' class='impl'><code class='in-band'>impl<T> <a class="trait" href="../../rand/seq/trait.SliceRandom.html" title="trait rand::seq::SliceRandom">SliceRandom</a> for <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a></code><a href='#impl-SliceRandom-for-%5BT%5D' class='anchor'></a><a class='srclink' href='../../src/rand/seq/mod.rs.html#378-476' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='associatedtype.Item-1' class="type"><code id='Item.t-1'>type <a href='#associatedtype.Item' class="type">Item</a> = T</code></h4><h4 id='method.choose' class="method hidden"><code id='choose.v-1'>fn <a href='#method.choose' class='fnname'>choose</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(&self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>> <span class="where fmt-newline">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>, </span></code><a class='srclink' href='../../src/rand/seq/mod.rs.html#381-388' title='goto source code'>[src]</a></h4><h4 id='method.choose_mut' class="method hidden"><code id='choose_mut.v-1'>fn <a href='#method.choose_mut' class='fnname'>choose_mut</a><R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>>(&mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>R) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&mut Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>> <span class="where fmt-newline">where<br> R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>, </span></code><a class='srclink' href='../../src/rand/seq/mod.rs.html#390-398' title='goto source code'>[src]</a></h4><h4 id='method.choose_multiple' class="method hidden"><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a><'a, S, T></span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a><'a, S, T></h3><code class="content"><span class="where fmt-newline">impl<'a, S: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/index/trait.Index.html" title="trait core::ops::index::Index">Index</a><<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, Output = T> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a, T: 'a> <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/iterator/trait.Iterator.html" title="trait core::iter::traits::iterator::Iterator">Iterator</a> for <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter"
|
|||
|
<h2 id='implementors' class='small-section-header'>Implementors<a href='#implementors' class='anchor'></a></h2><div class='item-list' id='implementors-list'></div><span class='loading-content'>Loading content...</span><script type="text/javascript">window.inlined_types=new Set([]);</script><script type="text/javascript" async
|
|||
|
src="../../implementors/rand/seq/trait.SliceRandom.js">
|
|||
|
</script></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "rand";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>
|