123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- <!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 `rand` crate.">
- <meta name="keywords" content="rust, rustlang, rust-lang, rand">
- <title>rand - 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="../main.css" id="themeStyle">
- <script src="../storage.js"></script>
-
- <link rel="shortcut icon" href="https://www.rust-lang.org/favicon.ico">
-
- </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">☰</div>
- <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'>Crate rand</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="#traits">Traits</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'rand', ty: 'mod', relpath: '../'};</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">
- <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'>Crate <a class="mod" href=''>rand</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#11-1214' title='goto source code'>[src]</a></span></h1>
- <div class='docblock'><p>Utilities for random number generation</p>
- <p>The key functions are <code>random()</code> and <code>Rng::gen()</code>. These are polymorphic and
- so can be used to generate any type that implements <code>Rand</code>. Type inference
- means that often a simple call to <code>rand::random()</code> or <code>rng.gen()</code> will
- suffice, but sometimes an annotation is required, e.g.
- <code>rand::random::<f64>()</code>.</p>
- <p>See the <code>distributions</code> submodule for sampling random numbers from
- distributions like normal and exponential.</p>
- <h1 id="usage" class="section-header"><a href="#usage">Usage</a></h1>
- <p>This crate is <a href="https://crates.io/crates/rand">on crates.io</a> and can be
- used by adding <code>rand</code> to the dependencies in your project's <code>Cargo.toml</code>.</p>
- <pre><code class="language-toml">[dependencies]
- rand = "0.4"
- </code></pre>
- <p>and this to your crate root:</p>
- <pre class="rust rust-example-rendered">
- <span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">rand</span>;</pre>
- <h1 id="thread-local-rng" class="section-header"><a href="#thread-local-rng">Thread-local RNG</a></h1>
- <p>There is built-in support for a RNG associated with each thread stored
- in thread-local storage. This RNG can be accessed via <code>thread_rng</code>, or
- used implicitly via <code>random</code>. This RNG is normally randomly seeded
- from an operating-system source of randomness, e.g. <code>/dev/urandom</code> on
- Unix systems, and will automatically reseed itself from this source
- after generating 32 KiB of random data.</p>
- <h1 id="cryptographic-security" class="section-header"><a href="#cryptographic-security">Cryptographic security</a></h1>
- <p>An application that requires an entropy source for cryptographic purposes
- must use <code>OsRng</code>, which reads randomness from the source that the operating
- system provides (e.g. <code>/dev/urandom</code> on Unixes or <code>CryptGenRandom()</code> on
- Windows).
- The other random number generators provided by this module are not suitable
- for such purposes.</p>
- <p><em>Note</em>: many Unix systems provide <code>/dev/random</code> as well as <code>/dev/urandom</code>.
- This module uses <code>/dev/urandom</code> for the following reasons:</p>
- <ul>
- <li>On Linux, <code>/dev/random</code> may block if entropy pool is empty;
- <code>/dev/urandom</code> will not block. This does not mean that <code>/dev/random</code>
- provides better output than <code>/dev/urandom</code>; the kernel internally runs a
- cryptographically secure pseudorandom number generator (CSPRNG) based on
- entropy pool for random number generation, so the "quality" of
- <code>/dev/random</code> is not better than <code>/dev/urandom</code> in most cases. However,
- this means that <code>/dev/urandom</code> can yield somewhat predictable randomness
- if the entropy pool is very small, such as immediately after first
- booting. Linux 3.17 added the <code>getrandom(2)</code> system call which solves
- the issue: it blocks if entropy pool is not initialized yet, but it does
- not block once initialized. <code>OsRng</code> tries to use <code>getrandom(2)</code> if
- available, and use <code>/dev/urandom</code> fallback if not. If an application
- does not have <code>getrandom</code> and likely to be run soon after first booting,
- or on a system with very few entropy sources, one should consider using
- <code>/dev/random</code> via <code>ReadRng</code>.</li>
- <li>On some systems (e.g. FreeBSD, OpenBSD and Mac OS X) there is no
- difference between the two sources. (Also note that, on some systems
- e.g. FreeBSD, both <code>/dev/random</code> and <code>/dev/urandom</code> may block once if
- the CSPRNG has not seeded yet.)</li>
- </ul>
- <h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
- <pre class="rust rust-example-rendered">
- <span class="kw">use</span> <span class="ident">rand</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">rand</span>::<span class="ident">thread_rng</span>();
- <span class="kw">if</span> <span class="ident">rng</span>.<span class="ident">gen</span>() { <span class="comment">// random bool</span>
- <span class="macro">println</span><span class="macro">!</span>(<span class="string">"i32: {}, u32: {}"</span>, <span class="ident">rng</span>.<span class="ident">gen</span>::<span class="op"><</span><span class="ident">i32</span><span class="op">></span>(), <span class="ident">rng</span>.<span class="ident">gen</span>::<span class="op"><</span><span class="ident">u32</span><span class="op">></span>())
- }</pre>
- <pre class="rust rust-example-rendered">
- <span class="kw">let</span> <span class="ident">tuple</span> <span class="op">=</span> <span class="ident">rand</span>::<span class="ident">random</span>::<span class="op"><</span>(<span class="ident">f64</span>, <span class="ident">char</span>)<span class="op">></span>();
- <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{:?}"</span>, <span class="ident">tuple</span>)</pre>
- <h2 id="monte-carlo-estimation-of-π" class="section-header"><a href="#monte-carlo-estimation-of-π">Monte Carlo estimation of π</a></h2>
- <p>For this example, imagine we have a square with sides of length 2 and a unit
- circle, both centered at the origin. Since the area of a unit circle is π,
- we have:</p>
- <pre><code class="language-text"> (area of unit circle) / (area of square) = π / 4
- </code></pre>
- <p>So if we sample many points randomly from the square, roughly π / 4 of them
- should be inside the circle.</p>
- <p>We can use the above fact to estimate the value of π: pick many points in
- the square at random, calculate the fraction that fall within the circle,
- and multiply this fraction by 4.</p>
- <pre class="rust rust-example-rendered">
- <span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">distributions</span>::{<span class="ident">IndependentSample</span>, <span class="ident">Range</span>};
- <span class="kw">fn</span> <span class="ident">main</span>() {
- <span class="kw">let</span> <span class="ident">between</span> <span class="op">=</span> <span class="ident">Range</span>::<span class="ident">new</span>(<span class="op">-</span><span class="number">1f64</span>, <span class="number">1.</span>);
- <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">rand</span>::<span class="ident">thread_rng</span>();
- <span class="kw">let</span> <span class="ident">total</span> <span class="op">=</span> <span class="number">1_000_000</span>;
- <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">in_circle</span> <span class="op">=</span> <span class="number">0</span>;
- <span class="kw">for</span> _ <span class="kw">in</span> <span class="number">0</span>..<span class="ident">total</span> {
- <span class="kw">let</span> <span class="ident">a</span> <span class="op">=</span> <span class="ident">between</span>.<span class="ident">ind_sample</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">rng</span>);
- <span class="kw">let</span> <span class="ident">b</span> <span class="op">=</span> <span class="ident">between</span>.<span class="ident">ind_sample</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">rng</span>);
- <span class="kw">if</span> <span class="ident">a</span><span class="kw-2">*</span><span class="ident">a</span> <span class="op">+</span> <span class="ident">b</span><span class="kw-2">*</span><span class="ident">b</span> <span class="op"><=</span> <span class="number">1.</span> {
- <span class="ident">in_circle</span> <span class="op">+=</span> <span class="number">1</span>;
- }
- }
- <span class="comment">// prints something close to 3.14159...</span>
- <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="number">4.</span> <span class="op">*</span> (<span class="ident">in_circle</span> <span class="kw">as</span> <span class="ident">f64</span>) <span class="op">/</span> (<span class="ident">total</span> <span class="kw">as</span> <span class="ident">f64</span>));
- }</pre>
- <h2 id="monty-hall-problem" class="section-header"><a href="#monty-hall-problem">Monty Hall Problem</a></h2>
- <p>This is a simulation of the <a href="http://en.wikipedia.org/wiki/Monty_Hall_problem">Monty Hall Problem</a>:</p>
- <blockquote>
- <p>Suppose you're on a game show, and you're given the choice of three doors:
- Behind one door is a car; behind the others, goats. You pick a door, say
- No. 1, and the host, who knows what's behind the doors, opens another
- door, say No. 3, which has a goat. He then says to you, "Do you want to
- pick door No. 2?" Is it to your advantage to switch your choice?</p>
- </blockquote>
- <p>The rather unintuitive answer is that you will have a 2/3 chance of winning
- if you switch and a 1/3 chance of winning if you don't, so it's better to
- switch.</p>
- <p>This program will simulate the game show and with large enough simulation
- steps it will indeed confirm that it is better to switch.</p>
- <pre class="rust rust-example-rendered">
- <span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">Rng</span>;
- <span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">distributions</span>::{<span class="ident">IndependentSample</span>, <span class="ident">Range</span>};
- <span class="kw">struct</span> <span class="ident">SimulationResult</span> {
- <span class="ident">win</span>: <span class="ident">bool</span>,
- <span class="ident">switch</span>: <span class="ident">bool</span>,
- }
- <span class="comment">// Run a single simulation of the Monty Hall problem.</span>
- <span class="kw">fn</span> <span class="ident">simulate</span><span class="op"><</span><span class="ident">R</span>: <span class="ident">Rng</span><span class="op">></span>(<span class="ident">random_door</span>: <span class="kw-2">&</span><span class="ident">Range</span><span class="op"><</span><span class="ident">u32</span><span class="op">></span>, <span class="ident">rng</span>: <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">R</span>)
- <span class="op">-></span> <span class="ident">SimulationResult</span> {
- <span class="kw">let</span> <span class="ident">car</span> <span class="op">=</span> <span class="ident">random_door</span>.<span class="ident">ind_sample</span>(<span class="ident">rng</span>);
- <span class="comment">// This is our initial choice</span>
- <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">choice</span> <span class="op">=</span> <span class="ident">random_door</span>.<span class="ident">ind_sample</span>(<span class="ident">rng</span>);
- <span class="comment">// The game host opens a door</span>
- <span class="kw">let</span> <span class="ident">open</span> <span class="op">=</span> <span class="ident">game_host_open</span>(<span class="ident">car</span>, <span class="ident">choice</span>, <span class="ident">rng</span>);
- <span class="comment">// Shall we switch?</span>
- <span class="kw">let</span> <span class="ident">switch</span> <span class="op">=</span> <span class="ident">rng</span>.<span class="ident">gen</span>();
- <span class="kw">if</span> <span class="ident">switch</span> {
- <span class="ident">choice</span> <span class="op">=</span> <span class="ident">switch_door</span>(<span class="ident">choice</span>, <span class="ident">open</span>);
- }
- <span class="ident">SimulationResult</span> { <span class="ident">win</span>: <span class="ident">choice</span> <span class="op">==</span> <span class="ident">car</span>, <span class="ident">switch</span>: <span class="ident">switch</span> }
- }
- <span class="comment">// Returns the door the game host opens given our choice and knowledge of</span>
- <span class="comment">// where the car is. The game host will never open the door with the car.</span>
- <span class="kw">fn</span> <span class="ident">game_host_open</span><span class="op"><</span><span class="ident">R</span>: <span class="ident">Rng</span><span class="op">></span>(<span class="ident">car</span>: <span class="ident">u32</span>, <span class="ident">choice</span>: <span class="ident">u32</span>, <span class="ident">rng</span>: <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">R</span>) <span class="op">-></span> <span class="ident">u32</span> {
- <span class="kw">let</span> <span class="ident">choices</span> <span class="op">=</span> <span class="ident">free_doors</span>(<span class="kw-2">&</span>[<span class="ident">car</span>, <span class="ident">choice</span>]);
- <span class="ident">rand</span>::<span class="ident">seq</span>::<span class="ident">sample_slice</span>(<span class="ident">rng</span>, <span class="kw-2">&</span><span class="ident">choices</span>, <span class="number">1</span>)[<span class="number">0</span>]
- }
- <span class="comment">// Returns the door we switch to, given our current choice and</span>
- <span class="comment">// the open door. There will only be one valid door.</span>
- <span class="kw">fn</span> <span class="ident">switch_door</span>(<span class="ident">choice</span>: <span class="ident">u32</span>, <span class="ident">open</span>: <span class="ident">u32</span>) <span class="op">-></span> <span class="ident">u32</span> {
- <span class="ident">free_doors</span>(<span class="kw-2">&</span>[<span class="ident">choice</span>, <span class="ident">open</span>])[<span class="number">0</span>]
- }
- <span class="kw">fn</span> <span class="ident">free_doors</span>(<span class="ident">blocked</span>: <span class="kw-2">&</span>[<span class="ident">u32</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="number">0</span>..<span class="number">3</span>).<span class="ident">filter</span>(<span class="op">|</span><span class="ident">x</span><span class="op">|</span> <span class="op">!</span><span class="ident">blocked</span>.<span class="ident">contains</span>(<span class="ident">x</span>)).<span class="ident">collect</span>()
- }
- <span class="kw">fn</span> <span class="ident">main</span>() {
- <span class="comment">// The estimation will be more accurate with more simulations</span>
- <span class="kw">let</span> <span class="ident">num_simulations</span> <span class="op">=</span> <span class="number">10000</span>;
- <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">rand</span>::<span class="ident">thread_rng</span>();
- <span class="kw">let</span> <span class="ident">random_door</span> <span class="op">=</span> <span class="ident">Range</span>::<span class="ident">new</span>(<span class="number">0</span>, <span class="number">3</span>);
- <span class="kw">let</span> (<span class="kw-2">mut</span> <span class="ident">switch_wins</span>, <span class="kw-2">mut</span> <span class="ident">switch_losses</span>) <span class="op">=</span> (<span class="number">0</span>, <span class="number">0</span>);
- <span class="kw">let</span> (<span class="kw-2">mut</span> <span class="ident">keep_wins</span>, <span class="kw-2">mut</span> <span class="ident">keep_losses</span>) <span class="op">=</span> (<span class="number">0</span>, <span class="number">0</span>);
- <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Running {} simulations..."</span>, <span class="ident">num_simulations</span>);
- <span class="kw">for</span> _ <span class="kw">in</span> <span class="number">0</span>..<span class="ident">num_simulations</span> {
- <span class="kw">let</span> <span class="ident">result</span> <span class="op">=</span> <span class="ident">simulate</span>(<span class="kw-2">&</span><span class="ident">random_door</span>, <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">rng</span>);
- <span class="kw">match</span> (<span class="ident">result</span>.<span class="ident">win</span>, <span class="ident">result</span>.<span class="ident">switch</span>) {
- (<span class="bool-val">true</span>, <span class="bool-val">true</span>) <span class="op">=></span> <span class="ident">switch_wins</span> <span class="op">+=</span> <span class="number">1</span>,
- (<span class="bool-val">true</span>, <span class="bool-val">false</span>) <span class="op">=></span> <span class="ident">keep_wins</span> <span class="op">+=</span> <span class="number">1</span>,
- (<span class="bool-val">false</span>, <span class="bool-val">true</span>) <span class="op">=></span> <span class="ident">switch_losses</span> <span class="op">+=</span> <span class="number">1</span>,
- (<span class="bool-val">false</span>, <span class="bool-val">false</span>) <span class="op">=></span> <span class="ident">keep_losses</span> <span class="op">+=</span> <span class="number">1</span>,
- }
- }
- <span class="kw">let</span> <span class="ident">total_switches</span> <span class="op">=</span> <span class="ident">switch_wins</span> <span class="op">+</span> <span class="ident">switch_losses</span>;
- <span class="kw">let</span> <span class="ident">total_keeps</span> <span class="op">=</span> <span class="ident">keep_wins</span> <span class="op">+</span> <span class="ident">keep_losses</span>;
- <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Switched door {} times with {} wins and {} losses"</span>,
- <span class="ident">total_switches</span>, <span class="ident">switch_wins</span>, <span class="ident">switch_losses</span>);
- <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Kept our choice {} times with {} wins and {} losses"</span>,
- <span class="ident">total_keeps</span>, <span class="ident">keep_wins</span>, <span class="ident">keep_losses</span>);
- <span class="comment">// With a large number of simulations, the values should converge to</span>
- <span class="comment">// 0.667 and 0.333 respectively.</span>
- <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Estimated chance to win if we switch: {}"</span>,
- <span class="ident">switch_wins</span> <span class="kw">as</span> <span class="ident">f32</span> <span class="op">/</span> <span class="ident">total_switches</span> <span class="kw">as</span> <span class="ident">f32</span>);
- <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Estimated chance to win if we don't: {}"</span>,
- <span class="ident">keep_wins</span> <span class="kw">as</span> <span class="ident">f32</span> <span class="op">/</span> <span class="ident">total_keeps</span> <span class="kw">as</span> <span class="ident">f32</span>);
- }</pre>
- </div><h2 id='reexports' class='section-header'><a href="#reexports">Re-exports</a></h2>
- <table><tr><td><code>pub use jitter::<a class="struct" href="../rand/jitter/struct.JitterRng.html" title="struct rand::jitter::JitterRng">JitterRng</a>;</code></td></tr><tr><td><code>pub use os::<a class="struct" href="../rand/os/struct.OsRng.html" title="struct rand::os::OsRng">OsRng</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="chacha/index.html"
- title='mod rand::chacha'>chacha</a></td>
- <td class='docblock-short'>
- <p>The ChaCha random number generator.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="mod" href="distributions/index.html"
- title='mod rand::distributions'>distributions</a></td>
- <td class='docblock-short'>
- <p>Sampling from random distributions.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="mod" href="isaac/index.html"
- title='mod rand::isaac'>isaac</a></td>
- <td class='docblock-short'>
- <p>The ISAAC random number generator.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="mod" href="jitter/index.html"
- title='mod rand::jitter'>jitter</a></td>
- <td class='docblock-short'>
- <p>Non-physical true random number generator based on timing jitter.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="mod" href="os/index.html"
- title='mod rand::os'>os</a></td>
- <td class='docblock-short'>
- <p>Interfaces to the operating system provided random number
- generators.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="mod" href="read/index.html"
- title='mod rand::read'>read</a></td>
- <td class='docblock-short'>
- <p>A wrapper around any Read to treat it as an RNG.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="mod" href="reseeding/index.html"
- title='mod rand::reseeding'>reseeding</a></td>
- <td class='docblock-short'>
- <p>A wrapper around another RNG that reseeds it after it
- generates a certain number of random bytes.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="mod" href="seq/index.html"
- title='mod rand::seq'>seq</a></td>
- <td class='docblock-short'>
- <p>Functions for randomly accessing and sampling sequences.</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.AsciiGenerator.html"
- title='struct rand::AsciiGenerator'>AsciiGenerator</a></td>
- <td class='docblock-short'>
- <p>Iterator which will continuously generate random ascii characters.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="struct" href="struct.ChaChaRng.html"
- title='struct rand::ChaChaRng'>ChaChaRng</a></td>
- <td class='docblock-short'>
- <p>A random number generator that uses the ChaCha20 algorithm [1].</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="struct" href="struct.Closed01.html"
- title='struct rand::Closed01'>Closed01</a></td>
- <td class='docblock-short'>
- <p>A wrapper for generating floating point numbers uniformly in the
- closed interval <code>[0,1]</code> (including both endpoints).</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="struct" href="struct.Generator.html"
- title='struct rand::Generator'>Generator</a></td>
- <td class='docblock-short'>
- <p>Iterator which will generate a stream of random items.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="struct" href="struct.Isaac64Rng.html"
- title='struct rand::Isaac64Rng'>Isaac64Rng</a></td>
- <td class='docblock-short'>
- <p>A random number generator that uses ISAAC-64[1], the 64-bit
- variant of the ISAAC algorithm.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="struct" href="struct.IsaacRng.html"
- title='struct rand::IsaacRng'>IsaacRng</a></td>
- <td class='docblock-short'>
- <p>A random number generator that uses the ISAAC algorithm[1].</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="struct" href="struct.Open01.html"
- title='struct rand::Open01'>Open01</a></td>
- <td class='docblock-short'>
- <p>A wrapper for generating floating point numbers uniformly in the
- open interval <code>(0,1)</code> (not including either endpoint).</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="struct" href="struct.StdRng.html"
- title='struct rand::StdRng'>StdRng</a></td>
- <td class='docblock-short'>
- <p>The standard RNG. This is designed to be efficient on the current
- platform.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="struct" href="struct.ThreadRng.html"
- title='struct rand::ThreadRng'>ThreadRng</a></td>
- <td class='docblock-short'>
- <p>The thread-local RNG.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="struct" href="struct.XorShiftRng.html"
- title='struct rand::XorShiftRng'>XorShiftRng</a></td>
- <td class='docblock-short'>
- <p>An Xorshift[1] random number
- generator.</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.Rand.html"
- title='trait rand::Rand'>Rand</a></td>
- <td class='docblock-short'>
- <p>A type that can be randomly generated using an <code>Rng</code>.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="trait" href="trait.Rng.html"
- title='trait rand::Rng'>Rng</a></td>
- <td class='docblock-short'>
- <p>A random number generator.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="trait" href="trait.SeedableRng.html"
- title='trait rand::SeedableRng'>SeedableRng</a></td>
- <td class='docblock-short'>
- <p>A random number generator that can be explicitly seeded to produce
- the same stream of randomness multiple times.</p>
- </td>
- </tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
- <table>
- <tr class=' module-item'>
- <td><a class="fn" href="fn.random.html"
- title='fn rand::random'>random</a></td>
- <td class='docblock-short'>
- <p>Generates a random value using the thread-local random number generator.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="fn" href="fn.sample.html"
- title='fn rand::sample'>sample</a></td>
- <td class='docblock-short'>
- [<div class='stab deprecated'>Deprecated</div>] <p>DEPRECATED: use <code>seq::sample_iter</code> instead.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="fn" href="fn.thread_rng.html"
- title='fn rand::thread_rng'>thread_rng</a></td>
- <td class='docblock-short'>
- <p>Retrieve the lazily-initialized thread-local random number
- generator, seeded by the system. Intended to be used in method
- chaining style, e.g. <code>thread_rng().gen::<i32>()</code>.</p>
- </td>
- </tr>
- <tr class=' module-item'>
- <td><a class="fn" href="fn.weak_rng.html"
- title='fn rand::weak_rng'>weak_rng</a></td>
- <td class='docblock-short'>
- <p>Create a weak random number generator with a default algorithm and seed.</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>⏎</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>
- </div>
- </div>
- </aside>
-
- <script>
- window.rootPath = "../";
- window.currentCrate = "rand";
- </script>
- <script src="../main.js"></script>
- <script defer src="../search-index.js"></script>
- </body>
- </html>
|