Adding large folders

This commit is contained in:
2026-02-26 12:00:21 -05:00
parent 5400d82acd
commit 49701c85ad
47332 changed files with 1942573 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
<!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="Sequence-related functionality"><title>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 mod"><!--[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="#">Module seq</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="#">Module seq</a></h2><h3><a href="#reexports">Module Items</a></h3><ul class="block"><li><a href="#reexports" title="Re-exports">Re-exports</a></li><li><a href="#modules" title="Modules">Modules</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#traits" title="Traits">Traits</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="../index.html">In crate rand</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></div><h1>Module <span>seq</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/mod.rs.html#9-80">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Sequence-related functionality</p>
<p>This module provides:</p>
<ul>
<li><a href="trait.IndexedRandom.html" title="trait rand::seq::IndexedRandom"><code>IndexedRandom</code></a> for sampling slices and other indexable lists</li>
<li><a href="trait.IndexedMutRandom.html" title="trait rand::seq::IndexedMutRandom"><code>IndexedMutRandom</code></a> for sampling slices and other mutably indexable lists</li>
<li><a href="trait.SliceRandom.html" title="trait rand::seq::SliceRandom"><code>SliceRandom</code></a> for mutating slices</li>
<li><a href="trait.IteratorRandom.html" title="trait rand::seq::IteratorRandom"><code>IteratorRandom</code></a> for sampling iterators</li>
<li><a href="index/fn.sample.html" title="fn rand::seq::index::sample"><code>index::sample</code></a> low-level API to choose multiple indices from
<code>0..length</code></li>
</ul>
<p>Also see:</p>
<ul>
<li><a href="../distr/weighted/struct.WeightedIndex.html" title="struct rand::distr::weighted::WeightedIndex"><code>crate::distr::weighted::WeightedIndex</code></a> distribution which provides
weighted index sampling.</li>
</ul>
<p>In order to make results reproducible across 32-64 bit architectures, all
<code>usize</code> indices are sampled as a <code>u32</code> where possible (also providing a
small performance boost in some cases).</p>
</div></details><h2 id="reexports" class="section-header">Re-exports<a href="#reexports" class="anchor">§</a></h2><dl class="item-table reexports"><dt id="reexport.WeightError"><code>pub use crate::distr::weighted::<a class="enum" href="../distr/weighted/enum.Error.html" title="enum rand::distr::weighted::Error">Error</a> as WeightError;</code></dt></dl><h2 id="modules" class="section-header">Modules<a href="#modules" class="anchor">§</a></h2><dl class="item-table"><dt><a class="mod" href="index/index.html" title="mod rand::seq::index">index</a></dt><dd>Low-level API for sampling indices</dd></dl><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">Slice<wbr>Choose<wbr>Iter</a></dt><dd>An iterator over multiple slice elements.</dd></dl><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><dl class="item-table"><dt><a class="trait" href="trait.IndexedMutRandom.html" title="trait rand::seq::IndexedMutRandom">Indexed<wbr>MutRandom</a></dt><dd>Extension trait on indexable lists, providing random sampling methods.</dd><dt><a class="trait" href="trait.IndexedRandom.html" title="trait rand::seq::IndexedRandom">Indexed<wbr>Random</a></dt><dd>Extension trait on indexable lists, providing random sampling methods.</dd><dt><a class="trait" href="trait.IteratorRandom.html" title="trait rand::seq::IteratorRandom">Iterator<wbr>Random</a></dt><dd>Extension trait on iterators, providing random sampling methods.</dd><dt><a class="trait" href="trait.SliceRandom.html" title="trait rand::seq::SliceRandom">Slice<wbr>Random</a></dt><dd>Extension trait on slices, providing shuffling methods.</dd></dl></section></div></main></body></html>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,19 @@
<!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="Randomly sample exactly `amount` distinct indices from `0..length`, and return them in random order (fully shuffled)."><title>sample in rand::seq::index - 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 fn"><!--[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="#">sample</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"><div id="rustdoc-modnav"><h2><a href="index.html">In rand::<wbr>seq::<wbr>index</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>::<wbr><a href="index.html">index</a></div><h1>Function <span class="fn">sample</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/index.rs.html#240-283">Source</a> </span></div><pre class="rust item-decl"><code>pub fn sample&lt;R&gt;(rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>, length: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>, amount: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>) -&gt; <a class="enum" href="enum.IndexVec.html" title="enum rand::seq::index::IndexVec">IndexVec</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.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Randomly sample exactly <code>amount</code> distinct indices from <code>0..length</code>, and
return them in random order (fully shuffled).</p>
<p>This method is used internally by the slice sampling methods, but it can
sometimes be useful to have the indices themselves so this is provided as
an alternative.</p>
<p>The implementation used is not specified; we automatically select the
fastest available algorithm for the <code>length</code> and <code>amount</code> parameters
(based on detailed profiling on an Intel Haswell CPU). Roughly speaking,
complexity is <code>O(amount)</code>, except that when <code>amount</code> is small, performance
is closer to <code>O(amount^2)</code>, and when <code>length</code> is close to <code>amount</code> then
<code>O(length)</code>.</p>
<p>Note that performance is significantly better over <code>u32</code> indices than over
<code>u64</code> indices. Because of this we hide the underlying type behind an
abstraction, <code>IndexVec</code>.</p>
<p>If an allocation-free <code>no_std</code> function is required, it is suggested
to adapt the internal <code>sample_floyd</code> implementation.</p>
<p>Panics if <code>amount &gt; length</code>.</p>
</div></details></section></div></main></body></html>

View File

@@ -0,0 +1,10 @@
<!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="Randomly sample exactly `N` distinct indices from `0..len`, and return them in random order (fully shuffled)."><title>sample_array in rand::seq::index - 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 fn"><!--[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="#">sample_array</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"><div id="rustdoc-modnav"><h2><a href="index.html">In rand::<wbr>seq::<wbr>index</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>::<wbr><a href="index.html">index</a></div><h1>Function <span class="fn">sample_<wbr>array</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/mod.rs.html#61-79">Source</a> </span></div><pre class="rust item-decl"><code>pub fn sample_array&lt;R, const N: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>&gt;(
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>,
len: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</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;[<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>; <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.array.html">N</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></code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Randomly sample exactly <code>N</code> distinct indices from <code>0..len</code>, and
return them in random order (fully shuffled).</p>
<p>This is implemented via Floyds algorithm. Time complexity is <code>O(N^2)</code>
and memory complexity is <code>O(N)</code>.</p>
<p>Returns <code>None</code> if (and only if) <code>N &gt; len</code>.</p>
</div></details></section></div></main></body></html>

View File

@@ -0,0 +1,22 @@
<!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="Randomly sample `amount` distinct indices from `0..length`"><title>sample_weighted in rand::seq::index - 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 fn"><!--[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="#">sample_weighted</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"><div id="rustdoc-modnav"><h2><a href="index.html">In rand::<wbr>seq::<wbr>index</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>::<wbr><a href="index.html">index</a></div><h1>Function <span class="fn">sample_<wbr>weighted</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/index.rs.html#302-329">Source</a> </span></div><pre class="rust item-decl"><code>pub fn sample_weighted&lt;R, F, X&gt;(
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>,
length: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>,
weight: F,
amount: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>,
) -&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="enum" href="enum.IndexVec.html" title="enum rand::seq::index::IndexVec">IndexVec</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>(<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</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></code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Randomly sample <code>amount</code> distinct indices from <code>0..length</code></p>
<p>The result may contain less than <code>amount</code> indices if insufficient non-zero
weights are available. Results are returned in an arbitrary order (there is
no guarantee of shuffling or ordering).</p>
<p>Function <code>weight</code> is called once for each index to provide weights.</p>
<p>This method is used internally by the slice sampling methods, but it can
sometimes be useful to have the indices themselves so this is provided as
an alternative.</p>
<p>Error cases:</p>
<ul>
<li><a href="../../distr/weighted/enum.Error.html#variant.InvalidWeight" title="variant rand::distr::weighted::Error::InvalidWeight"><code>WeightError::InvalidWeight</code></a> when a weight is not-a-number or negative.</li>
</ul>
<p>This implementation uses <code>O(length + amount)</code> space and <code>O(length)</code> time.</p>
</div></details></section></div></main></body></html>

View File

@@ -0,0 +1,4 @@
<!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="Low-level API for sampling indices"><title>rand::seq::index - 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 mod"><!--[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="#">Module index</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="#">Module index</a></h2><h3><a href="#enums">Module Items</a></h3><ul class="block"><li><a href="#enums" title="Enums">Enums</a></li><li><a href="#functions" title="Functions">Functions</a></li></ul></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>Module <span>index</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/mod.rs.html#47">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Low-level API for sampling indices</p>
</div></details><h2 id="enums" class="section-header">Enums<a href="#enums" class="anchor">§</a></h2><dl class="item-table"><dt><a class="enum" href="enum.IndexVec.html" title="enum rand::seq::index::IndexVec">Index<wbr>Vec</a></dt><dd>A vector of indices.</dd><dt><a class="enum" href="enum.IndexVecIntoIter.html" title="enum rand::seq::index::IndexVecIntoIter">Index<wbr>VecInto<wbr>Iter</a></dt><dd>Return type of <code>IndexVec::into_iter</code>.</dd><dt><a class="enum" href="enum.IndexVecIter.html" title="enum rand::seq::index::IndexVecIter">Index<wbr>VecIter</a></dt><dd>Return type of <code>IndexVec::iter</code>.</dd></dl><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><dl class="item-table"><dt><a class="fn" href="fn.sample.html" title="fn rand::seq::index::sample">sample</a></dt><dd>Randomly sample exactly <code>amount</code> distinct indices from <code>0..length</code>, and
return them in random order (fully shuffled).</dd><dt><a class="fn" href="fn.sample_array.html" title="fn rand::seq::index::sample_array">sample_<wbr>array</a></dt><dd>Randomly sample exactly <code>N</code> distinct indices from <code>0..len</code>, and
return them in random order (fully shuffled).</dd><dt><a class="fn" href="fn.sample_weighted.html" title="fn rand::seq::index::sample_weighted">sample_<wbr>weighted</a></dt><dd>Randomly sample <code>amount</code> distinct indices from <code>0..length</code></dd></dl></section></div></main></body></html>

View File

@@ -0,0 +1 @@
window.SIDEBAR_ITEMS = {"enum":["IndexVec","IndexVecIntoIter","IndexVecIter"],"fn":["sample","sample_array","sample_weighted"]};

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=../../../rand/seq/index/enum.IndexVec.html">
<title>Redirection</title>
</head>
<body>
<p>Redirecting to <a href="../../../rand/seq/index/enum.IndexVec.html">../../../rand/seq/index/enum.IndexVec.html</a>...</p>
<script>location.replace("../../../rand/seq/index/enum.IndexVec.html" + location.search + location.hash);</script>
</body>
</html>

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=../../../rand/seq/index/enum.IndexVecIntoIter.html">
<title>Redirection</title>
</head>
<body>
<p>Redirecting to <a href="../../../rand/seq/index/enum.IndexVecIntoIter.html">../../../rand/seq/index/enum.IndexVecIntoIter.html</a>...</p>
<script>location.replace("../../../rand/seq/index/enum.IndexVecIntoIter.html" + location.search + location.hash);</script>
</body>
</html>

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=../../../rand/seq/index/enum.IndexVecIter.html">
<title>Redirection</title>
</head>
<body>
<p>Redirecting to <a href="../../../rand/seq/index/enum.IndexVecIter.html">../../../rand/seq/index/enum.IndexVecIter.html</a>...</p>
<script>location.replace("../../../rand/seq/index/enum.IndexVecIter.html" + location.search + location.hash);</script>
</body>
</html>

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=../../../rand/seq/index/fn.sample.html">
<title>Redirection</title>
</head>
<body>
<p>Redirecting to <a href="../../../rand/seq/index/fn.sample.html">../../../rand/seq/index/fn.sample.html</a>...</p>
<script>location.replace("../../../rand/seq/index/fn.sample.html" + location.search + location.hash);</script>
</body>
</html>

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=../../../rand/seq/index/fn.sample_weighted.html">
<title>Redirection</title>
</head>
<body>
<p>Redirecting to <a href="../../../rand/seq/index/fn.sample_weighted.html">../../../rand/seq/index/fn.sample_weighted.html</a>...</p>
<script>location.replace("../../../rand/seq/index/fn.sample_weighted.html" + location.search + location.hash);</script>
</body>
</html>

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=../../../rand/seq/trait.IteratorRandom.html">
<title>Redirection</title>
</head>
<body>
<p>Redirecting to <a href="../../../rand/seq/trait.IteratorRandom.html">../../../rand/seq/trait.IteratorRandom.html</a>...</p>
<script>location.replace("../../../rand/seq/trait.IteratorRandom.html" + location.search + location.hash);</script>
</body>
</html>

View File

@@ -0,0 +1 @@
window.SIDEBAR_ITEMS = {"mod":["index"],"struct":["SliceChooseIter"],"trait":["IndexedMutRandom","IndexedRandom","IteratorRandom","SliceRandom"]};

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=../../../rand/seq/struct.SliceChooseIter.html">
<title>Redirection</title>
</head>
<body>
<p>Redirecting to <a href="../../../rand/seq/struct.SliceChooseIter.html">../../../rand/seq/struct.SliceChooseIter.html</a>...</p>
<script>location.replace("../../../rand/seq/struct.SliceChooseIter.html" + location.search + location.hash);</script>
</body>
</html>

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=../../../rand/seq/trait.IndexedMutRandom.html">
<title>Redirection</title>
</head>
<body>
<p>Redirecting to <a href="../../../rand/seq/trait.IndexedMutRandom.html">../../../rand/seq/trait.IndexedMutRandom.html</a>...</p>
<script>location.replace("../../../rand/seq/trait.IndexedMutRandom.html" + location.search + location.hash);</script>
</body>
</html>

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=../../../rand/seq/trait.IndexedRandom.html">
<title>Redirection</title>
</head>
<body>
<p>Redirecting to <a href="../../../rand/seq/trait.IndexedRandom.html">../../../rand/seq/trait.IndexedRandom.html</a>...</p>
<script>location.replace("../../../rand/seq/trait.IndexedRandom.html" + location.search + location.hash);</script>
</body>
</html>

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=../../../rand/seq/trait.SliceRandom.html">
<title>Redirection</title>
</head>
<body>
<p>Redirecting to <a href="../../../rand/seq/trait.SliceRandom.html">../../../rand/seq/trait.SliceRandom.html</a>...</p>
<script>location.replace("../../../rand/seq/trait.SliceRandom.html" + location.search + location.hash);</script>
</body>
</html>

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,40 @@
<!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>IndexedMutRandom 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="#">IndexedMutRandom</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>MutRandom</a></h2><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.choose_mut" title="choose_mut">choose_mut</a></li><li><a href="#method.choose_weighted_mut" title="choose_weighted_mut">choose_weighted_mut</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>MutRandom</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#242-296">Source</a> </span></div><pre class="rust item-decl"><code>pub trait IndexedMutRandom: <a class="trait" href="trait.IndexedRandom.html" title="trait rand::seq::IndexedRandom">IndexedRandom</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.IndexMut.html" title="trait core::ops::index::IndexMut">IndexMut</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>&gt; {
// Provided methods
fn <a href="#method.choose_mut" class="fn">choose_mut</a>&lt;R&gt;(&amp;mut 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;mut 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_weighted_mut" class="fn">choose_weighted_mut</a>&lt;R, F, B, X&gt;(
&amp;mut 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;mut 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> { ... }
}</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 automatically for every type implementing
<a href="trait.IndexedRandom.html" title="trait rand::seq::IndexedRandom"><code>IndexedRandom</code></a> and <a href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.IndexMut.html" title="trait core::ops::index::IndexMut"><code>std::ops::IndexMut&lt;usize&gt;</code></a>.</p>
</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_mut" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#249-259">Source</a><h4 class="code-header">fn <a href="#method.choose_mut" class="fn">choose_mut</a>&lt;R&gt;(&amp;mut 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;mut 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 (mut)</p>
<p>Returns a mutable 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>
</div></details><details class="toggle method-toggle" open><summary><section id="method.choose_weighted_mut" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#280-295">Source</a><h4 class="code-header">fn <a href="#method.choose_weighted_mut" class="fn">choose_weighted_mut</a>&lt;R, F, B, X&gt;(
&amp;mut 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;mut 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 (mut)</p>
<p>Returns a mutable 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.IndexedRandom.html#method.choose_weighted" title="method rand::seq::IndexedRandom::choose_weighted"><code>choose_weighted</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.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="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-IndexedMutRandom-for-IR" class="impl"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#370">Source</a><a href="#impl-IndexedMutRandom-for-IR" class="anchor">§</a><h3 class="code-header">impl&lt;IR: <a class="trait" href="trait.IndexedRandom.html" title="trait rand::seq::IndexedRandom">IndexedRandom</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.IndexMut.html" title="trait core::ops::index::IndexMut">IndexMut</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>&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>&gt; <a class="trait" href="trait.IndexedMutRandom.html" title="trait rand::seq::IndexedMutRandom">IndexedMutRandom</a> for IR</h3></section></div><script src="../../trait.impl/rand/seq/slice/trait.IndexedMutRandom.js" async></script></section></div></main></body></html>

View File

@@ -0,0 +1,153 @@
<!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>

View File

@@ -0,0 +1,86 @@
<!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-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="#">IteratorRandom</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="#">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" 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">Iterator<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/iterator.rs.html#34-269">Source</a> </span></div><pre class="rust item-decl"><code>pub trait IteratorRandom: <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> + <a class="trait" href="https://doc.rust-lang.org/1.93.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.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/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.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_stable" class="fn">choose_stable</a>&lt;R&gt;(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/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.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_fill" class="fn">choose_multiple_fill</a>&lt;R&gt;(
self,
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>,
buf: &amp;mut [Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.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.93.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.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;(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="https://doc.rust-lang.org/1.93.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.93.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.93.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>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>rand::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/iterator.rs.html#66-119">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.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/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.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>Assuming that the <a href="https://doc.rust-lang.org/1.93.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> is correct, this method
returns one uniformly-sampled random element of the slice, or <code>None</code>
only if the slice is empty. Incorrect bounds on the <code>size_hint</code> may
cause this method to incorrectly return <code>None</code> if fewer elements than
the advertised <code>lower</code> bound are present and may prevent sampling of
elements beyond an advertised <code>upper</code> bound (i.e. incorrect <code>size_hint</code>
is memory-safe, but may result in unexpected <code>None</code> result and
non-uniform distribution).</p>
<p>With an accurate <a href="https://doc.rust-lang.org/1.93.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> and where <a href="https://doc.rust-lang.org/1.93.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 further that <a href="https://doc.rust-lang.org/1.93.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> may affect the number of RNG
samples used as well as the result (while remaining uniform sampling).
Consider instead using <a href="trait.IteratorRandom.html#method.choose_stable" title="method rand::seq::IteratorRandom::choose_stable"><code>IteratorRandom::choose_stable</code></a> to avoid
<a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html" title="trait core::iter::traits::iterator::Iterator"><code>Iterator</code></a> combinators which only change size hints from affecting the
results.</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::IteratorRandom;
<span class="kw">let </span>words = <span class="string">"Mary had a little lamb"</span>.split(<span class="string">' '</span>);
<span class="macro">println!</span>(<span class="string">"{}"</span>, words.choose(<span class="kw-2">&amp;mut </span>rand::rng()).unwrap());</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.choose_stable" class="method"><a class="src rightside" href="../../src/rand/seq/iterator.rs.html#145-186">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.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/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.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 (stable)</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.93.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/iterator.rs.html#203-227">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.93.1/std/primitive.reference.html">&amp;mut R</a>, buf: &amp;mut [Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.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.93.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.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 into a buffer</p>
<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.IndexedRandom.html#method.choose_multiple" title="method rand::seq::IndexedRandom::choose_multiple"><code>IndexedRandom::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/iterator.rs.html#244-268">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.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="https://doc.rust-lang.org/1.93.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.93.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.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 into a <a href="https://doc.rust-lang.org/1.93.1/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec"><code>Vec</code></a></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.IndexedRandom.html#method.choose_multiple" title="method rand::seq::IndexedRandom::choose_multiple"><code>IndexedRandom::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.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="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/iterator.rs.html#271">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.93.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.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></div><script src="../../trait.impl/rand/seq/iterator/trait.IteratorRandom.js" async></script></section></div></main></body></html>

View File

@@ -0,0 +1,59 @@
<!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 slices, providing shuffling methods."><title>SliceRandom 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="#">SliceRandom</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="#">Slice<wbr>Random</a></h2><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.partial_shuffle" title="partial_shuffle">partial_shuffle</a></li><li><a href="#tymethod.shuffle" title="shuffle">shuffle</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-SliceRandom-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">Slice<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#316-362">Source</a> </span></div><pre class="rust item-decl"><code>pub trait SliceRandom: <a class="trait" href="trait.IndexedMutRandom.html" title="trait rand::seq::IndexedMutRandom">IndexedMutRandom</a> {
// Required methods
fn <a href="#tymethod.shuffle" class="fn">shuffle</a>&lt;R&gt;(&amp;mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</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.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>;
<span class="item-spacer"></span> fn <a href="#tymethod.partial_shuffle" class="fn">partial_shuffle</a>&lt;R&gt;(
&amp;mut 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; (&amp;mut [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>], &amp;mut [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>])
<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>;
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Extension trait on slices, providing shuffling 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"><code><span class="kw">use </span>rand::seq::SliceRandom;
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
<span class="kw">let </span><span class="kw-2">mut </span>bytes = <span class="string">"Hello, random!"</span>.to_string().into_bytes();
bytes.shuffle(<span class="kw-2">&amp;mut </span>rng);
<span class="kw">let </span>str = String::from_utf8(bytes).unwrap();
<span class="macro">println!</span>(<span class="string">"{}"</span>, str);</code></pre></div>
<p>Example output (non-deterministic):</p>
<div class="example-wrap"><pre class="language-none"><code>l,nmroHado !le</code></pre></div></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.shuffle" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#333-335">Source</a><h4 class="code-header">fn <a href="#tymethod.shuffle" class="fn">shuffle</a>&lt;R&gt;(&amp;mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</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.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h4></section></summary><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>.
The resulting permutation is picked uniformly from the set of all possible permutations.</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::SliceRandom;
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
<span class="kw">let </span><span class="kw-2">mut </span>y = [<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="string">"Unshuffled: {:?}"</span>, y);
y.shuffle(<span class="kw-2">&amp;mut </span>rng);
<span class="macro">println!</span>(<span class="string">"Shuffled: {:?}"</span>, y);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="tymethod.partial_shuffle" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#354-361">Source</a><h4 class="code-header">fn <a href="#tymethod.partial_shuffle" class="fn">partial_shuffle</a>&lt;R&gt;(
&amp;mut 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; (&amp;mut [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>], &amp;mut [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>])<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>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 &gt; self.len()/2</code>
then you may improve performance by taking
<code>amount = self.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></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-SliceRandom-for-%5BT%5D" class="impl"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#372-414">Source</a><a href="#impl-SliceRandom-for-%5BT%5D" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="trait.SliceRandom.html" title="trait rand::seq::SliceRandom">SliceRandom</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.shuffle" class="method trait-impl"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#373-382">Source</a><a href="#method.shuffle" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.shuffle" class="fn">shuffle</a>&lt;R&gt;(&amp;mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</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.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h4></section><section id="method.partial_shuffle" class="method trait-impl"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#384-413">Source</a><a href="#method.partial_shuffle" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.partial_shuffle" class="fn">partial_shuffle</a>&lt;R&gt;(
&amp;mut 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; (&amp;mut <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.slice.html">[T]</a>, &amp;mut <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.slice.html">[T]</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.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></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.SliceRandom.js" data-ignore-extern-crates="std" async></script></section></div></main></body></html>