]> Untitled Git - bitcoindevkit.org/blob
1265dd2342fa5cae11d31aa541726a4106f1354b
[bitcoindevkit.org] /
1 <!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."><title>bdk_chain::bitcoin::secp256k1::rand::distributions::uniform - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../../../../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../../../../../static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="../../../../../../" data-static-root-path="../../../../../../static.files/" data-current-crate="bdk_chain" data-themes="" data-resource-suffix="" data-rustdoc-version="1.80.0-nightly (78a775127 2024-05-11)" data-channel="nightly" data-search-js="search-d52510db62a78183.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../../../../../static.files/storage-118b08c4c78b968e.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../../../../../static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="../../../../../../static.files/noscript-df360f571f6edeae.css"></noscript><link rel="alternate icon" type="image/png" href="../../../../../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../../../../../static.files/favicon-2c020d218678b618.svg"></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" title="show sidebar"></button><a class="logo-container" href="../../../../../../bdk_chain/index.html"><img src="https://github.com/bitcoindevkit/bdk/raw/master/static/bdk.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../../../../../../bdk_chain/index.html"><img src="https://github.com/bitcoindevkit/bdk/raw/master/static/bdk.png" alt="logo"></a><h2><a href="../../../../../../bdk_chain/index.html">bdk_chain</a><span class="version">0.19.0</span></h2></div><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><h2><a href="../index.html">In bdk_chain::bitcoin::secp256k1::rand::distributions</a></h2></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Module <a href="../../../../../index.html">bdk_chain</a>::<wbr><a href="../../../../index.html">bitcoin</a>::<wbr><a href="../../../index.html">secp256k1</a>::<wbr><a href="../../index.html">rand</a>::<wbr><a href="../index.html">distributions</a>::<wbr><a class="mod" href="#">uniform</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="https://rust-random.github.io/rand/src/rand/distributions/mod.rs.html#110">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><details class="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>
2 <p><a href="../struct.Uniform.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::Uniform"><code>Uniform</code></a> is the standard distribution to sample uniformly from a range;
3 e.g. <code>Uniform::new_inclusive(1, 6)</code> can sample integers from 1 to 6, like a
4 standard die. <a href="../../trait.Rng.html#method.gen_range" title="method bdk_chain::bitcoin::secp256k1::rand::Rng::gen_range"><code>Rng::gen_range</code></a> supports any type supported by
5 <a href="../struct.Uniform.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::Uniform"><code>Uniform</code></a>.</p>
6 <p>This distribution is provided with support for several primitive types
7 (all integer and floating-point types) as well as <a href="https://doc.rust-lang.org/nightly/core/time/struct.Duration.html" title="struct core::time::Duration"><code>std::time::Duration</code></a>,
8 and supports extension to user-defined types via a type-specific <em>back-end</em>
9 implementation.</p>
10 <p>The types <a href="struct.UniformInt.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformInt"><code>UniformInt</code></a>, <a href="struct.UniformFloat.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformFloat"><code>UniformFloat</code></a> and <a href="struct.UniformDuration.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformDuration"><code>UniformDuration</code></a> are the
11 back-ends supporting sampling from primitive integer and floating-point
12 ranges as well as from <a href="https://doc.rust-lang.org/nightly/core/time/struct.Duration.html" title="struct core::time::Duration"><code>std::time::Duration</code></a>; these types do not normally
13 need to be used directly (unless implementing a derived back-end).</p>
14 <h2 id="example-usage"><a class="doc-anchor" href="#example-usage">§</a>Example usage</h2>
15 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::{Rng, thread_rng};
16 <span class="kw">use </span>rand::distributions::Uniform;
17
18 <span class="kw">let </span><span class="kw-2">mut </span>rng = thread_rng();
19 <span class="kw">let </span>side = Uniform::new(-<span class="number">10.0</span>, <span class="number">10.0</span>);
20
21 <span class="comment">// sample between 1 and 10 points
22 </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>) {
23 <span class="comment">// sample a point from the square with sides -10 - 10 in two dimensions
24 </span><span class="kw">let </span>(x, y) = (rng.sample(side), rng.sample(side));
25 <span class="macro">println!</span>(<span class="string">"Point: {}, {}"</span>, x, y);
26 }</code></pre></div>
27 <h2 id="extending-uniform-to-support-a-custom-type"><a class="doc-anchor" href="#extending-uniform-to-support-a-custom-type">§</a>Extending <code>Uniform</code> to support a custom type</h2>
28 <p>To extend <a href="../struct.Uniform.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::Uniform"><code>Uniform</code></a> to support your own types, write a back-end which
29 implements the <a href="trait.UniformSampler.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformSampler"><code>UniformSampler</code></a> trait, then implement the <a href="trait.SampleUniform.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::SampleUniform"><code>SampleUniform</code></a>
30 helper trait to “register” your back-end. See the <code>MyF32</code> example below.</p>
31 <p>At a minimum, the back-end needs to store any parameters needed for sampling
32 (e.g. the target range) and implement <code>new</code>, <code>new_inclusive</code> and <code>sample</code>.
33 Those methods should include an assert to check the range is valid (i.e.
34 <code>low &lt; high</code>). The example below merely wraps another back-end.</p>
35 <p>The <code>new</code>, <code>new_inclusive</code> and <code>sample_single</code> functions use arguments of
36 type SampleBorrow<X> in order to support passing in values by reference or
37 by value. In the implementation of these functions, you can choose to
38 simply use the reference returned by <a href="trait.SampleBorrow.html#tymethod.borrow" title="method bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::SampleBorrow::borrow"><code>SampleBorrow::borrow</code></a>, or you can choose
39 to copy or clone the value, whatever is appropriate for your type.</p>
40
41 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::prelude::<span class="kw-2">*</span>;
42 <span class="kw">use </span>rand::distributions::uniform::{Uniform, SampleUniform,
43 UniformSampler, UniformFloat, SampleBorrow};
44
45 <span class="kw">struct </span>MyF32(f32);
46
47 <span class="attr">#[derive(Clone, Copy, Debug)]
48 </span><span class="kw">struct </span>UniformMyF32(UniformFloat&lt;f32&gt;);
49
50 <span class="kw">impl </span>UniformSampler <span class="kw">for </span>UniformMyF32 {
51 <span class="kw">type </span>X = MyF32;
52 <span class="kw">fn </span>new&lt;B1, B2&gt;(low: B1, high: B2) -&gt; <span class="self">Self
53 </span><span class="kw">where </span>B1: SampleBorrow&lt;<span class="self">Self</span>::X&gt; + Sized,
54 B2: SampleBorrow&lt;<span class="self">Self</span>::X&gt; + Sized
55 {
56 UniformMyF32(UniformFloat::&lt;f32&gt;::new(low.borrow().<span class="number">0</span>, high.borrow().<span class="number">0</span>))
57 }
58 <span class="kw">fn </span>new_inclusive&lt;B1, B2&gt;(low: B1, high: B2) -&gt; <span class="self">Self
59 </span><span class="kw">where </span>B1: SampleBorrow&lt;<span class="self">Self</span>::X&gt; + Sized,
60 B2: SampleBorrow&lt;<span class="self">Self</span>::X&gt; + Sized
61 {
62 UniformMyF32(UniformFloat::&lt;f32&gt;::new_inclusive(
63 low.borrow().<span class="number">0</span>,
64 high.borrow().<span class="number">0</span>,
65 ))
66 }
67 <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 {
68 MyF32(<span class="self">self</span>.<span class="number">0</span>.sample(rng))
69 }
70 }
71
72 <span class="kw">impl </span>SampleUniform <span class="kw">for </span>MyF32 {
73 <span class="kw">type </span>Sampler = UniformMyF32;
74 }
75
76 <span class="kw">let </span>(low, high) = (MyF32(<span class="number">17.0f32</span>), MyF32(<span class="number">22.0f32</span>));
77 <span class="kw">let </span>uniform = Uniform::new(low, high);
78 <span class="kw">let </span>x = uniform.sample(<span class="kw-2">&amp;mut </span>thread_rng());</code></pre></div>
79 </div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.Uniform.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::Uniform">Uniform</a></div><div class="desc docblock-short">Sample values uniformly between two bounds.</div></li><li><div class="item-name"><a class="struct" href="struct.UniformChar.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformChar">UniformChar</a></div><div class="desc docblock-short">The back-end implementing <a href="trait.UniformSampler.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformSampler"><code>UniformSampler</code></a> for <code>char</code>.</div></li><li><div class="item-name"><a class="struct" href="struct.UniformDuration.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformDuration">UniformDuration</a></div><div class="desc docblock-short">The back-end implementing <a href="trait.UniformSampler.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformSampler"><code>UniformSampler</code></a> for <code>Duration</code>.</div></li><li><div class="item-name"><a class="struct" href="struct.UniformFloat.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformFloat">UniformFloat</a></div><div class="desc docblock-short">The back-end implementing <a href="trait.UniformSampler.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformSampler"><code>UniformSampler</code></a> for floating-point types.</div></li><li><div class="item-name"><a class="struct" href="struct.UniformInt.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformInt">UniformInt</a></div><div class="desc docblock-short">The back-end implementing <a href="trait.UniformSampler.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformSampler"><code>UniformSampler</code></a> for integer types.</div></li></ul><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="trait" href="trait.SampleBorrow.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::SampleBorrow">SampleBorrow</a></div><div class="desc docblock-short">Helper trait similar to <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html" title="trait core::borrow::Borrow"><code>Borrow</code></a> but implemented
80 only for SampleUniform and references to SampleUniform in
81 order to resolve ambiguity issues.</div></li><li><div class="item-name"><a class="trait" href="trait.SampleRange.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::SampleRange">SampleRange</a></div><div class="desc docblock-short">Range that supports generating a single sample efficiently.</div></li><li><div class="item-name"><a class="trait" href="trait.SampleUniform.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::SampleUniform">SampleUniform</a></div><div class="desc docblock-short">Helper trait for creating objects using the correct implementation of
82 <a href="trait.UniformSampler.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformSampler"><code>UniformSampler</code></a> for the sampling type.</div></li><li><div class="item-name"><a class="trait" href="trait.UniformSampler.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::UniformSampler">UniformSampler</a></div><div class="desc docblock-short">Helper trait handling actual uniform sampling.</div></li></ul></section></div></main></body></html>