index.html 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="generator" content="rustdoc">
  7. <meta name="description" content="API documentation for the Rust `rand` crate.">
  8. <meta name="keywords" content="rust, rustlang, rust-lang, rand">
  9. <title>rand - Rust</title>
  10. <link rel="stylesheet" type="text/css" href="../normalize.css">
  11. <link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle">
  12. <link rel="stylesheet" type="text/css" href="../dark.css">
  13. <link rel="stylesheet" type="text/css" href="../main.css" id="themeStyle">
  14. <script src="../storage.js"></script>
  15. <link rel="shortcut icon" href="https://www.rust-lang.org/favicon.ico">
  16. </head>
  17. <body class="rustdoc mod">
  18. <!--[if lte IE 8]>
  19. <div class="warning">
  20. This old browser is unsupported and will most likely display funky
  21. things.
  22. </div>
  23. <![endif]-->
  24. <nav class="sidebar">
  25. <div class="sidebar-menu">&#9776;</div>
  26. <a href='../rand/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk.png' alt='logo' width='100'></a>
  27. <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>
  28. </nav>
  29. <div class="theme-picker">
  30. <button id="theme-picker" aria-label="Pick another theme!">
  31. <img src="../brush.svg" width="18" alt="Pick another theme!">
  32. </button>
  33. <div id="theme-choices"></div>
  34. </div>
  35. <script src="../theme.js"></script>
  36. <nav class="sub">
  37. <form class="search-form js-only">
  38. <div class="search-container">
  39. <input class="search-input" name="search"
  40. autocomplete="off"
  41. placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
  42. type="search">
  43. </div>
  44. </form>
  45. </nav>
  46. <section id='main' class="content">
  47. <h1 class='fqn'><span class='in-band'>Crate <a class="mod" href=''>rand</a></span><span class='out-of-band'><span id='render-detail'>
  48. <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
  49. [<span class='inner'>&#x2212;</span>]
  50. </a>
  51. </span><a class='srclink' href='../src/rand/lib.rs.html#11-1214' title='goto source code'>[src]</a></span></h1>
  52. <div class='docblock'><p>Utilities for random number generation</p>
  53. <p>The key functions are <code>random()</code> and <code>Rng::gen()</code>. These are polymorphic and
  54. so can be used to generate any type that implements <code>Rand</code>. Type inference
  55. means that often a simple call to <code>rand::random()</code> or <code>rng.gen()</code> will
  56. suffice, but sometimes an annotation is required, e.g.
  57. <code>rand::random::&lt;f64&gt;()</code>.</p>
  58. <p>See the <code>distributions</code> submodule for sampling random numbers from
  59. distributions like normal and exponential.</p>
  60. <h1 id="usage" class="section-header"><a href="#usage">Usage</a></h1>
  61. <p>This crate is <a href="https://crates.io/crates/rand">on crates.io</a> and can be
  62. used by adding <code>rand</code> to the dependencies in your project's <code>Cargo.toml</code>.</p>
  63. <pre><code class="language-toml">[dependencies]
  64. rand = &quot;0.4&quot;
  65. </code></pre>
  66. <p>and this to your crate root:</p>
  67. <pre class="rust rust-example-rendered">
  68. <span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">rand</span>;</pre>
  69. <h1 id="thread-local-rng" class="section-header"><a href="#thread-local-rng">Thread-local RNG</a></h1>
  70. <p>There is built-in support for a RNG associated with each thread stored
  71. in thread-local storage. This RNG can be accessed via <code>thread_rng</code>, or
  72. used implicitly via <code>random</code>. This RNG is normally randomly seeded
  73. from an operating-system source of randomness, e.g. <code>/dev/urandom</code> on
  74. Unix systems, and will automatically reseed itself from this source
  75. after generating 32 KiB of random data.</p>
  76. <h1 id="cryptographic-security" class="section-header"><a href="#cryptographic-security">Cryptographic security</a></h1>
  77. <p>An application that requires an entropy source for cryptographic purposes
  78. must use <code>OsRng</code>, which reads randomness from the source that the operating
  79. system provides (e.g. <code>/dev/urandom</code> on Unixes or <code>CryptGenRandom()</code> on
  80. Windows).
  81. The other random number generators provided by this module are not suitable
  82. for such purposes.</p>
  83. <p><em>Note</em>: many Unix systems provide <code>/dev/random</code> as well as <code>/dev/urandom</code>.
  84. This module uses <code>/dev/urandom</code> for the following reasons:</p>
  85. <ul>
  86. <li>On Linux, <code>/dev/random</code> may block if entropy pool is empty;
  87. <code>/dev/urandom</code> will not block. This does not mean that <code>/dev/random</code>
  88. provides better output than <code>/dev/urandom</code>; the kernel internally runs a
  89. cryptographically secure pseudorandom number generator (CSPRNG) based on
  90. entropy pool for random number generation, so the &quot;quality&quot; of
  91. <code>/dev/random</code> is not better than <code>/dev/urandom</code> in most cases. However,
  92. this means that <code>/dev/urandom</code> can yield somewhat predictable randomness
  93. if the entropy pool is very small, such as immediately after first
  94. booting. Linux 3.17 added the <code>getrandom(2)</code> system call which solves
  95. the issue: it blocks if entropy pool is not initialized yet, but it does
  96. not block once initialized. <code>OsRng</code> tries to use <code>getrandom(2)</code> if
  97. available, and use <code>/dev/urandom</code> fallback if not. If an application
  98. does not have <code>getrandom</code> and likely to be run soon after first booting,
  99. or on a system with very few entropy sources, one should consider using
  100. <code>/dev/random</code> via <code>ReadRng</code>.</li>
  101. <li>On some systems (e.g. FreeBSD, OpenBSD and Mac OS X) there is no
  102. difference between the two sources. (Also note that, on some systems
  103. e.g. FreeBSD, both <code>/dev/random</code> and <code>/dev/urandom</code> may block once if
  104. the CSPRNG has not seeded yet.)</li>
  105. </ul>
  106. <h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
  107. <pre class="rust rust-example-rendered">
  108. <span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">Rng</span>;
  109. <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>();
  110. <span class="kw">if</span> <span class="ident">rng</span>.<span class="ident">gen</span>() { <span class="comment">// random bool</span>
  111. <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;i32: {}, u32: {}&quot;</span>, <span class="ident">rng</span>.<span class="ident">gen</span>::<span class="op">&lt;</span><span class="ident">i32</span><span class="op">&gt;</span>(), <span class="ident">rng</span>.<span class="ident">gen</span>::<span class="op">&lt;</span><span class="ident">u32</span><span class="op">&gt;</span>())
  112. }</pre>
  113. <pre class="rust rust-example-rendered">
  114. <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">&lt;</span>(<span class="ident">f64</span>, <span class="ident">char</span>)<span class="op">&gt;</span>();
  115. <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{:?}&quot;</span>, <span class="ident">tuple</span>)</pre>
  116. <h2 id="monte-carlo-estimation-of-π" class="section-header"><a href="#monte-carlo-estimation-of-π">Monte Carlo estimation of π</a></h2>
  117. <p>For this example, imagine we have a square with sides of length 2 and a unit
  118. circle, both centered at the origin. Since the area of a unit circle is π,
  119. we have:</p>
  120. <pre><code class="language-text"> (area of unit circle) / (area of square) = π / 4
  121. </code></pre>
  122. <p>So if we sample many points randomly from the square, roughly π / 4 of them
  123. should be inside the circle.</p>
  124. <p>We can use the above fact to estimate the value of π: pick many points in
  125. the square at random, calculate the fraction that fall within the circle,
  126. and multiply this fraction by 4.</p>
  127. <pre class="rust rust-example-rendered">
  128. <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>};
  129. <span class="kw">fn</span> <span class="ident">main</span>() {
  130. <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>);
  131. <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>();
  132. <span class="kw">let</span> <span class="ident">total</span> <span class="op">=</span> <span class="number">1_000_000</span>;
  133. <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>;
  134. <span class="kw">for</span> _ <span class="kw">in</span> <span class="number">0</span>..<span class="ident">total</span> {
  135. <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">&amp;</span><span class="kw-2">mut</span> <span class="ident">rng</span>);
  136. <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">&amp;</span><span class="kw-2">mut</span> <span class="ident">rng</span>);
  137. <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">&lt;=</span> <span class="number">1.</span> {
  138. <span class="ident">in_circle</span> <span class="op">+=</span> <span class="number">1</span>;
  139. }
  140. }
  141. <span class="comment">// prints something close to 3.14159...</span>
  142. <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</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>));
  143. }</pre>
  144. <h2 id="monty-hall-problem" class="section-header"><a href="#monty-hall-problem">Monty Hall Problem</a></h2>
  145. <p>This is a simulation of the <a href="http://en.wikipedia.org/wiki/Monty_Hall_problem">Monty Hall Problem</a>:</p>
  146. <blockquote>
  147. <p>Suppose you're on a game show, and you're given the choice of three doors:
  148. Behind one door is a car; behind the others, goats. You pick a door, say
  149. No. 1, and the host, who knows what's behind the doors, opens another
  150. door, say No. 3, which has a goat. He then says to you, &quot;Do you want to
  151. pick door No. 2?&quot; Is it to your advantage to switch your choice?</p>
  152. </blockquote>
  153. <p>The rather unintuitive answer is that you will have a 2/3 chance of winning
  154. if you switch and a 1/3 chance of winning if you don't, so it's better to
  155. switch.</p>
  156. <p>This program will simulate the game show and with large enough simulation
  157. steps it will indeed confirm that it is better to switch.</p>
  158. <pre class="rust rust-example-rendered">
  159. <span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">Rng</span>;
  160. <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>};
  161. <span class="kw">struct</span> <span class="ident">SimulationResult</span> {
  162. <span class="ident">win</span>: <span class="ident">bool</span>,
  163. <span class="ident">switch</span>: <span class="ident">bool</span>,
  164. }
  165. <span class="comment">// Run a single simulation of the Monty Hall problem.</span>
  166. <span class="kw">fn</span> <span class="ident">simulate</span><span class="op">&lt;</span><span class="ident">R</span>: <span class="ident">Rng</span><span class="op">&gt;</span>(<span class="ident">random_door</span>: <span class="kw-2">&amp;</span><span class="ident">Range</span><span class="op">&lt;</span><span class="ident">u32</span><span class="op">&gt;</span>, <span class="ident">rng</span>: <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">R</span>)
  167. <span class="op">-&gt;</span> <span class="ident">SimulationResult</span> {
  168. <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>);
  169. <span class="comment">// This is our initial choice</span>
  170. <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>);
  171. <span class="comment">// The game host opens a door</span>
  172. <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>);
  173. <span class="comment">// Shall we switch?</span>
  174. <span class="kw">let</span> <span class="ident">switch</span> <span class="op">=</span> <span class="ident">rng</span>.<span class="ident">gen</span>();
  175. <span class="kw">if</span> <span class="ident">switch</span> {
  176. <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>);
  177. }
  178. <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> }
  179. }
  180. <span class="comment">// Returns the door the game host opens given our choice and knowledge of</span>
  181. <span class="comment">// where the car is. The game host will never open the door with the car.</span>
  182. <span class="kw">fn</span> <span class="ident">game_host_open</span><span class="op">&lt;</span><span class="ident">R</span>: <span class="ident">Rng</span><span class="op">&gt;</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">&amp;</span><span class="kw-2">mut</span> <span class="ident">R</span>) <span class="op">-&gt;</span> <span class="ident">u32</span> {
  183. <span class="kw">let</span> <span class="ident">choices</span> <span class="op">=</span> <span class="ident">free_doors</span>(<span class="kw-2">&amp;</span>[<span class="ident">car</span>, <span class="ident">choice</span>]);
  184. <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">&amp;</span><span class="ident">choices</span>, <span class="number">1</span>)[<span class="number">0</span>]
  185. }
  186. <span class="comment">// Returns the door we switch to, given our current choice and</span>
  187. <span class="comment">// the open door. There will only be one valid door.</span>
  188. <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">-&gt;</span> <span class="ident">u32</span> {
  189. <span class="ident">free_doors</span>(<span class="kw-2">&amp;</span>[<span class="ident">choice</span>, <span class="ident">open</span>])[<span class="number">0</span>]
  190. }
  191. <span class="kw">fn</span> <span class="ident">free_doors</span>(<span class="ident">blocked</span>: <span class="kw-2">&amp;</span>[<span class="ident">u32</span>]) <span class="op">-&gt;</span> <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">u32</span><span class="op">&gt;</span> {
  192. (<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>()
  193. }
  194. <span class="kw">fn</span> <span class="ident">main</span>() {
  195. <span class="comment">// The estimation will be more accurate with more simulations</span>
  196. <span class="kw">let</span> <span class="ident">num_simulations</span> <span class="op">=</span> <span class="number">10000</span>;
  197. <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>();
  198. <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>);
  199. <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>);
  200. <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>);
  201. <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Running {} simulations...&quot;</span>, <span class="ident">num_simulations</span>);
  202. <span class="kw">for</span> _ <span class="kw">in</span> <span class="number">0</span>..<span class="ident">num_simulations</span> {
  203. <span class="kw">let</span> <span class="ident">result</span> <span class="op">=</span> <span class="ident">simulate</span>(<span class="kw-2">&amp;</span><span class="ident">random_door</span>, <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">rng</span>);
  204. <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>) {
  205. (<span class="bool-val">true</span>, <span class="bool-val">true</span>) <span class="op">=&gt;</span> <span class="ident">switch_wins</span> <span class="op">+=</span> <span class="number">1</span>,
  206. (<span class="bool-val">true</span>, <span class="bool-val">false</span>) <span class="op">=&gt;</span> <span class="ident">keep_wins</span> <span class="op">+=</span> <span class="number">1</span>,
  207. (<span class="bool-val">false</span>, <span class="bool-val">true</span>) <span class="op">=&gt;</span> <span class="ident">switch_losses</span> <span class="op">+=</span> <span class="number">1</span>,
  208. (<span class="bool-val">false</span>, <span class="bool-val">false</span>) <span class="op">=&gt;</span> <span class="ident">keep_losses</span> <span class="op">+=</span> <span class="number">1</span>,
  209. }
  210. }
  211. <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>;
  212. <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>;
  213. <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Switched door {} times with {} wins and {} losses&quot;</span>,
  214. <span class="ident">total_switches</span>, <span class="ident">switch_wins</span>, <span class="ident">switch_losses</span>);
  215. <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Kept our choice {} times with {} wins and {} losses&quot;</span>,
  216. <span class="ident">total_keeps</span>, <span class="ident">keep_wins</span>, <span class="ident">keep_losses</span>);
  217. <span class="comment">// With a large number of simulations, the values should converge to</span>
  218. <span class="comment">// 0.667 and 0.333 respectively.</span>
  219. <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Estimated chance to win if we switch: {}&quot;</span>,
  220. <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>);
  221. <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Estimated chance to win if we don&#39;t: {}&quot;</span>,
  222. <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>);
  223. }</pre>
  224. </div><h2 id='reexports' class='section-header'><a href="#reexports">Re-exports</a></h2>
  225. <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>
  226. <table>
  227. <tr class=' module-item'>
  228. <td><a class="mod" href="chacha/index.html"
  229. title='mod rand::chacha'>chacha</a></td>
  230. <td class='docblock-short'>
  231. <p>The ChaCha random number generator.</p>
  232. </td>
  233. </tr>
  234. <tr class=' module-item'>
  235. <td><a class="mod" href="distributions/index.html"
  236. title='mod rand::distributions'>distributions</a></td>
  237. <td class='docblock-short'>
  238. <p>Sampling from random distributions.</p>
  239. </td>
  240. </tr>
  241. <tr class=' module-item'>
  242. <td><a class="mod" href="isaac/index.html"
  243. title='mod rand::isaac'>isaac</a></td>
  244. <td class='docblock-short'>
  245. <p>The ISAAC random number generator.</p>
  246. </td>
  247. </tr>
  248. <tr class=' module-item'>
  249. <td><a class="mod" href="jitter/index.html"
  250. title='mod rand::jitter'>jitter</a></td>
  251. <td class='docblock-short'>
  252. <p>Non-physical true random number generator based on timing jitter.</p>
  253. </td>
  254. </tr>
  255. <tr class=' module-item'>
  256. <td><a class="mod" href="os/index.html"
  257. title='mod rand::os'>os</a></td>
  258. <td class='docblock-short'>
  259. <p>Interfaces to the operating system provided random number
  260. generators.</p>
  261. </td>
  262. </tr>
  263. <tr class=' module-item'>
  264. <td><a class="mod" href="read/index.html"
  265. title='mod rand::read'>read</a></td>
  266. <td class='docblock-short'>
  267. <p>A wrapper around any Read to treat it as an RNG.</p>
  268. </td>
  269. </tr>
  270. <tr class=' module-item'>
  271. <td><a class="mod" href="reseeding/index.html"
  272. title='mod rand::reseeding'>reseeding</a></td>
  273. <td class='docblock-short'>
  274. <p>A wrapper around another RNG that reseeds it after it
  275. generates a certain number of random bytes.</p>
  276. </td>
  277. </tr>
  278. <tr class=' module-item'>
  279. <td><a class="mod" href="seq/index.html"
  280. title='mod rand::seq'>seq</a></td>
  281. <td class='docblock-short'>
  282. <p>Functions for randomly accessing and sampling sequences.</p>
  283. </td>
  284. </tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
  285. <table>
  286. <tr class=' module-item'>
  287. <td><a class="struct" href="struct.AsciiGenerator.html"
  288. title='struct rand::AsciiGenerator'>AsciiGenerator</a></td>
  289. <td class='docblock-short'>
  290. <p>Iterator which will continuously generate random ascii characters.</p>
  291. </td>
  292. </tr>
  293. <tr class=' module-item'>
  294. <td><a class="struct" href="struct.ChaChaRng.html"
  295. title='struct rand::ChaChaRng'>ChaChaRng</a></td>
  296. <td class='docblock-short'>
  297. <p>A random number generator that uses the ChaCha20 algorithm [1].</p>
  298. </td>
  299. </tr>
  300. <tr class=' module-item'>
  301. <td><a class="struct" href="struct.Closed01.html"
  302. title='struct rand::Closed01'>Closed01</a></td>
  303. <td class='docblock-short'>
  304. <p>A wrapper for generating floating point numbers uniformly in the
  305. closed interval <code>[0,1]</code> (including both endpoints).</p>
  306. </td>
  307. </tr>
  308. <tr class=' module-item'>
  309. <td><a class="struct" href="struct.Generator.html"
  310. title='struct rand::Generator'>Generator</a></td>
  311. <td class='docblock-short'>
  312. <p>Iterator which will generate a stream of random items.</p>
  313. </td>
  314. </tr>
  315. <tr class=' module-item'>
  316. <td><a class="struct" href="struct.Isaac64Rng.html"
  317. title='struct rand::Isaac64Rng'>Isaac64Rng</a></td>
  318. <td class='docblock-short'>
  319. <p>A random number generator that uses ISAAC-64[1], the 64-bit
  320. variant of the ISAAC algorithm.</p>
  321. </td>
  322. </tr>
  323. <tr class=' module-item'>
  324. <td><a class="struct" href="struct.IsaacRng.html"
  325. title='struct rand::IsaacRng'>IsaacRng</a></td>
  326. <td class='docblock-short'>
  327. <p>A random number generator that uses the ISAAC algorithm[1].</p>
  328. </td>
  329. </tr>
  330. <tr class=' module-item'>
  331. <td><a class="struct" href="struct.Open01.html"
  332. title='struct rand::Open01'>Open01</a></td>
  333. <td class='docblock-short'>
  334. <p>A wrapper for generating floating point numbers uniformly in the
  335. open interval <code>(0,1)</code> (not including either endpoint).</p>
  336. </td>
  337. </tr>
  338. <tr class=' module-item'>
  339. <td><a class="struct" href="struct.StdRng.html"
  340. title='struct rand::StdRng'>StdRng</a></td>
  341. <td class='docblock-short'>
  342. <p>The standard RNG. This is designed to be efficient on the current
  343. platform.</p>
  344. </td>
  345. </tr>
  346. <tr class=' module-item'>
  347. <td><a class="struct" href="struct.ThreadRng.html"
  348. title='struct rand::ThreadRng'>ThreadRng</a></td>
  349. <td class='docblock-short'>
  350. <p>The thread-local RNG.</p>
  351. </td>
  352. </tr>
  353. <tr class=' module-item'>
  354. <td><a class="struct" href="struct.XorShiftRng.html"
  355. title='struct rand::XorShiftRng'>XorShiftRng</a></td>
  356. <td class='docblock-short'>
  357. <p>An Xorshift[1] random number
  358. generator.</p>
  359. </td>
  360. </tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
  361. <table>
  362. <tr class=' module-item'>
  363. <td><a class="trait" href="trait.Rand.html"
  364. title='trait rand::Rand'>Rand</a></td>
  365. <td class='docblock-short'>
  366. <p>A type that can be randomly generated using an <code>Rng</code>.</p>
  367. </td>
  368. </tr>
  369. <tr class=' module-item'>
  370. <td><a class="trait" href="trait.Rng.html"
  371. title='trait rand::Rng'>Rng</a></td>
  372. <td class='docblock-short'>
  373. <p>A random number generator.</p>
  374. </td>
  375. </tr>
  376. <tr class=' module-item'>
  377. <td><a class="trait" href="trait.SeedableRng.html"
  378. title='trait rand::SeedableRng'>SeedableRng</a></td>
  379. <td class='docblock-short'>
  380. <p>A random number generator that can be explicitly seeded to produce
  381. the same stream of randomness multiple times.</p>
  382. </td>
  383. </tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
  384. <table>
  385. <tr class=' module-item'>
  386. <td><a class="fn" href="fn.random.html"
  387. title='fn rand::random'>random</a></td>
  388. <td class='docblock-short'>
  389. <p>Generates a random value using the thread-local random number generator.</p>
  390. </td>
  391. </tr>
  392. <tr class=' module-item'>
  393. <td><a class="fn" href="fn.sample.html"
  394. title='fn rand::sample'>sample</a></td>
  395. <td class='docblock-short'>
  396. [<div class='stab deprecated'>Deprecated</div>] <p>DEPRECATED: use <code>seq::sample_iter</code> instead.</p>
  397. </td>
  398. </tr>
  399. <tr class=' module-item'>
  400. <td><a class="fn" href="fn.thread_rng.html"
  401. title='fn rand::thread_rng'>thread_rng</a></td>
  402. <td class='docblock-short'>
  403. <p>Retrieve the lazily-initialized thread-local random number
  404. generator, seeded by the system. Intended to be used in method
  405. chaining style, e.g. <code>thread_rng().gen::&lt;i32&gt;()</code>.</p>
  406. </td>
  407. </tr>
  408. <tr class=' module-item'>
  409. <td><a class="fn" href="fn.weak_rng.html"
  410. title='fn rand::weak_rng'>weak_rng</a></td>
  411. <td class='docblock-short'>
  412. <p>Create a weak random number generator with a default algorithm and seed.</p>
  413. </td>
  414. </tr></table></section>
  415. <section id='search' class="content hidden"></section>
  416. <section class="footer"></section>
  417. <aside id="help" class="hidden">
  418. <div>
  419. <h1 class="hidden">Help</h1>
  420. <div class="shortcuts">
  421. <h2>Keyboard Shortcuts</h2>
  422. <dl>
  423. <dt><kbd>?</kbd></dt>
  424. <dd>Show this help dialog</dd>
  425. <dt><kbd>S</kbd></dt>
  426. <dd>Focus the search field</dd>
  427. <dt><kbd>↑</kbd></dt>
  428. <dd>Move up in search results</dd>
  429. <dt><kbd>↓</kbd></dt>
  430. <dd>Move down in search results</dd>
  431. <dt><kbd>↹</kbd></dt>
  432. <dd>Switch tab</dd>
  433. <dt><kbd>&#9166;</kbd></dt>
  434. <dd>Go to active search result</dd>
  435. <dt><kbd>+</kbd></dt>
  436. <dd>Expand all sections</dd>
  437. <dt><kbd>-</kbd></dt>
  438. <dd>Collapse all sections</dd>
  439. </dl>
  440. </div>
  441. <div class="infos">
  442. <h2>Search Tricks</h2>
  443. <p>
  444. Prefix searches with a type followed by a colon (e.g.
  445. <code>fn:</code>) to restrict the search to a given type.
  446. </p>
  447. <p>
  448. Accepted types are: <code>fn</code>, <code>mod</code>,
  449. <code>struct</code>, <code>enum</code>,
  450. <code>trait</code>, <code>type</code>, <code>macro</code>,
  451. and <code>const</code>.
  452. </p>
  453. <p>
  454. Search functions by type signature (e.g.
  455. <code>vec -> usize</code> or <code>* -> vec</code>)
  456. </p>
  457. </div>
  458. </div>
  459. </aside>
  460. <script>
  461. window.rootPath = "../";
  462. window.currentCrate = "rand";
  463. </script>
  464. <script src="../main.js"></script>
  465. <script defer src="../search-index.js"></script>
  466. </body>
  467. </html>