blob: 7e63af031ab88058257dc345d0d492503cb73aa2 [file] [log] [blame]
<!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="A distribution uniformly sampling numbers within a given range."><meta name="keywords" content="rust, rustlang, rust-lang, uniform"><title>rand::distributions::uniform - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.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]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../../../rand/index.html"><div class="logo-container"><img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../rand/index.html"><div class="logo-container">
<img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></div></a><h2 class="location"><a href="#">Module uniform</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Module <a href="../../index.html">rand</a>::<wbr><a href="../index.html">distributions</a>::<wbr><a class="mod" href="#">uniform</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../../src/rand/distributions/uniform.rs.html#10-1658">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A distribution uniformly sampling numbers within a given range.</p>
<p><a href="../struct.Uniform.html" title="Uniform"><code>Uniform</code></a> is the standard distribution to sample uniformly from a range;
e.g. <code>Uniform::new_inclusive(1, 6)</code> can sample integers from 1 to 6, like a
standard die. <a href="../../trait.Rng.html#method.gen_range" title="Rng::gen_range"><code>Rng::gen_range</code></a> supports any type supported by
<a href="../struct.Uniform.html" title="Uniform"><code>Uniform</code></a>.</p>
<p>This distribution is provided with support for several primitive types
(all integer and floating-point types) as well as <a href="https://doc.rust-lang.org/nightly/core/time/struct.Duration.html" title="std::time::Duration"><code>std::time::Duration</code></a>,
and supports extension to user-defined types via a type-specific <em>back-end</em>
implementation.</p>
<p>The types <a href="struct.UniformInt.html"><code>UniformInt</code></a>, <a href="struct.UniformFloat.html"><code>UniformFloat</code></a> and <a href="struct.UniformDuration.html"><code>UniformDuration</code></a> are the
back-ends supporting sampling from primitive integer and floating-point
ranges as well as from <a href="https://doc.rust-lang.org/nightly/core/time/struct.Duration.html" title="std::time::Duration"><code>std::time::Duration</code></a>; these types do not normally
need to be used directly (unless implementing a derived back-end).</p>
<h2 id="example-usage"><a href="#example-usage">Example usage</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::{Rng, thread_rng};
<span class="kw">use </span>rand::distributions::Uniform;
<span class="kw">let </span><span class="kw-2">mut </span>rng = thread_rng();
<span class="kw">let </span>side = Uniform::new(-<span class="number">10.0</span>, <span class="number">10.0</span>);
<span class="comment">// sample between 1 and 10 points
</span><span class="kw">for _ in </span><span class="number">0</span>..rng.gen_range(<span class="number">1</span>..=<span class="number">10</span>) {
<span class="comment">// sample a point from the square with sides -10 - 10 in two dimensions
</span><span class="kw">let </span>(x, y) = (rng.sample(side), rng.sample(side));
<span class="macro">println!</span>(<span class="string">&quot;Point: {}, {}&quot;</span>, x, y);
}</code></pre></div>
<h2 id="extending-uniform-to-support-a-custom-type"><a href="#extending-uniform-to-support-a-custom-type">Extending <code>Uniform</code> to support a custom type</a></h2>
<p>To extend <a href="../struct.Uniform.html" title="Uniform"><code>Uniform</code></a> to support your own types, write a back-end which
implements the <a href="trait.UniformSampler.html"><code>UniformSampler</code></a> trait, then implement the <a href="trait.SampleUniform.html"><code>SampleUniform</code></a>
helper trait to “register” your back-end. See the <code>MyF32</code> example below.</p>
<p>At a minimum, the back-end needs to store any parameters needed for sampling
(e.g. the target range) and implement <code>new</code>, <code>new_inclusive</code> and <code>sample</code>.
Those methods should include an assert to check the range is valid (i.e.
<code>low &lt; high</code>). The example below merely wraps another back-end.</p>
<p>The <code>new</code>, <code>new_inclusive</code> and <code>sample_single</code> functions use arguments of
type SampleBorrow<X> in order to support passing in values by reference or
by value. In the implementation of these functions, you can choose to
simply use the reference returned by <a href="trait.SampleBorrow.html#tymethod.borrow"><code>SampleBorrow::borrow</code></a>, or you can choose
to copy or clone the value, whatever is appropriate for your type.</p>
<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">use </span>rand::distributions::uniform::{Uniform, SampleUniform,
UniformSampler, UniformFloat, SampleBorrow};
<span class="kw">struct </span>MyF32(f32);
<span class="attribute">#[derive(Clone, Copy, Debug)]
</span><span class="kw">struct </span>UniformMyF32(UniformFloat&lt;f32&gt;);
<span class="kw">impl </span>UniformSampler <span class="kw">for </span>UniformMyF32 {
<span class="kw">type </span>X = MyF32;
<span class="kw">fn </span>new&lt;B1, B2&gt;(low: B1, high: B2) -&gt; <span class="self">Self
</span><span class="kw">where </span>B1: SampleBorrow&lt;<span class="self">Self</span>::X&gt; + Sized,
B2: SampleBorrow&lt;<span class="self">Self</span>::X&gt; + Sized
{
UniformMyF32(UniformFloat::&lt;f32&gt;::new(low.borrow().<span class="number">0</span>, high.borrow().<span class="number">0</span>))
}
<span class="kw">fn </span>new_inclusive&lt;B1, B2&gt;(low: B1, high: B2) -&gt; <span class="self">Self
</span><span class="kw">where </span>B1: SampleBorrow&lt;<span class="self">Self</span>::X&gt; + Sized,
B2: SampleBorrow&lt;<span class="self">Self</span>::X&gt; + Sized
{
UniformMyF32(UniformFloat::&lt;f32&gt;::new_inclusive(
low.borrow().<span class="number">0</span>,
high.borrow().<span class="number">0</span>,
))
}
<span class="kw">fn </span>sample&lt;R: Rng + <span class="question-mark">?</span>Sized&gt;(<span class="kw-2">&amp;</span><span class="self">self</span>, rng: <span class="kw-2">&amp;mut </span>R) -&gt; <span class="self">Self</span>::X {
MyF32(<span class="self">self</span>.<span class="number">0</span>.sample(rng))
}
}
<span class="kw">impl </span>SampleUniform <span class="kw">for </span>MyF32 {
<span class="kw">type </span>Sampler = UniformMyF32;
}
<span class="kw">let </span>(low, high) = (MyF32(<span class="number">17.0f32</span>), MyF32(<span class="number">22.0f32</span>));
<span class="kw">let </span>uniform = Uniform::new(low, high);
<span class="kw">let </span>x = uniform.sample(<span class="kw-2">&amp;mut </span>thread_rng());</code></pre></div>
</div></details><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Uniform.html" title="rand::distributions::uniform::Uniform struct">Uniform</a></div><div class="item-right docblock-short">Sample values uniformly between two bounds.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.UniformChar.html" title="rand::distributions::uniform::UniformChar struct">UniformChar</a></div><div class="item-right docblock-short">The back-end implementing <a href="trait.UniformSampler.html" title="UniformSampler"><code>UniformSampler</code></a> for <code>char</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.UniformDuration.html" title="rand::distributions::uniform::UniformDuration struct">UniformDuration</a></div><div class="item-right docblock-short">The back-end implementing <a href="trait.UniformSampler.html" title="UniformSampler"><code>UniformSampler</code></a> for <code>Duration</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.UniformFloat.html" title="rand::distributions::uniform::UniformFloat struct">UniformFloat</a></div><div class="item-right docblock-short">The back-end implementing <a href="trait.UniformSampler.html" title="UniformSampler"><code>UniformSampler</code></a> for floating-point types.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.UniformInt.html" title="rand::distributions::uniform::UniformInt struct">UniformInt</a></div><div class="item-right docblock-short">The back-end implementing <a href="trait.UniformSampler.html" title="UniformSampler"><code>UniformSampler</code></a> for integer types.</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.SampleBorrow.html" title="rand::distributions::uniform::SampleBorrow trait">SampleBorrow</a></div><div class="item-right docblock-short">Helper trait similar to <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html"><code>Borrow</code></a> but implemented
only for SampleUniform and references to SampleUniform in
order to resolve ambiguity issues.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.SampleRange.html" title="rand::distributions::uniform::SampleRange trait">SampleRange</a></div><div class="item-right docblock-short">Range that supports generating a single sample efficiently.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.SampleUniform.html" title="rand::distributions::uniform::SampleUniform trait">SampleUniform</a></div><div class="item-right docblock-short">Helper trait for creating objects using the correct implementation of
<a href="trait.UniformSampler.html" title="UniformSampler"><code>UniformSampler</code></a> for the sampling type.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.UniformSampler.html" title="rand::distributions::uniform::UniformSampler trait">UniformSampler</a></div><div class="item-right docblock-short">Helper trait handling actual uniform sampling.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="rand" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>