192 lines
34 KiB
HTML
192 lines
34 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="User-level interface for RNGs"><title>Rng in rand - 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="#">Rng</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="#">Rng</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#generic-usage" title="Generic usage">Generic usage</a></li></ul><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.fill" title="fill">fill</a></li><li><a href="#method.gen" title="gen">gen</a></li><li><a href="#method.gen_bool" title="gen_bool">gen_bool</a></li><li><a href="#method.gen_range" title="gen_range">gen_range</a></li><li><a href="#method.gen_ratio" title="gen_ratio">gen_ratio</a></li><li><a href="#method.random" title="random">random</a></li><li><a href="#method.random_bool" title="random_bool">random_bool</a></li><li><a href="#method.random_iter" title="random_iter">random_iter</a></li><li><a href="#method.random_range" title="random_range">random_range</a></li><li><a href="#method.random_ratio" title="random_ratio">random_ratio</a></li><li><a href="#method.sample" title="sample">sample</a></li><li><a href="#method.sample_iter" title="sample_iter">sample_iter</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 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>Trait <span class="trait">Rng</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/rand/rng.rs.html#58-355">Source</a> </span></div><pre class="rust item-decl"><code>pub trait Rng: <a class="trait" href="trait.RngCore.html" title="trait rand::RngCore">RngCore</a> {
|
|
// Provided methods
|
|
fn <a href="#method.random" class="fn">random</a><T>(&mut self) -> T
|
|
<span class="where">where <a class="struct" href="distr/struct.StandardUniform.html" title="struct rand::distr::StandardUniform">StandardUniform</a>: <a class="trait" href="distr/trait.Distribution.html" title="trait rand::distr::Distribution">Distribution</a><T></span> { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.random_iter" class="fn">random_iter</a><T>(self) -> <a class="struct" href="distr/struct.Iter.html" title="struct rand::distr::Iter">Iter</a><<a class="struct" href="distr/struct.StandardUniform.html" title="struct rand::distr::StandardUniform">StandardUniform</a>, Self, T> <a href="#" class="tooltip" data-notable-ty="Iter<StandardUniform, Self, T>">ⓘ</a>
|
|
<span class="where">where Self: <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 class="struct" href="distr/struct.StandardUniform.html" title="struct rand::distr::StandardUniform">StandardUniform</a>: <a class="trait" href="distr/trait.Distribution.html" title="trait rand::distr::Distribution">Distribution</a><T></span> { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.random_range" class="fn">random_range</a><T, R>(&mut self, range: R) -> T
|
|
<span class="where">where T: <a class="trait" href="distr/uniform/trait.SampleUniform.html" title="trait rand::distr::uniform::SampleUniform">SampleUniform</a>,
|
|
R: <a class="trait" href="distr/uniform/trait.SampleRange.html" title="trait rand::distr::uniform::SampleRange">SampleRange</a><T></span> { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.random_bool" class="fn">random_bool</a>(&mut self, p: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.f64.html">f64</a>) -> <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.random_ratio" class="fn">random_ratio</a>(&mut self, numerator: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>, denominator: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>) -> <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.sample" class="fn">sample</a><T, D: <a class="trait" href="distr/trait.Distribution.html" title="trait rand::distr::Distribution">Distribution</a><T>>(&mut self, distr: D) -> T { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.sample_iter" class="fn">sample_iter</a><T, D>(self, distr: D) -> <a class="struct" href="distr/struct.Iter.html" title="struct rand::distr::Iter">Iter</a><D, Self, T> <a href="#" class="tooltip" data-notable-ty="Iter<D, Self, T>">ⓘ</a>
|
|
<span class="where">where D: <a class="trait" href="distr/trait.Distribution.html" title="trait rand::distr::Distribution">Distribution</a><T>,
|
|
Self: <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.fill" class="fn">fill</a><T: <a class="trait" href="trait.Fill.html" title="trait rand::Fill">Fill</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>>(&mut self, dest: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&mut T</a>) { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.gen" class="fn">gen</a><T>(&mut self) -> T
|
|
<span class="where">where <a class="struct" href="distr/struct.StandardUniform.html" title="struct rand::distr::StandardUniform">StandardUniform</a>: <a class="trait" href="distr/trait.Distribution.html" title="trait rand::distr::Distribution">Distribution</a><T></span> { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.gen_range" class="fn">gen_range</a><T, R>(&mut self, range: R) -> T
|
|
<span class="where">where T: <a class="trait" href="distr/uniform/trait.SampleUniform.html" title="trait rand::distr::uniform::SampleUniform">SampleUniform</a>,
|
|
R: <a class="trait" href="distr/uniform/trait.SampleRange.html" title="trait rand::distr::uniform::SampleRange">SampleRange</a><T></span> { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.gen_bool" class="fn">gen_bool</a>(&mut self, p: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.f64.html">f64</a>) -> <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.gen_ratio" class="fn">gen_ratio</a>(&mut self, numerator: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>, denominator: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a> { ... }
|
|
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>User-level interface for RNGs</p>
|
|
<p><a href="trait.RngCore.html" title="trait rand::RngCore"><code>RngCore</code></a> is the <code>dyn</code>-safe implementation-level interface for Random
|
|
(Number) Generators. This trait, <code>Rng</code>, provides a user-level interface on
|
|
RNGs. It is implemented automatically for any <code>R: RngCore</code>.</p>
|
|
<p>This trait must usually be brought into scope via <code>use rand::Rng;</code> or
|
|
<code>use rand::prelude::*;</code>.</p>
|
|
<h2 id="generic-usage"><a class="doc-anchor" href="#generic-usage">§</a>Generic usage</h2>
|
|
<p>The basic pattern is <code>fn foo<R: Rng + ?Sized>(rng: &mut R)</code>. Some
|
|
things are worth noting here:</p>
|
|
<ul>
|
|
<li>Since <code>Rng: RngCore</code> and every <code>RngCore</code> implements <code>Rng</code>, it makes no
|
|
difference whether we use <code>R: Rng</code> or <code>R: RngCore</code>.</li>
|
|
<li>The <code>+ ?Sized</code> un-bounding allows functions to be called directly on
|
|
type-erased references; i.e. <code>foo(r)</code> where <code>r: &mut dyn RngCore</code>. Without
|
|
this it would be necessary to write <code>foo(&mut r)</code>.</li>
|
|
</ul>
|
|
<p>An alternative pattern is possible: <code>fn foo<R: Rng>(rng: R)</code>. This has some
|
|
trade-offs. It allows the argument to be consumed directly without a <code>&mut</code>
|
|
(which is how <code>from_rng(rand::rng())</code> works); also it still works directly
|
|
on references (including type-erased references). Unfortunately within the
|
|
function <code>foo</code> it is not known whether <code>rng</code> is a reference type or not,
|
|
hence many uses of <code>rng</code> require an extra reference, either explicitly
|
|
(<code>distr.sample(&mut rng)</code>) or implicitly (<code>rng.random()</code>); one may hope the
|
|
optimiser can remove redundant references later.</p>
|
|
<p>Example:</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::Rng;
|
|
|
|
<span class="kw">fn </span>foo<R: Rng + <span class="question-mark">?</span>Sized>(rng: <span class="kw-2">&mut </span>R) -> f32 {
|
|
rng.random()
|
|
}
|
|
</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.random" class="method"><a class="src rightside" href="../src/rand/rng.rs.html#95-100">Source</a><h4 class="code-header">fn <a href="#method.random" class="fn">random</a><T>(&mut self) -> T<div class="where">where
|
|
<a class="struct" href="distr/struct.StandardUniform.html" title="struct rand::distr::StandardUniform">StandardUniform</a>: <a class="trait" href="distr/trait.Distribution.html" title="trait rand::distr::Distribution">Distribution</a><T>,</div></h4></section></summary><div class="docblock"><p>Return a random value via the <a href="distr/struct.StandardUniform.html" title="struct rand::distr::StandardUniform"><code>StandardUniform</code></a> distribution.</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::Rng;
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
|
|
<span class="kw">let </span>x: u32 = rng.random();
|
|
<span class="macro">println!</span>(<span class="string">"{}"</span>, x);
|
|
<span class="macro">println!</span>(<span class="string">"{:?}"</span>, rng.random::<(f64, bool)>());</code></pre></div><h5 id="arrays-and-tuples"><a class="doc-anchor" href="#arrays-and-tuples">§</a>Arrays and tuples</h5>
|
|
<p>The <code>rng.random()</code> method is able to generate arrays
|
|
and tuples (up to 12 elements), so long as all element types can be
|
|
generated.</p>
|
|
<p>For arrays of integers, especially for those with small element types
|
|
(< 64 bit), it will likely be faster to instead use <a href="trait.Rng.html#method.fill" title="method rand::Rng::fill"><code>Rng::fill</code></a>,
|
|
though note that generated values will differ.</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::Rng;
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
|
|
<span class="kw">let </span>tuple: (u8, i32, char) = rng.random(); <span class="comment">// arbitrary tuple support
|
|
|
|
</span><span class="kw">let </span>arr1: [f32; <span class="number">32</span>] = rng.random(); <span class="comment">// array construction
|
|
</span><span class="kw">let </span><span class="kw-2">mut </span>arr2 = [<span class="number">0u8</span>; <span class="number">128</span>];
|
|
rng.fill(<span class="kw-2">&mut </span>arr2); <span class="comment">// array fill</span></code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.random_iter" class="method"><a class="src rightside" href="../src/rand/rng.rs.html#120-126">Source</a><h4 class="code-header">fn <a href="#method.random_iter" class="fn">random_iter</a><T>(self) -> <a class="struct" href="distr/struct.Iter.html" title="struct rand::distr::Iter">Iter</a><<a class="struct" href="distr/struct.StandardUniform.html" title="struct rand::distr::StandardUniform">StandardUniform</a>, Self, T> <a href="#" class="tooltip" data-notable-ty="Iter<StandardUniform, Self, T>">ⓘ</a><div class="where">where
|
|
Self: <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 class="struct" href="distr/struct.StandardUniform.html" title="struct rand::distr::StandardUniform">StandardUniform</a>: <a class="trait" href="distr/trait.Distribution.html" title="trait rand::distr::Distribution">Distribution</a><T>,</div></h4></section></summary><div class="docblock"><p>Return an iterator over <a href="trait.Rng.html#method.random"><code>random</code></a> variates</p>
|
|
<p>This is a just a wrapper over <a href="trait.Rng.html#method.sample_iter" title="method rand::Rng::sample_iter"><code>Rng::sample_iter</code></a> using
|
|
<a href="distr/struct.StandardUniform.html" title="struct rand::distr::StandardUniform"><code>distr::StandardUniform</code></a>.</p>
|
|
<p>Note: this method consumes its argument. Use
|
|
<code>(&mut rng).random_iter()</code> to avoid consuming the RNG.</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::{rngs::SmallRng, Rng, SeedableRng};
|
|
|
|
<span class="kw">let </span>rng = SmallRng::seed_from_u64(<span class="number">0</span>);
|
|
<span class="kw">let </span>v: Vec<i32> = rng.random_iter().take(<span class="number">5</span>).collect();
|
|
<span class="macro">assert_eq!</span>(v.len(), <span class="number">5</span>);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.random_range" class="method"><a class="src rightside" href="../src/rand/rng.rs.html#161-168">Source</a><h4 class="code-header">fn <a href="#method.random_range" class="fn">random_range</a><T, R>(&mut self, range: R) -> T<div class="where">where
|
|
T: <a class="trait" href="distr/uniform/trait.SampleUniform.html" title="trait rand::distr::uniform::SampleUniform">SampleUniform</a>,
|
|
R: <a class="trait" href="distr/uniform/trait.SampleRange.html" title="trait rand::distr::uniform::SampleRange">SampleRange</a><T>,</div></h4></section></summary><div class="docblock"><p>Generate a random value in the given range.</p>
|
|
<p>This function is optimised for the case that only a single sample is
|
|
made from the given range. See also the <a href="distr/struct.Uniform.html" title="struct rand::distr::Uniform"><code>Uniform</code></a> distribution
|
|
type which may be faster if sampling from the same range repeatedly.</p>
|
|
<p>All types support <code>low..high_exclusive</code> and <code>low..=high</code> range syntax.
|
|
Unsigned integer types also support <code>..high_exclusive</code> and <code>..=high</code> syntax.</p>
|
|
<h5 id="panics"><a class="doc-anchor" href="#panics">§</a>Panics</h5>
|
|
<p>Panics if the range is empty, or if <code>high - low</code> overflows for floats.</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::Rng;
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
|
|
|
|
<span class="comment">// Exclusive range
|
|
</span><span class="kw">let </span>n: u32 = rng.random_range(..<span class="number">10</span>);
|
|
<span class="macro">println!</span>(<span class="string">"{}"</span>, n);
|
|
<span class="kw">let </span>m: f64 = rng.random_range(-<span class="number">40.0</span>..<span class="number">1.3e5</span>);
|
|
<span class="macro">println!</span>(<span class="string">"{}"</span>, m);
|
|
|
|
<span class="comment">// Inclusive range
|
|
</span><span class="kw">let </span>n: u32 = rng.random_range(..=<span class="number">10</span>);
|
|
<span class="macro">println!</span>(<span class="string">"{}"</span>, n);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.random_bool" class="method"><a class="src rightside" href="../src/rand/rng.rs.html#191-196">Source</a><h4 class="code-header">fn <a href="#method.random_bool" class="fn">random_bool</a>(&mut self, p: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.f64.html">f64</a>) -> <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>Return a bool with a probability <code>p</code> of being true.</p>
|
|
<p>See also the <a href="distr/struct.Bernoulli.html" title="struct rand::distr::Bernoulli"><code>Bernoulli</code></a> distribution, which may be faster if
|
|
sampling from the same probability repeatedly.</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::Rng;
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
|
|
<span class="macro">println!</span>(<span class="string">"{}"</span>, rng.random_bool(<span class="number">1.0 </span>/ <span class="number">3.0</span>));</code></pre></div><h5 id="panics-1"><a class="doc-anchor" href="#panics-1">§</a>Panics</h5>
|
|
<p>If <code>p < 0</code> or <code>p > 1</code>.</p>
|
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.random_ratio" class="method"><a class="src rightside" href="../src/rand/rng.rs.html#225-233">Source</a><h4 class="code-header">fn <a href="#method.random_ratio" class="fn">random_ratio</a>(&mut self, numerator: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>, denominator: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>) -> <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>Return a bool with a probability of <code>numerator/denominator</code> of being
|
|
true.</p>
|
|
<p>That is, <code>random_ratio(2, 3)</code> has chance of 2 in 3, or about 67%, of
|
|
returning true. If <code>numerator == denominator</code>, then the returned value
|
|
is guaranteed to be <code>true</code>. If <code>numerator == 0</code>, then the returned
|
|
value is guaranteed to be <code>false</code>.</p>
|
|
<p>See also the <a href="distr/struct.Bernoulli.html" title="struct rand::distr::Bernoulli"><code>Bernoulli</code></a> distribution, which may be faster if
|
|
sampling from the same <code>numerator</code> and <code>denominator</code> repeatedly.</p>
|
|
<h5 id="panics-2"><a class="doc-anchor" href="#panics-2">§</a>Panics</h5>
|
|
<p>If <code>denominator == 0</code> or <code>numerator > denominator</code>.</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::Rng;
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
|
|
<span class="macro">println!</span>(<span class="string">"{}"</span>, rng.random_ratio(<span class="number">2</span>, <span class="number">3</span>));</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.sample" class="method"><a class="src rightside" href="../src/rand/rng.rs.html#249-251">Source</a><h4 class="code-header">fn <a href="#method.sample" class="fn">sample</a><T, D: <a class="trait" href="distr/trait.Distribution.html" title="trait rand::distr::Distribution">Distribution</a><T>>(&mut self, distr: D) -> T</h4></section></summary><div class="docblock"><p>Sample a new value, using the given distribution.</p>
|
|
<h6 id="example-5"><a class="doc-anchor" href="#example-5">§</a>Example</h6>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::Rng;
|
|
<span class="kw">use </span>rand::distr::Uniform;
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
|
|
<span class="kw">let </span>x = rng.sample(Uniform::new(<span class="number">10u32</span>, <span class="number">15</span>).unwrap());
|
|
<span class="comment">// Type annotation requires two types, the type and distribution; the
|
|
// distribution can be inferred.
|
|
</span><span class="kw">let </span>y = rng.sample::<u16, <span class="kw">_</span>>(Uniform::new(<span class="number">10</span>, <span class="number">15</span>).unwrap());</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.sample_iter" class="method"><a class="src rightside" href="../src/rand/rng.rs.html#286-292">Source</a><h4 class="code-header">fn <a href="#method.sample_iter" class="fn">sample_iter</a><T, D>(self, distr: D) -> <a class="struct" href="distr/struct.Iter.html" title="struct rand::distr::Iter">Iter</a><D, Self, T> <a href="#" class="tooltip" data-notable-ty="Iter<D, Self, T>">ⓘ</a><div class="where">where
|
|
D: <a class="trait" href="distr/trait.Distribution.html" title="trait rand::distr::Distribution">Distribution</a><T>,
|
|
Self: <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>Create an iterator that generates values using the given distribution.</p>
|
|
<p>Note: this method consumes its arguments. Use
|
|
<code>(&mut rng).sample_iter(..)</code> to avoid consuming the RNG.</p>
|
|
<h5 id="example-6"><a class="doc-anchor" href="#example-6">§</a>Example</h5>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::Rng;
|
|
<span class="kw">use </span>rand::distr::{Alphanumeric, Uniform, StandardUniform};
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
|
|
|
|
<span class="comment">// Vec of 16 x f32:
|
|
</span><span class="kw">let </span>v: Vec<f32> = (<span class="kw-2">&mut </span>rng).sample_iter(StandardUniform).take(<span class="number">16</span>).collect();
|
|
|
|
<span class="comment">// String:
|
|
</span><span class="kw">let </span>s: String = (<span class="kw-2">&mut </span>rng).sample_iter(Alphanumeric)
|
|
.take(<span class="number">7</span>)
|
|
.map(char::from)
|
|
.collect();
|
|
|
|
<span class="comment">// Combined values
|
|
</span><span class="macro">println!</span>(<span class="string">"{:?}"</span>, (<span class="kw-2">&mut </span>rng).sample_iter(StandardUniform).take(<span class="number">5</span>)
|
|
.collect::<Vec<(f64, bool)>>());
|
|
|
|
<span class="comment">// Dice-rolling:
|
|
</span><span class="kw">let </span>die_range = Uniform::new_inclusive(<span class="number">1</span>, <span class="number">6</span>).unwrap();
|
|
<span class="kw">let </span><span class="kw-2">mut </span>roll_die = (<span class="kw-2">&mut </span>rng).sample_iter(die_range);
|
|
<span class="kw">while </span>roll_die.next().unwrap() != <span class="number">6 </span>{
|
|
<span class="macro">println!</span>(<span class="string">"Not a 6; rolling again!"</span>);
|
|
}</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.fill" class="method"><a class="src rightside" href="../src/rand/rng.rs.html#314-316">Source</a><h4 class="code-header">fn <a href="#method.fill" class="fn">fill</a><T: <a class="trait" href="trait.Fill.html" title="trait rand::Fill">Fill</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>>(&mut self, dest: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&mut T</a>)</h4></section></summary><div class="docblock"><p>Fill any type implementing <a href="trait.Fill.html" title="trait rand::Fill"><code>Fill</code></a> with random data</p>
|
|
<p>This method is implemented for types which may be safely reinterpreted
|
|
as an (aligned) <code>[u8]</code> slice then filled with random data. It is often
|
|
faster than using <a href="trait.Rng.html#method.random" title="method rand::Rng::random"><code>Rng::random</code></a> but not value-equivalent.</p>
|
|
<p>The distribution is expected to be uniform with portable results, but
|
|
this cannot be guaranteed for third-party implementations.</p>
|
|
<h5 id="example-7"><a class="doc-anchor" href="#example-7">§</a>Example</h5>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::Rng;
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>arr = [<span class="number">0i8</span>; <span class="number">20</span>];
|
|
rand::rng().fill(<span class="kw-2">&mut </span>arr[..]);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.gen" class="method"><a class="src rightside" href="../src/rand/rng.rs.html#324-329">Source</a><h4 class="code-header">fn <a href="#method.gen" class="fn">gen</a><T>(&mut self) -> T<div class="where">where
|
|
<a class="struct" href="distr/struct.StandardUniform.html" title="struct rand::distr::StandardUniform">StandardUniform</a>: <a class="trait" href="distr/trait.Distribution.html" title="trait rand::distr::Distribution">Distribution</a><T>,</div></h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated since 0.9.0: Renamed to <code>random</code> to avoid conflict with the new <code>gen</code> keyword in Rust 2024.</span></div></span></summary><div class="docblock"><p>Alias for <a href="trait.Rng.html#method.random" title="method rand::Rng::random"><code>Rng::random</code></a>.</p>
|
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.gen_range" class="method"><a class="src rightside" href="../src/rand/rng.rs.html#334-340">Source</a><h4 class="code-header">fn <a href="#method.gen_range" class="fn">gen_range</a><T, R>(&mut self, range: R) -> T<div class="where">where
|
|
T: <a class="trait" href="distr/uniform/trait.SampleUniform.html" title="trait rand::distr::uniform::SampleUniform">SampleUniform</a>,
|
|
R: <a class="trait" href="distr/uniform/trait.SampleRange.html" title="trait rand::distr::uniform::SampleRange">SampleRange</a><T>,</div></h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated since 0.9.0: Renamed to <code>random_range</code></span></div></span></summary><div class="docblock"><p>Alias for <a href="trait.Rng.html#method.random_range" title="method rand::Rng::random_range"><code>Rng::random_range</code></a>.</p>
|
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.gen_bool" class="method"><a class="src rightside" href="../src/rand/rng.rs.html#345-347">Source</a><h4 class="code-header">fn <a href="#method.gen_bool" class="fn">gen_bool</a>(&mut self, p: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.f64.html">f64</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a></h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated since 0.9.0: Renamed to <code>random_bool</code></span></div></span></summary><div class="docblock"><p>Alias for <a href="trait.Rng.html#method.random_bool" title="method rand::Rng::random_bool"><code>Rng::random_bool</code></a>.</p>
|
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.gen_ratio" class="method"><a class="src rightside" href="../src/rand/rng.rs.html#352-354">Source</a><h4 class="code-header">fn <a href="#method.gen_ratio" class="fn">gen_ratio</a>(&mut self, numerator: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>, denominator: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.u32.html">u32</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a></h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated since 0.9.0: Renamed to <code>random_ratio</code></span></div></span></summary><div class="docblock"><p>Alias for <a href="trait.Rng.html#method.random_ratio" title="method rand::Rng::random_ratio"><code>Rng::random_ratio</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-Rng-for-R" class="impl"><a class="src rightside" href="../src/rand/rng.rs.html#357">Source</a><a href="#impl-Rng-for-R" class="anchor">§</a><h3 class="code-header">impl<R: <a class="trait" href="trait.RngCore.html" title="trait rand::RngCore">RngCore</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>> <a class="trait" href="trait.Rng.html" title="trait rand::Rng">Rng</a> for R</h3></section></div><script src="../trait.impl/rand/rng/trait.Rng.js" async></script><script type="text/json" id="notable-traits-data">{"Iter<D, Self, T>":"<h3>Notable traits for <code><a class=\"struct\" href=\"distr/struct.Iter.html\" title=\"struct rand::distr::Iter\">Iter</a><D, R, T></code></h3><pre><code><div class=\"where\">impl<D, R, T> <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=\"distr/struct.Iter.html\" title=\"struct rand::distr::Iter\">Iter</a><D, R, T><div class=\"where\">where\n D: <a class=\"trait\" href=\"distr/trait.Distribution.html\" title=\"trait rand::distr::Distribution\">Distribution</a><T>,\n R: <a class=\"trait\" href=\"trait.Rng.html\" title=\"trait rand::Rng\">Rng</a>,</div></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> = T;</div>","Iter<StandardUniform, Self, T>":"<h3>Notable traits for <code><a class=\"struct\" href=\"distr/struct.Iter.html\" title=\"struct rand::distr::Iter\">Iter</a><D, R, T></code></h3><pre><code><div class=\"where\">impl<D, R, T> <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=\"distr/struct.Iter.html\" title=\"struct rand::distr::Iter\">Iter</a><D, R, T><div class=\"where\">where\n D: <a class=\"trait\" href=\"distr/trait.Distribution.html\" title=\"trait rand::distr::Distribution\">Distribution</a><T>,\n R: <a class=\"trait\" href=\"trait.Rng.html\" title=\"trait rand::Rng\">Rng</a>,</div></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> = T;</div>"}</script></section></div></main></body></html> |