owoify/target/doc/rand/distributions/index.html

103 lines
19 KiB
HTML
Raw Normal View History

2019-08-01 13:54:11 +00:00
<!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="API documentation for the Rust `distributions` mod in crate `rand`."><meta name="keywords" content="rust, rustlang, rust-lang, distributions"><title>rand::distributions - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="shortcut icon" href="https://www.rust-lang.org/favicon.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><a 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><p class='location'>Module distributions</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#reexports">Re-exports</a></li><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#traits">Traits</a></li></ul></div><p class='location'><a href='../index.html'>rand</a></p><script>window.sidebarCurrent = {name: 'distributions', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>&#x2212;</span>]</a></span><a class='srclink' href='../../src/rand/distributions/mod.rs.html#10-381' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../index.html'>rand</a>::<wbr><a class="mod" href=''>distributions</a></span></h1><div class='docblock'><p>Generating random samples from probability distributions</p>
<p>This module is the home of the [<code>Distribution</code>] trait and several of its
implementations. It is the workhorse behind some of the convenient
functionality of the [<code>Rng</code>] trait, e.g. [<code>Rng::gen</code>], [<code>Rng::gen_range</code>] and
of course [<code>Rng::sample</code>].</p>
<p>Abstractly, a <a href="https://en.wikipedia.org/wiki/Probability_distribution">probability distribution</a> describes the probability of
occurance of each value in its sample space.</p>
<p>More concretely, an implementation of <code>Distribution&lt;T&gt;</code> for type <code>X</code> is an
algorithm for choosing values from the sample space (a subset of <code>T</code>)
according to the distribution <code>X</code> represents, using an external source of
randomness (an RNG supplied to the <code>sample</code> function).</p>
<p>A type <code>X</code> may implement <code>Distribution&lt;T&gt;</code> for multiple types <code>T</code>.
Any type implementing [<code>Distribution</code>] is stateless (i.e. immutable),
but it may have internal parameters set at construction time (for example,
<a href="distributions::Uniform"><code>Uniform</code></a> allows specification of its sample space as a range within <code>T</code>).</p>
<h1 id="the-standard-distribution" class="section-header"><a href="#the-standard-distribution">The <code>Standard</code> distribution</a></h1>
<p>The <a href="distributions::Standard"><code>Standard</code></a> distribution is important to mention. This is the
distribution used by [<code>Rng::gen()</code>] and represents the &quot;default&quot; way to
produce a random value for many different types, including most primitive
types, tuples, arrays, and a few derived types. See the documentation of
<a href="distributions::Standard"><code>Standard</code></a> for more details.</p>
<p>Implementing <code>Distribution&lt;T&gt;</code> for <a href="distributions::Standard"><code>Standard</code></a> for user types <code>T</code> makes it
possible to generate type <code>T</code> with [<code>Rng::gen()</code>], and by extension also
with the [<code>random()</code>] function.</p>
<h2 id="random-characters" class="section-header"><a href="#random-characters">Random characters</a></h2>
<p><a href="distributions::Alphanumeric"><code>Alphanumeric</code></a> is a simple distribution to sample random letters and
numbers of the <code>char</code> type; in contrast <a href="distributions::Standard"><code>Standard</code></a> may sample any valid
<code>char</code>.</p>
<h1 id="uniform-numeric-ranges" class="section-header"><a href="#uniform-numeric-ranges">Uniform numeric ranges</a></h1>
<p>The <a href="distributions::Uniform"><code>Uniform</code></a> distribution is more flexible than <a href="distributions::Standard"><code>Standard</code></a>, but also
more specialised: it supports fewer target types, but allows the sample
space to be specified as an arbitrary range within its target type <code>T</code>.
Both <a href="distributions::Standard"><code>Standard</code></a> and <a href="distributions::Uniform"><code>Uniform</code></a> are in some sense uniform distributions.</p>
<p>Values may be sampled from this distribution using [<code>Rng::gen_range</code>] or
by creating a distribution object with <a href="distributions::Uniform::new"><code>Uniform::new</code></a>,
<a href="distributions::Uniform::new_inclusive"><code>Uniform::new_inclusive</code></a> or <code>From&lt;Range&gt;</code>. When the range limits are not
known at compile time it is typically faster to reuse an existing
distribution object than to call [<code>Rng::gen_range</code>].</p>
<p>User types <code>T</code> may also implement <code>Distribution&lt;T&gt;</code> for <a href="distributions::Uniform"><code>Uniform</code></a>,
although this is less straightforward than for <a href="distributions::Standard"><code>Standard</code></a> (see the
documentation in the <a href="distributions::Uniform"><code>uniform</code></a> module. Doing so enables generation of
values of type <code>T</code> with [<code>Rng::gen_range</code>].</p>
<h2 id="open-and-half-open-ranges" class="section-header"><a href="#open-and-half-open-ranges">Open and half-open ranges</a></h2>
<p>There are surprisingly many ways to uniformly generate random floats. A
range between 0 and 1 is standard, but the exact bounds (open vs closed)
and accuracy differ. In addition to the <a href="distributions::Standard"><code>Standard</code></a> distribution Rand offers
<a href="distributions::Open01"><code>Open01</code></a> and <a href="distributions::OpenClosed01"><code>OpenClosed01</code></a>. See &quot;Floating point implementation&quot; section of
<a href="distributions::Standard"><code>Standard</code></a> documentation for more details.</p>
<h1 id="non-uniform-sampling" class="section-header"><a href="#non-uniform-sampling">Non-uniform sampling</a></h1>
<p>Sampling a simple true/false outcome with a given probability has a name:
the <a href="distributions::Bernoulli"><code>Bernoulli</code></a> distribution (this is used by [<code>Rng::gen_bool</code>]).</p>
<p>For weighted sampling from a sequence of discrete values, use the
<a href="distributions::weighted"><code>weighted</code></a> module.</p>
<p>This crate no longer includes other non-uniform distributions; instead
it is recommended that you use either <a href="https://crates.io/crates/rand_distr"><code>rand_distr</code></a> or <a href="https://crates.io/crates/statrs"><code>statrs</code></a>.</p>
</div><h2 id='reexports' class='section-header'><a href="#reexports">Re-exports</a></h2>
<table><tr><td><code>pub use self::weighted::<a class="struct" href="../../rand/distributions/weighted/struct.WeightedIndex.html" title="struct rand::distributions::weighted::WeightedIndex">WeightedIndex</a>;</code></td></tr><tr><td><code>pub use self::weighted::<a class="enum" href="../../rand/distributions/weighted/enum.WeightedError.html" title="enum rand::distributions::weighted::WeightedError">WeightedError</a>;</code></td></tr></table><h2 id='modules' class='section-header'><a href="#modules">Modules</a></h2>
<table><tr class='module-item'><td><a class="mod" href="uniform/index.html" title='rand::distributions::uniform mod'>uniform</a></td><td class='docblock-short'><p>A distribution uniformly sampling numbers within a given range.</p>
</td></tr><tr class='module-item'><td><a class="mod" href="weighted/index.html" title='rand::distributions::weighted mod'>weighted</a></td><td class='docblock-short'><p>Weighted index sampling</p>
</td></tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table><tr class='module-item'><td><a class="struct" href="struct.Alphanumeric.html" title='rand::distributions::Alphanumeric struct'>Alphanumeric</a></td><td class='docblock-short'><p>Sample a <code>char</code>, uniformly distributed over ASCII letters and numbers:
a-z, A-Z and 0-9.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Bernoulli.html" title='rand::distributions::Bernoulli struct'>Bernoulli</a></td><td class='docblock-short'><p>The Bernoulli distribution.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Beta.html" title='rand::distributions::Beta struct'>Beta</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The Beta distribution with shape parameters <code>alpha</code> and <code>beta</code>.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Binomial.html" title='rand::distributions::Binomial struct'>Binomial</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The binomial distribution <code>Binomial(n, p)</code>.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Cauchy.html" title='rand::distributions::Cauchy struct'>Cauchy</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The Cauchy distribution <code>Cauchy(median, scale)</code>.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.ChiSquared.html" title='rand::distributions::ChiSquared struct'>ChiSquared</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The chi-squared distribution <code>χ²(k)</code>, where <code>k</code> is the degrees of
freedom.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Dirichlet.html" title='rand::distributions::Dirichlet struct'>Dirichlet</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The dirichelet distribution <code>Dirichlet(alpha)</code>.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.DistIter.html" title='rand::distributions::DistIter struct'>DistIter</a></td><td class='docblock-short'><p>An iterator that generates random values of <code>T</code> with distribution <code>D</code>,
using <code>R</code> as the source of randomness.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Exp.html" title='rand::distributions::Exp struct'>Exp</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The exponential distribution <code>Exp(lambda)</code>.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Exp1.html" title='rand::distributions::Exp1 struct'>Exp1</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>Samples floating-point numbers according to the exponential distribution,
with rate parameter <code>λ = 1</code>. This is equivalent to <code>Exp::new(1.0)</code> or
sampling with <code>-rng.gen::&lt;f64&gt;().ln()</code>, but faster.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.FisherF.html" title='rand::distributions::FisherF struct'>FisherF</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The Fisher F distribution <code>F(m, n)</code>.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Gamma.html" title='rand::distributions::Gamma struct'>Gamma</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The Gamma distribution <code>Gamma(shape, scale)</code> distribution.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.LogNormal.html" title='rand::distributions::LogNormal struct'>LogNormal</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The log-normal distribution <code>ln N(mean, std_dev**2)</code>.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Normal.html" title='rand::distributions::Normal struct'>Normal</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The normal distribution <code>N(mean, std_dev**2)</code>.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Open01.html" title='rand::distributions::Open01 struct'>Open01</a></td><td class='docblock-short'><p>A distribution to sample floating point numbers uniformly in the open
interval <code>(0, 1)</code>, i.e. not including either endpoint.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.OpenClosed01.html" title='rand::distributions::OpenClosed01 struct'>OpenClosed01</a></td><td class='docblock-short'><p>A distribution to sample floating point numbers uniformly in the half-open
interval <code>(0, 1]</code>, i.e. including 1 but not 0.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Pareto.html" title='rand::distributions::Pareto struct'>Pareto</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>Samples floating-point numbers according to the Pareto distribution</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Poisson.html" title='rand::distributions::Poisson struct'>Poisson</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The Poisson distribution <code>Poisson(lambda)</code>.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Standard.html" title='rand::distributions::Standard struct'>Standard</a></td><td class='docblock-short'><p>A generic random value distribution, implemented for many primitive types.
Usually generates values with a numerically uniform distribution, and with a
range appropriate to the type.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.StandardNormal.html" title='rand::distributions::StandardNormal struct'>StandardNormal</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>Samples floating-point numbers according to the normal distribution
<code>N(0, 1)</code> (a.k.a. a standard normal, or Gaussian). This is equivalent to
<code>Normal::new(0.0, 1.0)</code> but faster.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.StudentT.html" title='rand::distributions::StudentT struct'>StudentT</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The Student t distribution, <code>t(nu)</code>, where <code>nu</code> is the degrees of
freedom.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Triangular.html" title='rand::distributions::Triangular struct'>Triangular</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>The triangular distribution.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Uniform.html" title='rand::distributions::Uniform struct'>Uniform</a></td><td class='docblock-short'><p>Sample values uniformly between two bounds.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.UnitCircle.html" title='rand::distributions::UnitCircle struct'>UnitCircle</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>Samples uniformly from the edge of the unit circle in two dimensions.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.UnitSphereSurface.html" title='rand::distributions::UnitSphereSurface struct'>UnitSphereSurface</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>Samples uniformly from the surface of the unit sphere in three dimensions.</p>
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Weibull.html" title='rand::distributions::Weibull struct'>Weibull</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>Samples floating-point numbers according to the Weibull distribution</p>
</td></tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
<table><tr class='module-item'><td><a class="enum" href="enum.BernoulliError.html" title='rand::distributions::BernoulliError enum'>BernoulliError</a></td><td class='docblock-short'><p>Error type returned from <code>Bernoulli::new</code>.</p>
</td></tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
<table><tr class='module-item'><td><a class="trait" href="trait.Distribution.html" title='rand::distributions::Distribution trait'>Distribution</a></td><td class='docblock-short'><p>Types (distributions) that can be used to create a random instance of <code>T</code>.</p>
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd></kbd></dt><dd>Move up in search results</dd><dt><kbd></kbd></dt><dd>Move down in search results</dd><dt><kbd></kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "rand";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>