mirror of
https://gitlab.redox-os.org/CoffeeCode/redox-ssh.git
synced 2025-12-28 16:42:19 +01:00
313 lines
No EOL
32 KiB
HTML
313 lines
No EOL
32 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="API documentation for the Rust `Rng` trait in crate `rand`.">
|
||
<meta name="keywords" content="rust, rustlang, rust-lang, Rng">
|
||
|
||
<title>rand::Rng - Rust</title>
|
||
|
||
<link rel="stylesheet" type="text/css" href="../normalize.css">
|
||
<link rel="stylesheet" type="text/css" href="../rustdoc.css">
|
||
<link rel="stylesheet" type="text/css" href="../main.css">
|
||
|
||
|
||
<link rel="shortcut icon" href="https://www.rust-lang.org/favicon.ico">
|
||
|
||
</head>
|
||
<body class="rustdoc trait">
|
||
<!--[if lte IE 8]>
|
||
<div class="warning">
|
||
This old browser is unsupported and will most likely display funky
|
||
things.
|
||
</div>
|
||
<![endif]-->
|
||
|
||
|
||
|
||
<nav class="sidebar">
|
||
<a href='../rand/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk.png' alt='logo' width='100'></a>
|
||
<p class='location'>Trait Rng</p><div class="block items"><ul><li><a href="#required-methods">Required Methods</a></li><li><a href="#provided-methods">Provided Methods</a></li><li><a href="#implementors">Implementors</a></li></ul></div><p class='location'><a href='index.html'>rand</a></p><script>window.sidebarCurrent = {name: 'Rng', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></script>
|
||
</nav>
|
||
|
||
<nav class="sub">
|
||
<form class="search-form js-only">
|
||
<div class="search-container">
|
||
<input class="search-input" name="search"
|
||
autocomplete="off"
|
||
placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
|
||
type="search">
|
||
</div>
|
||
</form>
|
||
</nav>
|
||
|
||
<section id='main' class="content">
|
||
<h1 class='fqn'><span class='in-band'>Trait <a href='index.html'>rand</a>::<wbr><a class="trait" href=''>Rng</a></span><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'>−</span>]
|
||
</a>
|
||
</span><a class='srclink' href='../src/rand/lib.rs.html#287-555' title='goto source code'>[src]</a></span></h1>
|
||
<pre class='rust trait'>pub trait Rng {
|
||
fn <a href='#tymethod.next_u32' class='fnname'>next_u32</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>;
|
||
|
||
fn <a href='#method.next_u64' class='fnname'>next_u64</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u64.html">u64</a> { ... }
|
||
fn <a href='#method.next_f32' class='fnname'>next_f32</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f32.html">f32</a> { ... }
|
||
fn <a href='#method.next_f64' class='fnname'>next_f64</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f64.html">f64</a> { ... }
|
||
fn <a href='#method.fill_bytes' class='fnname'>fill_bytes</a>(&mut self, dest: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>) { ... }
|
||
fn <a href='#method.gen' class='fnname'>gen</a><T: <a class="trait" href="../rand/trait.Rand.html" title="trait rand::Rand">Rand</a>>(&mut self) -> T<br> <span class="where">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
|
||
{ ... }
|
||
fn <a href='#method.gen_iter' class='fnname'>gen_iter</a><'a, T: <a class="trait" href="../rand/trait.Rand.html" title="trait rand::Rand">Rand</a>>(&'a mut self) -> <a class="struct" href="../rand/struct.Generator.html" title="struct rand::Generator">Generator</a><'a, T, Self><br> <span class="where">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
|
||
{ ... }
|
||
fn <a href='#method.gen_range' class='fnname'>gen_range</a><T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a> + <a class="trait" href="../rand/distributions/range/trait.SampleRange.html" title="trait rand::distributions::range::SampleRange">SampleRange</a>>(&mut self, low: T, high: T) -> T<br> <span class="where">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
|
||
{ ... }
|
||
fn <a href='#method.gen_weighted_bool' class='fnname'>gen_weighted_bool</a>(&mut self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a><br> <span class="where">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
|
||
{ ... }
|
||
fn <a href='#method.gen_ascii_chars' class='fnname'>gen_ascii_chars</a><'a>(&'a mut self) -> <a class="struct" href="../rand/struct.AsciiGenerator.html" title="struct rand::AsciiGenerator">AsciiGenerator</a><'a, Self><br> <span class="where">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
|
||
{ ... }
|
||
fn <a href='#method.choose' class='fnname'>choose</a><'a, T>(&mut self, values: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&'a [</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&'a T><br> <span class="where">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
|
||
{ ... }
|
||
fn <a href='#method.choose_mut' class='fnname'>choose_mut</a><'a, T>(&mut self, values: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&'a mut [</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&'a mut T><br> <span class="where">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
|
||
{ ... }
|
||
fn <a href='#method.shuffle' class='fnname'>shuffle</a><T>(&mut self, values: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>)<br> <span class="where">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
|
||
{ ... }
|
||
}</pre><div class='docblock'><p>A random number generator.</p>
|
||
</div>
|
||
<h2 id='required-methods'>Required Methods</h2>
|
||
<div class='methods'>
|
||
<h3 id='tymethod.next_u32' class='method'><span id='next_u32.v' class='invisible'><code>fn <a href='#tymethod.next_u32' class='fnname'>next_u32</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code></span></h3><div class='docblock'><p>Return the next random u32.</p>
|
||
|
||
<p>This rarely needs to be called directly, prefer <code>r.gen()</code> to
|
||
<code>r.next_u32()</code>.</p>
|
||
</div></div>
|
||
<h2 id='provided-methods'>Provided Methods</h2>
|
||
<div class='methods'>
|
||
<h3 id='method.next_u64' class='method'><span id='next_u64.v' class='invisible'><code>fn <a href='#method.next_u64' class='fnname'>next_u64</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u64.html">u64</a></code></span></h3><div class='docblock'><p>Return the next random u64.</p>
|
||
|
||
<p>By default this is implemented in terms of <code>next_u32</code>. An
|
||
implementation of this trait must provide at least one of
|
||
these two methods. Similarly to <code>next_u32</code>, this rarely needs
|
||
to be called directly, prefer <code>r.gen()</code> to <code>r.next_u64()</code>.</p>
|
||
</div><h3 id='method.next_f32' class='method'><span id='next_f32.v' class='invisible'><code>fn <a href='#method.next_f32' class='fnname'>next_f32</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f32.html">f32</a></code></span></h3><div class='docblock'><p>Return the next random f32 selected from the half-open
|
||
interval <code>[0, 1)</code>.</p>
|
||
|
||
<p>This uses a technique described by Saito and Matsumoto at
|
||
MCQMC'08. Given that the IEEE floating point numbers are
|
||
uniformly distributed over [1,2), we generate a number in
|
||
this range and then offset it onto the range [0,1). Our
|
||
choice of bits (masking v. shifting) is arbitrary and
|
||
should be immaterial for high quality generators. For low
|
||
quality generators (ex. LCG), prefer bitshifting due to
|
||
correlation between sequential low order bits.</p>
|
||
|
||
<p>See:
|
||
A PRNG specialized in double precision floating point numbers using
|
||
an affine transition
|
||
<a href="http://www.math.sci.hiroshima-u.ac.jp/%7Em-mat/MT/ARTICLES/dSFMT.pdf">http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/ARTICLES/dSFMT.pdf</a>
|
||
<a href="http://www.math.sci.hiroshima-u.ac.jp/%7Em-mat/MT/SFMT/dSFMT-slide-e.pdf">http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/dSFMT-slide-e.pdf</a></p>
|
||
|
||
<p>By default this is implemented in terms of <code>next_u32</code>, but a
|
||
random number generator which can generate numbers satisfying
|
||
the requirements directly can overload this for performance.
|
||
It is required that the return value lies in <code>[0, 1)</code>.</p>
|
||
|
||
<p>See <code>Closed01</code> for the closed interval <code>[0,1]</code>, and
|
||
<code>Open01</code> for the open interval <code>(0,1)</code>.</p>
|
||
</div><h3 id='method.next_f64' class='method'><span id='next_f64.v' class='invisible'><code>fn <a href='#method.next_f64' class='fnname'>next_f64</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f64.html">f64</a></code></span></h3><div class='docblock'><p>Return the next random f64 selected from the half-open
|
||
interval <code>[0, 1)</code>.</p>
|
||
|
||
<p>By default this is implemented in terms of <code>next_u64</code>, but a
|
||
random number generator which can generate numbers satisfying
|
||
the requirements directly can overload this for performance.
|
||
It is required that the return value lies in <code>[0, 1)</code>.</p>
|
||
|
||
<p>See <code>Closed01</code> for the closed interval <code>[0,1]</code>, and
|
||
<code>Open01</code> for the open interval <code>(0,1)</code>.</p>
|
||
</div><h3 id='method.fill_bytes' class='method'><span id='fill_bytes.v' class='invisible'><code>fn <a href='#method.fill_bytes' class='fnname'>fill_bytes</a>(&mut self, dest: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>)</code></span></h3><div class='docblock'><p>Fill <code>dest</code> with random data.</p>
|
||
|
||
<p>This has a default implementation in terms of <code>next_u64</code> and
|
||
<code>next_u32</code>, but should be overridden by implementations that
|
||
offer a more efficient solution than just calling those
|
||
methods repeatedly.</p>
|
||
|
||
<p>This method does <em>not</em> have a requirement to bear any fixed
|
||
relationship to the other methods, for example, it does <em>not</em>
|
||
have to result in the same output as progressively filling
|
||
<code>dest</code> with <code>self.gen::<u8>()</code>, and any such behaviour should
|
||
not be relied upon.</p>
|
||
|
||
<p>This method should guarantee that <code>dest</code> is entirely filled
|
||
with new data, and may panic if this is impossible
|
||
(e.g. reading past the end of a file that is being used as the
|
||
source of randomness).</p>
|
||
|
||
<h1 id='example' class='section-header'><a href='#example'>Example</a></h1>
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">rand</span>::{<span class="ident">thread_rng</span>, <span class="ident">Rng</span>};
|
||
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">v</span> <span class="op">=</span> [<span class="number">0u8</span>; <span class="number">13579</span>];
|
||
<span class="ident">thread_rng</span>().<span class="ident">fill_bytes</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">v</span>);
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="kw-2">&</span><span class="ident">v</span>[..]);</pre>
|
||
</div><h3 id='method.gen' class='method'><span id='gen.v' class='invisible'><code>fn <a href='#method.gen' class='fnname'>gen</a><T: <a class="trait" href="../rand/trait.Rand.html" title="trait rand::Rand">Rand</a>>(&mut self) -> T <span class="where fmt-newline">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, </span></code></span></h3><div class='docblock'><p>Return a random value of a <code>Rand</code> type.</p>
|
||
|
||
<h1 id='example-1' class='section-header'><a href='#example-1'>Example</a></h1>
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">rand</span>::{<span class="ident">thread_rng</span>, <span class="ident">Rng</span>};
|
||
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">thread_rng</span>();
|
||
<span class="kw">let</span> <span class="ident">x</span>: <span class="ident">u32</span> <span class="op">=</span> <span class="ident">rng</span>.<span class="ident">gen</span>();
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">x</span>);
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="ident">rng</span>.<span class="ident">gen</span>::<span class="op"><</span>(<span class="ident">f64</span>, <span class="ident">bool</span>)<span class="op">></span>());</pre>
|
||
</div><h3 id='method.gen_iter' class='method'><span id='gen_iter.v' class='invisible'><code>fn <a href='#method.gen_iter' class='fnname'>gen_iter</a><'a, T: <a class="trait" href="../rand/trait.Rand.html" title="trait rand::Rand">Rand</a>>(&'a mut self) -> <a class="struct" href="../rand/struct.Generator.html" title="struct rand::Generator">Generator</a><'a, T, Self> <span class="where fmt-newline">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, </span></code></span></h3><div class='docblock'><p>Return an iterator that will yield an infinite number of randomly
|
||
generated items.</p>
|
||
|
||
<h1 id='example-2' class='section-header'><a href='#example-2'>Example</a></h1>
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">rand</span>::{<span class="ident">thread_rng</span>, <span class="ident">Rng</span>};
|
||
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">thread_rng</span>();
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident">rng</span>.<span class="ident">gen_iter</span>::<span class="op"><</span><span class="ident">u32</span><span class="op">></span>().<span class="ident">take</span>(<span class="number">10</span>).<span class="ident">collect</span>::<span class="op"><</span><span class="ident">Vec</span><span class="op"><</span><span class="ident">u32</span><span class="op">>></span>();
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="ident">x</span>);
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="ident">rng</span>.<span class="ident">gen_iter</span>::<span class="op"><</span>(<span class="ident">f64</span>, <span class="ident">bool</span>)<span class="op">></span>().<span class="ident">take</span>(<span class="number">5</span>)
|
||
.<span class="ident">collect</span>::<span class="op"><</span><span class="ident">Vec</span><span class="op"><</span>(<span class="ident">f64</span>, <span class="ident">bool</span>)<span class="op">>></span>());</pre>
|
||
</div><h3 id='method.gen_range' class='method'><span id='gen_range.v' class='invisible'><code>fn <a href='#method.gen_range' class='fnname'>gen_range</a><T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a> + <a class="trait" href="../rand/distributions/range/trait.SampleRange.html" title="trait rand::distributions::range::SampleRange">SampleRange</a>>(&mut self, low: T, high: T) -> T <span class="where fmt-newline">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, </span></code></span></h3><div class='docblock'><p>Generate a random value in the range [<code>low</code>, <code>high</code>).</p>
|
||
|
||
<p>This is a convenience wrapper around
|
||
<code>distributions::Range</code>. If this function will be called
|
||
repeatedly with the same arguments, one should use <code>Range</code>, as
|
||
that will amortize the computations that allow for perfect
|
||
uniformity, as they only happen on initialization.</p>
|
||
|
||
<h1 id='panics' class='section-header'><a href='#panics'>Panics</a></h1>
|
||
<p>Panics if <code>low >= high</code>.</p>
|
||
|
||
<h1 id='example-3' class='section-header'><a href='#example-3'>Example</a></h1>
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">rand</span>::{<span class="ident">thread_rng</span>, <span class="ident">Rng</span>};
|
||
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">thread_rng</span>();
|
||
<span class="kw">let</span> <span class="ident">n</span>: <span class="ident">u32</span> <span class="op">=</span> <span class="ident">rng</span>.<span class="ident">gen_range</span>(<span class="number">0</span>, <span class="number">10</span>);
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">n</span>);
|
||
<span class="kw">let</span> <span class="ident">m</span>: <span class="ident">f64</span> <span class="op">=</span> <span class="ident">rng</span>.<span class="ident">gen_range</span>(<span class="op">-</span><span class="number">40.0f64</span>, <span class="number">1.3e5f64</span>);
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">m</span>);</pre>
|
||
</div><h3 id='method.gen_weighted_bool' class='method'><span id='gen_weighted_bool.v' class='invisible'><code>fn <a href='#method.gen_weighted_bool' class='fnname'>gen_weighted_bool</a>(&mut self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a>) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a> <span class="where fmt-newline">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, </span></code></span></h3><div class='docblock'><p>Return a bool with a 1 in n chance of true</p>
|
||
|
||
<h1 id='example-4' class='section-header'><a href='#example-4'>Example</a></h1>
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">rand</span>::{<span class="ident">thread_rng</span>, <span class="ident">Rng</span>};
|
||
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">thread_rng</span>();
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">rng</span>.<span class="ident">gen_weighted_bool</span>(<span class="number">3</span>));</pre>
|
||
</div><h3 id='method.gen_ascii_chars' class='method'><span id='gen_ascii_chars.v' class='invisible'><code>fn <a href='#method.gen_ascii_chars' class='fnname'>gen_ascii_chars</a><'a>(&'a mut self) -> <a class="struct" href="../rand/struct.AsciiGenerator.html" title="struct rand::AsciiGenerator">AsciiGenerator</a><'a, Self> <span class="where fmt-newline">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, </span></code></span></h3><div class='docblock'><p>Return an iterator of random characters from the set A-Z,a-z,0-9.</p>
|
||
|
||
<h1 id='example-5' class='section-header'><a href='#example-5'>Example</a></h1>
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">rand</span>::{<span class="ident">thread_rng</span>, <span class="ident">Rng</span>};
|
||
|
||
<span class="kw">let</span> <span class="ident">s</span>: <span class="ident">String</span> <span class="op">=</span> <span class="ident">thread_rng</span>().<span class="ident">gen_ascii_chars</span>().<span class="ident">take</span>(<span class="number">10</span>).<span class="ident">collect</span>();
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">s</span>);</pre>
|
||
</div><h3 id='method.choose' class='method'><span id='choose.v' class='invisible'><code>fn <a href='#method.choose' class='fnname'>choose</a><'a, T>(&mut self, values: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&'a [</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&'a T> <span class="where fmt-newline">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, </span></code></span></h3><div class='docblock'><p>Return a random element from <code>values</code>.</p>
|
||
|
||
<p>Return <code>None</code> if <code>values</code> is empty.</p>
|
||
|
||
<h1 id='example-6' class='section-header'><a href='#example-6'>Example</a></h1>
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">rand</span>::{<span class="ident">thread_rng</span>, <span class="ident">Rng</span>};
|
||
|
||
<span class="kw">let</span> <span class="ident">choices</span> <span class="op">=</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">8</span>, <span class="number">16</span>, <span class="number">32</span>];
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">thread_rng</span>();
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="ident">rng</span>.<span class="ident">choose</span>(<span class="kw-2">&</span><span class="ident">choices</span>));
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">rng</span>.<span class="ident">choose</span>(<span class="kw-2">&</span><span class="ident">choices</span>[..<span class="number">0</span>]), <span class="prelude-val">None</span>);</pre>
|
||
</div><h3 id='method.choose_mut' class='method'><span id='choose_mut.v' class='invisible'><code>fn <a href='#method.choose_mut' class='fnname'>choose_mut</a><'a, T>(&mut self, values: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&'a mut [</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&'a mut T> <span class="where fmt-newline">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, </span></code></span></h3><div class='docblock'><p>Return a mutable pointer to a random element from <code>values</code>.</p>
|
||
|
||
<p>Return <code>None</code> if <code>values</code> is empty.</p>
|
||
</div><h3 id='method.shuffle' class='method'><span id='shuffle.v' class='invisible'><code>fn <a href='#method.shuffle' class='fnname'>shuffle</a><T>(&mut self, values: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&mut [</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>) <span class="where fmt-newline">where<br> Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, </span></code></span></h3><div class='docblock'><p>Shuffle a mutable slice in place.</p>
|
||
|
||
<h1 id='example-7' class='section-header'><a href='#example-7'>Example</a></h1>
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">rand</span>::{<span class="ident">thread_rng</span>, <span class="ident">Rng</span>};
|
||
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">thread_rng</span>();
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">y</span> <span class="op">=</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];
|
||
<span class="ident">rng</span>.<span class="ident">shuffle</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">y</span>);
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="ident">y</span>);
|
||
<span class="ident">rng</span>.<span class="ident">shuffle</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">y</span>);
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="ident">y</span>);</pre>
|
||
</div></div>
|
||
<h2 id='implementors'>Implementors</h2>
|
||
<ul class='item-list' id='implementors-list'>
|
||
<li><code>impl Rng for <a class="struct" href="../rand/isaac/struct.IsaacRng.html" title="struct rand::isaac::IsaacRng">IsaacRng</a></code></li>
|
||
<li><code>impl Rng for <a class="struct" href="../rand/isaac/struct.Isaac64Rng.html" title="struct rand::isaac::Isaac64Rng">Isaac64Rng</a></code></li>
|
||
<li><code>impl Rng for <a class="struct" href="../rand/chacha/struct.ChaChaRng.html" title="struct rand::chacha::ChaChaRng">ChaChaRng</a></code></li>
|
||
<li><code>impl<R: <a class="trait" href="../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>, Rsdr: <a class="trait" href="../rand/reseeding/trait.Reseeder.html" title="trait rand::reseeding::Reseeder">Reseeder</a><R>> Rng for <a class="struct" href="../rand/reseeding/struct.ReseedingRng.html" title="struct rand::reseeding::ReseedingRng">ReseedingRng</a><R, Rsdr></code></li>
|
||
<li><code>impl Rng for <a class="struct" href="../rand/os/struct.OsRng.html" title="struct rand::os::OsRng">OsRng</a></code></li>
|
||
<li><code>impl<R: <a class="trait" href="https://doc.rust-lang.org/nightly/std/io/trait.Read.html" title="trait std::io::Read">Read</a>> Rng for <a class="struct" href="../rand/read/struct.ReadRng.html" title="struct rand::read::ReadRng">ReadRng</a><R></code></li>
|
||
<li><code>impl<'a, R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> Rng for &'a mut R <span class="where fmt-newline">where<br> R: <a class="trait" href="../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>, </span></code></li>
|
||
<li><code>impl<R: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> Rng for <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a><R> <span class="where fmt-newline">where<br> R: <a class="trait" href="../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>, </span></code></li>
|
||
<li><code>impl Rng for <a class="struct" href="../rand/struct.XorShiftRng.html" title="struct rand::XorShiftRng">XorShiftRng</a></code></li>
|
||
<li><code>impl Rng for <a class="struct" href="../rand/struct.StdRng.html" title="struct rand::StdRng">StdRng</a></code></li>
|
||
<li><code>impl Rng for <a class="struct" href="../rand/struct.ThreadRng.html" title="struct rand::ThreadRng">ThreadRng</a></code></li>
|
||
</ul><script type="text/javascript" async
|
||
src="../implementors/rand/trait.Rng.js">
|
||
</script></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>?</dt>
|
||
<dd>Show this help dialog</dd>
|
||
<dt>S</dt>
|
||
<dd>Focus the search field</dd>
|
||
<dt>⇤</dt>
|
||
<dd>Move up in search results</dd>
|
||
<dt>⇥</dt>
|
||
<dd>Move down in search results</dd>
|
||
<dt>⏎</dt>
|
||
<dd>Go to active search result</dd>
|
||
<dt>+</dt>
|
||
<dd>Collapse/expand 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>
|
||
</div>
|
||
</div>
|
||
</aside>
|
||
|
||
|
||
|
||
<script>
|
||
window.rootPath = "../";
|
||
window.currentCrate = "rand";
|
||
</script>
|
||
<script src="../main.js"></script>
|
||
<script defer src="../search-index.js"></script>
|
||
</body>
|
||
</html> |