]> Untitled Git - bitcoindevkit.org/blob
948ec25aa5fdc7c9862f234f7e4ea9d9327e4a76
[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="An automatically-implemented extension trait on `RngCore` providing high-level generic methods for sampling values and other convenience methods."><title>Rng in bdk_chain::bitcoin::secp256k1::rand::prelude - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.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" crossorigin href="../../../../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../../../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../../../../static.files/rustdoc-46132b98.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.85.0-nightly (4ba4ac612 2024-12-18)" data-channel="nightly" data-search-js="search-036cda7a.js" data-settings-js="settings-0f613d39.js" ><script src="../../../../../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../../../../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../../../../../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../../../../../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../../../../../static.files/favicon-044be391.svg"></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]--><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_<wbr>chain</a><span class="version">0.21.1</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.sample" title="sample">sample</a></li><li><a href="#method.sample_iter" title="sample_iter">sample_iter</a></li><li><a href="#method.try_fill" title="try_fill">try_fill</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 bdk_<wbr>chain::<wbr>bitcoin::<wbr>secp256k1::<wbr>rand::<wbr>prelude</a></h2></div></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"><span class="rustdoc-breadcrumbs"><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">prelude</a></span><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="https://rust-random.github.io/rand/src/rand/rng.rs.html#55">Source</a> </span></div><pre class="rust item-decl"><code>pub trait Rng: <a class="trait" href="../trait.RngCore.html" title="trait bdk_chain::bitcoin::secp256k1::rand::RngCore">RngCore</a> {
2 // Provided methods
3 fn <a href="#method.gen" class="fn">gen</a>&lt;T&gt;(&amp;mut self) -&gt; T
4 <span class="where">where <a class="struct" href="../distributions/struct.Standard.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::Standard">Standard</a>: <a class="trait" href="trait.Distribution.html" title="trait bdk_chain::bitcoin::secp256k1::rand::prelude::Distribution">Distribution</a>&lt;T&gt;</span> { ... }
5 <span class="item-spacer"></span> fn <a href="#method.gen_range" class="fn">gen_range</a>&lt;T, R&gt;(&amp;mut self, range: R) -&gt; T
6 <span class="where">where T: <a class="trait" href="../distributions/uniform/trait.SampleUniform.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::SampleUniform">SampleUniform</a>,
7 R: <a class="trait" href="../distributions/uniform/trait.SampleRange.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::SampleRange">SampleRange</a>&lt;T&gt;</span> { ... }
8 <span class="item-spacer"></span> fn <a href="#method.sample" class="fn">sample</a>&lt;T, D&gt;(&amp;mut self, distr: D) -&gt; T
9 <span class="where">where D: <a class="trait" href="trait.Distribution.html" title="trait bdk_chain::bitcoin::secp256k1::rand::prelude::Distribution">Distribution</a>&lt;T&gt;</span> { ... }
10 <span class="item-spacer"></span> fn <a href="#method.sample_iter" class="fn">sample_iter</a>&lt;T, D&gt;(self, distr: D) -&gt; <a class="struct" href="../distributions/struct.DistIter.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::DistIter">DistIter</a>&lt;D, Self, T&gt; <a href="#" class="tooltip" data-notable-ty="DistIter&lt;D, Self, T&gt;">ⓘ</a>
11 <span class="where">where D: <a class="trait" href="trait.Distribution.html" title="trait bdk_chain::bitcoin::secp256k1::rand::prelude::Distribution">Distribution</a>&lt;T&gt;,
12 Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
13 <span class="item-spacer"></span> fn <a href="#method.fill" class="fn">fill</a>&lt;T&gt;(&amp;mut self, dest: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut T</a>)
14 <span class="where">where T: <a class="trait" href="../trait.Fill.html" title="trait bdk_chain::bitcoin::secp256k1::rand::Fill">Fill</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
15 <span class="item-spacer"></span> fn <a href="#method.try_fill" class="fn">try_fill</a>&lt;T&gt;(&amp;mut self, dest: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut T</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="../struct.Error.html" title="struct bdk_chain::bitcoin::secp256k1::rand::Error">Error</a>&gt;
16 <span class="where">where T: <a class="trait" href="../trait.Fill.html" title="trait bdk_chain::bitcoin::secp256k1::rand::Fill">Fill</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
17 <span class="item-spacer"></span> fn <a href="#method.gen_bool" class="fn">gen_bool</a>(&amp;mut self, p: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f64.html">f64</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a> { ... }
18 <span class="item-spacer"></span> fn <a href="#method.gen_ratio" class="fn">gen_ratio</a>(&amp;mut self, numerator: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, denominator: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a> { ... }
19 }</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>An automatically-implemented extension trait on <a href="../trait.RngCore.html" title="trait bdk_chain::bitcoin::secp256k1::rand::RngCore"><code>RngCore</code></a> providing high-level
20 generic methods for sampling values and other convenience methods.</p>
21 <p>This is the primary trait to use when generating random values.</p>
22 <h2 id="generic-usage"><a class="doc-anchor" href="#generic-usage">§</a>Generic usage</h2>
23 <p>The basic pattern is <code>fn foo&lt;R: Rng + ?Sized&gt;(rng: &amp;mut R)</code>. Some
24 things are worth noting here:</p>
25 <ul>
26 <li>Since <code>Rng: RngCore</code> and every <code>RngCore</code> implements <code>Rng</code>, it makes no
27 difference whether we use <code>R: Rng</code> or <code>R: RngCore</code>.</li>
28 <li>The <code>+ ?Sized</code> un-bounding allows functions to be called directly on
29 type-erased references; i.e. <code>foo(r)</code> where <code>r: &amp;mut dyn RngCore</code>. Without
30 this it would be necessary to write <code>foo(&amp;mut r)</code>.</li>
31 </ul>
32 <p>An alternative pattern is possible: <code>fn foo&lt;R: Rng&gt;(rng: R)</code>. This has some
33 trade-offs. It allows the argument to be consumed directly without a <code>&amp;mut</code>
34 (which is how <code>from_rng(thread_rng())</code> works); also it still works directly
35 on references (including type-erased references). Unfortunately within the
36 function <code>foo</code> it is not known whether <code>rng</code> is a reference type or not,
37 hence many uses of <code>rng</code> require an extra reference, either explicitly
38 (<code>distr.sample(&amp;mut rng)</code>) or implicitly (<code>rng.gen()</code>); one may hope the
39 optimiser can remove redundant references later.</p>
40 <p>Example:</p>
41
42 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::Rng;
43
44 <span class="kw">fn </span>foo&lt;R: Rng + <span class="question-mark">?</span>Sized&gt;(rng: <span class="kw-2">&amp;mut </span>R) -&gt; f32 {
45 rng.gen()
46 }
47 </code></pre></div>
48 </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.gen" class="method"><a class="src rightside" href="https://rust-random.github.io/rand/src/rand/rng.rs.html#93-94">Source</a><h4 class="code-header">fn <a href="#method.gen" class="fn">gen</a>&lt;T&gt;(&amp;mut self) -&gt; T<div class="where">where
49 <a class="struct" href="../distributions/struct.Standard.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::Standard">Standard</a>: <a class="trait" href="trait.Distribution.html" title="trait bdk_chain::bitcoin::secp256k1::rand::prelude::Distribution">Distribution</a>&lt;T&gt;,</div></h4></section></summary><div class="docblock"><p>Return a random value supporting the <a href="../distributions/struct.Standard.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::Standard"><code>Standard</code></a> distribution.</p>
50 <h5 id="example"><a class="doc-anchor" href="#example">§</a>Example</h5>
51 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::{thread_rng, Rng};
52
53 <span class="kw">let </span><span class="kw-2">mut </span>rng = thread_rng();
54 <span class="kw">let </span>x: u32 = rng.gen();
55 <span class="macro">println!</span>(<span class="string">"{}"</span>, x);
56 <span class="macro">println!</span>(<span class="string">"{:?}"</span>, rng.gen::&lt;(f64, bool)&gt;());</code></pre></div>
57 <h5 id="arrays-and-tuples"><a class="doc-anchor" href="#arrays-and-tuples">§</a>Arrays and tuples</h5>
58 <p>The <code>rng.gen()</code> method is able to generate arrays (up to 32 elements)
59 and tuples (up to 12 elements), so long as all element types can be
60 generated.
61 When using <code>rustc</code> ≥ 1.51, enable the <code>min_const_gen</code> feature to support
62 arrays larger than 32 elements.</p>
63 <p>For arrays of integers, especially for those with small element types
64 (&lt; 64 bit), it will likely be faster to instead use <a href="../trait.Rng.html#method.fill" title="method bdk_chain::bitcoin::secp256k1::rand::Rng::fill"><code>Rng::fill</code></a>.</p>
65
66 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::{thread_rng, Rng};
67
68 <span class="kw">let </span><span class="kw-2">mut </span>rng = thread_rng();
69 <span class="kw">let </span>tuple: (u8, i32, char) = rng.gen(); <span class="comment">// arbitrary tuple support
70
71 </span><span class="kw">let </span>arr1: [f32; <span class="number">32</span>] = rng.gen(); <span class="comment">// array construction
72 </span><span class="kw">let </span><span class="kw-2">mut </span>arr2 = [<span class="number">0u8</span>; <span class="number">128</span>];
73 rng.fill(<span class="kw-2">&amp;mut </span>arr2); <span class="comment">// array fill</span></code></pre></div>
74 </div></details><details class="toggle method-toggle" open><summary><section id="method.gen_range" class="method"><a class="src rightside" href="https://rust-random.github.io/rand/src/rand/rng.rs.html#129-132">Source</a><h4 class="code-header">fn <a href="#method.gen_range" class="fn">gen_range</a>&lt;T, R&gt;(&amp;mut self, range: R) -&gt; T<div class="where">where
75 T: <a class="trait" href="../distributions/uniform/trait.SampleUniform.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::SampleUniform">SampleUniform</a>,
76 R: <a class="trait" href="../distributions/uniform/trait.SampleRange.html" title="trait bdk_chain::bitcoin::secp256k1::rand::distributions::uniform::SampleRange">SampleRange</a>&lt;T&gt;,</div></h4></section></summary><div class="docblock"><p>Generate a random value in the given range.</p>
77 <p>This function is optimised for the case that only a single sample is
78 made from the given range. See also the <a href="../distributions/struct.Uniform.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::Uniform"><code>Uniform</code></a> distribution
79 type which may be faster if sampling from the same range repeatedly.</p>
80 <p>Only <code>gen_range(low..high)</code> and <code>gen_range(low..=high)</code> are supported.</p>
81 <h5 id="panics"><a class="doc-anchor" href="#panics">§</a>Panics</h5>
82 <p>Panics if the range is empty.</p>
83 <h5 id="example-1"><a class="doc-anchor" href="#example-1">§</a>Example</h5>
84 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::{thread_rng, Rng};
85
86 <span class="kw">let </span><span class="kw-2">mut </span>rng = thread_rng();
87
88 <span class="comment">// Exclusive range
89 </span><span class="kw">let </span>n: u32 = rng.gen_range(<span class="number">0</span>..<span class="number">10</span>);
90 <span class="macro">println!</span>(<span class="string">"{}"</span>, n);
91 <span class="kw">let </span>m: f64 = rng.gen_range(-<span class="number">40.0</span>..<span class="number">1.3e5</span>);
92 <span class="macro">println!</span>(<span class="string">"{}"</span>, m);
93
94 <span class="comment">// Inclusive range
95 </span><span class="kw">let </span>n: u32 = rng.gen_range(<span class="number">0</span>..=<span class="number">10</span>);
96 <span class="macro">println!</span>(<span class="string">"{}"</span>, n);</code></pre></div>
97 </div></details><details class="toggle method-toggle" open><summary><section id="method.sample" class="method"><a class="src rightside" href="https://rust-random.github.io/rand/src/rand/rng.rs.html#152">Source</a><h4 class="code-header">fn <a href="#method.sample" class="fn">sample</a>&lt;T, D&gt;(&amp;mut self, distr: D) -&gt; T<div class="where">where
98 D: <a class="trait" href="trait.Distribution.html" title="trait bdk_chain::bitcoin::secp256k1::rand::prelude::Distribution">Distribution</a>&lt;T&gt;,</div></h4></section></summary><div class="docblock"><p>Sample a new value, using the given distribution.</p>
99 <h6 id="example-2"><a class="doc-anchor" href="#example-2">§</a>Example</h6>
100 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::{thread_rng, Rng};
101 <span class="kw">use </span>rand::distributions::Uniform;
102
103 <span class="kw">let </span><span class="kw-2">mut </span>rng = thread_rng();
104 <span class="kw">let </span>x = rng.sample(Uniform::new(<span class="number">10u32</span>, <span class="number">15</span>));
105 <span class="comment">// Type annotation requires two types, the type and distribution; the
106 // distribution can be inferred.
107 </span><span class="kw">let </span>y = rng.sample::&lt;u16, <span class="kw">_</span>&gt;(Uniform::new(<span class="number">10</span>, <span class="number">15</span>));</code></pre></div>
108 </div></details><details class="toggle method-toggle" open><summary><section id="method.sample_iter" class="method"><a class="src rightside" href="https://rust-random.github.io/rand/src/rand/rng.rs.html#192-195">Source</a><h4 class="code-header">fn <a href="#method.sample_iter" class="fn">sample_iter</a>&lt;T, D&gt;(self, distr: D) -&gt; <a class="struct" href="../distributions/struct.DistIter.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::DistIter">DistIter</a>&lt;D, Self, T&gt; <a href="#" class="tooltip" data-notable-ty="DistIter&lt;D, Self, T&gt;">ⓘ</a><div class="where">where
109 D: <a class="trait" href="trait.Distribution.html" title="trait bdk_chain::bitcoin::secp256k1::rand::prelude::Distribution">Distribution</a>&lt;T&gt;,
110 Self: <a class="trait" href="https://doc.rust-lang.org/nightly/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>
111 <p>Note that this function takes its arguments by value. This works since
112 <code>(&amp;mut R): Rng where R: Rng</code> and
113 <code>(&amp;D): Distribution where D: Distribution</code>,
114 however borrowing is not automatic hence <code>rng.sample_iter(...)</code> may
115 need to be replaced with <code>(&amp;mut rng).sample_iter(...)</code>.</p>
116 <h5 id="example-3"><a class="doc-anchor" href="#example-3">§</a>Example</h5>
117 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::{thread_rng, Rng};
118 <span class="kw">use </span>rand::distributions::{Alphanumeric, Uniform, Standard};
119
120 <span class="kw">let </span><span class="kw-2">mut </span>rng = thread_rng();
121
122 <span class="comment">// Vec of 16 x f32:
123 </span><span class="kw">let </span>v: Vec&lt;f32&gt; = (<span class="kw-2">&amp;mut </span>rng).sample_iter(Standard).take(<span class="number">16</span>).collect();
124
125 <span class="comment">// String:
126 </span><span class="kw">let </span>s: String = (<span class="kw-2">&amp;mut </span>rng).sample_iter(Alphanumeric)
127 .take(<span class="number">7</span>)
128 .map(char::from)
129 .collect();
130
131 <span class="comment">// Combined values
132 </span><span class="macro">println!</span>(<span class="string">"{:?}"</span>, (<span class="kw-2">&amp;mut </span>rng).sample_iter(Standard).take(<span class="number">5</span>)
133 .collect::&lt;Vec&lt;(f64, bool)&gt;&gt;());
134
135 <span class="comment">// Dice-rolling:
136 </span><span class="kw">let </span>die_range = Uniform::new_inclusive(<span class="number">1</span>, <span class="number">6</span>);
137 <span class="kw">let </span><span class="kw-2">mut </span>roll_die = (<span class="kw-2">&amp;mut </span>rng).sample_iter(die_range);
138 <span class="kw">while </span>roll_die.next().unwrap() != <span class="number">6 </span>{
139 <span class="macro">println!</span>(<span class="string">"Not a 6; rolling again!"</span>);
140 }</code></pre></div>
141 </div></details><details class="toggle method-toggle" open><summary><section id="method.fill" class="method"><a class="src rightside" href="https://rust-random.github.io/rand/src/rand/rng.rs.html#218">Source</a><h4 class="code-header">fn <a href="#method.fill" class="fn">fill</a>&lt;T&gt;(&amp;mut self, dest: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut T</a>)<div class="where">where
142 T: <a class="trait" href="../trait.Fill.html" title="trait bdk_chain::bitcoin::secp256k1::rand::Fill">Fill</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h4></section></summary><div class="docblock"><p>Fill any type implementing <a href="../trait.Fill.html" title="trait bdk_chain::bitcoin::secp256k1::rand::Fill"><code>Fill</code></a> with random data</p>
143 <p>The distribution is expected to be uniform with portable results, but
144 this cannot be guaranteed for third-party implementations.</p>
145 <p>This is identical to <a href="../trait.Rng.html#method.try_fill" title="method bdk_chain::bitcoin::secp256k1::rand::Rng::try_fill"><code>try_fill</code></a> except that it panics on error.</p>
146 <h5 id="example-4"><a class="doc-anchor" href="#example-4">§</a>Example</h5>
147 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::{thread_rng, Rng};
148
149 <span class="kw">let </span><span class="kw-2">mut </span>arr = [<span class="number">0i8</span>; <span class="number">20</span>];
150 thread_rng().fill(<span class="kw-2">&amp;mut </span>arr[..]);</code></pre></div>
151 </div></details><details class="toggle method-toggle" open><summary><section id="method.try_fill" class="method"><a class="src rightside" href="https://rust-random.github.io/rand/src/rand/rng.rs.html#246">Source</a><h4 class="code-header">fn <a href="#method.try_fill" class="fn">try_fill</a>&lt;T&gt;(&amp;mut self, dest: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut T</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>, <a class="struct" href="../struct.Error.html" title="struct bdk_chain::bitcoin::secp256k1::rand::Error">Error</a>&gt;<div class="where">where
152 T: <a class="trait" href="../trait.Fill.html" title="trait bdk_chain::bitcoin::secp256k1::rand::Fill">Fill</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h4></section></summary><div class="docblock"><p>Fill any type implementing <a href="../trait.Rng.html#method.fill" title="method bdk_chain::bitcoin::secp256k1::rand::Rng::fill"><code>Fill</code></a> with random data</p>
153 <p>The distribution is expected to be uniform with portable results, but
154 this cannot be guaranteed for third-party implementations.</p>
155 <p>This is identical to <a href="../trait.Rng.html#method.fill" title="method bdk_chain::bitcoin::secp256k1::rand::Rng::fill"><code>fill</code></a> except that it forwards errors.</p>
156 <h5 id="example-5"><a class="doc-anchor" href="#example-5">§</a>Example</h5>
157 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::{thread_rng, Rng};
158
159 <span class="kw">let </span><span class="kw-2">mut </span>arr = [<span class="number">0u64</span>; <span class="number">4</span>];
160 thread_rng().try_fill(<span class="kw-2">&amp;mut </span>arr[..])<span class="question-mark">?</span>;
161 </code></pre></div>
162 </div></details><details class="toggle method-toggle" open><summary><section id="method.gen_bool" class="method"><a class="src rightside" href="https://rust-random.github.io/rand/src/rand/rng.rs.html#270">Source</a><h4 class="code-header">fn <a href="#method.gen_bool" class="fn">gen_bool</a>(&amp;mut self, p: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f64.html">f64</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/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>
163 <p>See also the <a href="../distributions/struct.Bernoulli.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::Bernoulli"><code>Bernoulli</code></a> distribution, which may be faster if
164 sampling from the same probability repeatedly.</p>
165 <h5 id="example-6"><a class="doc-anchor" href="#example-6">§</a>Example</h5>
166 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::{thread_rng, Rng};
167
168 <span class="kw">let </span><span class="kw-2">mut </span>rng = thread_rng();
169 <span class="macro">println!</span>(<span class="string">"{}"</span>, rng.gen_bool(<span class="number">1.0 </span>/ <span class="number">3.0</span>));</code></pre></div>
170 <h5 id="panics-1"><a class="doc-anchor" href="#panics-1">§</a>Panics</h5>
171 <p>If <code>p &lt; 0</code> or <code>p &gt; 1</code>.</p>
172 </div></details><details class="toggle method-toggle" open><summary><section id="method.gen_ratio" class="method"><a class="src rightside" href="https://rust-random.github.io/rand/src/rand/rng.rs.html#299">Source</a><h4 class="code-header">fn <a href="#method.gen_ratio" class="fn">gen_ratio</a>(&amp;mut self, numerator: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>, denominator: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/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
173 true. I.e. <code>gen_ratio(2, 3)</code> has chance of 2 in 3, or about 67%, of
174 returning true. If <code>numerator == denominator</code>, then the returned value
175 is guaranteed to be <code>true</code>. If <code>numerator == 0</code>, then the returned
176 value is guaranteed to be <code>false</code>.</p>
177 <p>See also the <a href="../distributions/struct.Bernoulli.html" title="struct bdk_chain::bitcoin::secp256k1::rand::distributions::Bernoulli"><code>Bernoulli</code></a> distribution, which may be faster if
178 sampling from the same <code>numerator</code> and <code>denominator</code> repeatedly.</p>
179 <h5 id="panics-2"><a class="doc-anchor" href="#panics-2">§</a>Panics</h5>
180 <p>If <code>denominator == 0</code> or <code>numerator &gt; denominator</code>.</p>
181 <h5 id="example-7"><a class="doc-anchor" href="#example-7">§</a>Example</h5>
182 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::{thread_rng, Rng};
183
184 <span class="kw">let </span><span class="kw-2">mut </span>rng = thread_rng();
185 <span class="macro">println!</span>(<span class="string">"{}"</span>, rng.gen_ratio(<span class="number">2</span>, <span class="number">3</span>));</code></pre></div>
186 </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/nightly/reference/items/traits.html#object-safety">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="https://rust-random.github.io/rand/src/rand/rng.rs.html#305">Source</a><a href="#impl-Rng-for-R" class="anchor">§</a><h3 class="code-header">impl&lt;R&gt; <a class="trait" href="../trait.Rng.html" title="trait bdk_chain::bitcoin::secp256k1::rand::Rng">Rng</a> for R<div class="where">where
187 R: <a class="trait" href="../trait.RngCore.html" title="trait bdk_chain::bitcoin::secp256k1::rand::RngCore">RngCore</a> + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></div><script src="../../../../../trait.impl/rand/rng/trait.Rng.js" async></script><script type="text/json" id="notable-traits-data">{"DistIter<D, Self, T>":"<h3>Notable traits for <code><a class=\"struct\" href=\"../distributions/struct.DistIter.html\" title=\"struct bdk_chain::bitcoin::secp256k1::rand::distributions::DistIter\">DistIter</a>&lt;D, R, T&gt;</code></h3><pre><code><div class=\"where\">impl&lt;D, R, T&gt; <a class=\"trait\" href=\"https://doc.rust-lang.org/nightly/core/iter/traits/iterator/trait.Iterator.html\" title=\"trait core::iter::traits::iterator::Iterator\">Iterator</a> for <a class=\"struct\" href=\"../distributions/struct.DistIter.html\" title=\"struct bdk_chain::bitcoin::secp256k1::rand::distributions::DistIter\">DistIter</a>&lt;D, R, T&gt;<div class=\"where\">where\n D: <a class=\"trait\" href=\"trait.Distribution.html\" title=\"trait bdk_chain::bitcoin::secp256k1::rand::prelude::Distribution\">Distribution</a>&lt;T&gt;,\n R: <a class=\"trait\" href=\"../trait.Rng.html\" title=\"trait bdk_chain::bitcoin::secp256k1::rand::Rng\">Rng</a>,</div></div><div class=\"where\"> type <a href=\"https://doc.rust-lang.org/nightly/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item\" class=\"associatedtype\">Item</a> = T;</div>"}</script></section></div></main></body></html>