25 lines
8.9 KiB
HTML
25 lines
8.9 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="Utilities for random number generation"><title>rand - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="rand" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="icon" href="https://www.rust-lang.org/favicon.ico"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Crate rand</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../rand/index.html"><img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></a><h2><a href="../rand/index.html">rand</a><span class="version">0.9.2</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#quick-start" title="Quick Start">Quick Start</a></li><li><a href="#the-book" title="The Book">The Book</a></li></ul><h3><a href="#reexports">Crate Items</a></h3><ul class="block"><li><a href="#reexports" title="Re-exports">Re-exports</a></li><li><a href="#modules" title="Modules">Modules</a></li><li><a href="#traits" title="Traits">Traits</a></li><li><a href="#functions" title="Functions">Functions</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>rand</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/rand/lib.rs.html#10-360">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Utilities for random number generation</p>
|
|
<p>Rand provides utilities to generate random numbers, to convert them to
|
|
useful types and distributions, and some randomness-related algorithms.</p>
|
|
<h2 id="quick-start"><a class="doc-anchor" href="#quick-start">§</a>Quick Start</h2>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// The prelude import enables methods we use below, specifically
|
|
// Rng::random, Rng::sample, SliceRandom::shuffle and IndexedRandom::choose.
|
|
</span><span class="kw">use </span>rand::prelude::<span class="kw-2">*</span>;
|
|
|
|
<span class="comment">// Get an RNG:
|
|
</span><span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
|
|
|
|
<span class="comment">// Try printing a random unicode code point (probably a bad idea)!
|
|
</span><span class="macro">println!</span>(<span class="string">"char: '{}'"</span>, rng.random::<char>());
|
|
<span class="comment">// Try printing a random alphanumeric value instead!
|
|
</span><span class="macro">println!</span>(<span class="string">"alpha: '{}'"</span>, rng.sample(rand::distr::Alphanumeric) <span class="kw">as </span>char);
|
|
|
|
<span class="comment">// Generate and shuffle a sequence:
|
|
</span><span class="kw">let </span><span class="kw-2">mut </span>nums: Vec<i32> = (<span class="number">1</span>..<span class="number">100</span>).collect();
|
|
nums.shuffle(<span class="kw-2">&mut </span>rng);
|
|
<span class="comment">// And take a random pick (yes, we didn't need to shuffle first!):
|
|
</span><span class="kw">let _ </span>= nums.choose(<span class="kw-2">&mut </span>rng);</code></pre></div><h2 id="the-book"><a class="doc-anchor" href="#the-book">§</a>The Book</h2>
|
|
<p>For the user guide and further documentation, please read
|
|
<a href="https://rust-random.github.io/book">The Rust Rand Book</a>.</p>
|
|
</div></details><h2 id="reexports" class="section-header">Re-exports<a href="#reexports" class="anchor">§</a></h2><dl class="item-table reexports"><dt id="reexport.rand_core"><code>pub use <a class="mod" href="../rand_core/index.html" title="mod rand_core">rand_core</a>;</code></dt></dl><h2 id="modules" class="section-header">Modules<a href="#modules" class="anchor">§</a></h2><dl class="item-table"><dt><a class="mod" href="distr/index.html" title="mod rand::distr">distr</a></dt><dd>Generating random samples from probability distributions</dd><dt><a class="mod" href="prelude/index.html" title="mod rand::prelude">prelude</a></dt><dd>Convenience re-export of common members</dd><dt><a class="mod" href="rngs/index.html" title="mod rand::rngs">rngs</a></dt><dd>Random number generators and adapters</dd><dt><a class="mod" href="seq/index.html" title="mod rand::seq">seq</a></dt><dd>Sequence-related functionality</dd></dl><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><dl class="item-table"><dt><a class="trait" href="trait.CryptoRng.html" title="trait rand::CryptoRng">Crypto<wbr>Rng</a></dt><dd>A marker trait over <a href="trait.RngCore.html" title="trait rand::RngCore"><code>RngCore</code></a> for securely unpredictable RNGs</dd><dt><a class="trait" href="trait.Fill.html" title="trait rand::Fill">Fill</a></dt><dd>Types which may be filled with random data</dd><dt><a class="trait" href="trait.Rng.html" title="trait rand::Rng">Rng</a></dt><dd>User-level interface for RNGs</dd><dt><a class="trait" href="trait.RngCore.html" title="trait rand::RngCore">RngCore</a></dt><dd>Implementation-level interface for RNGs</dd><dt><a class="trait" href="trait.SeedableRng.html" title="trait rand::SeedableRng">Seedable<wbr>Rng</a></dt><dd>A random number generator that can be explicitly seeded.</dd><dt><a class="trait" href="trait.TryCryptoRng.html" title="trait rand::TryCryptoRng">TryCrypto<wbr>Rng</a></dt><dd>A marker trait over <a href="trait.TryRngCore.html" title="trait rand::TryRngCore"><code>TryRngCore</code></a> for securely unpredictable RNGs</dd><dt><a class="trait" href="trait.TryRngCore.html" title="trait rand::TryRngCore">TryRng<wbr>Core</a></dt><dd>A potentially fallible variant of <a href="trait.RngCore.html" title="trait rand::RngCore"><code>RngCore</code></a></dd></dl><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><dl class="item-table"><dt><a class="fn" href="fn.fill.html" title="fn rand::fill">fill</a></dt><dd>Fill any type implementing <a href="trait.Fill.html" title="trait rand::Fill"><code>Fill</code></a> with random data</dd><dt><a class="fn" href="fn.random.html" title="fn rand::random">random</a></dt><dd>Generate a random value using the thread-local random number generator.</dd><dt><a class="fn" href="fn.random_bool.html" title="fn rand::random_bool">random_<wbr>bool</a></dt><dd>Return a bool with a probability <code>p</code> of being true.</dd><dt><a class="fn" href="fn.random_iter.html" title="fn rand::random_iter">random_<wbr>iter</a></dt><dd>Return an iterator over <a href="fn.random.html" title="fn rand::random"><code>random()</code></a> variates</dd><dt><a class="fn" href="fn.random_range.html" title="fn rand::random_range">random_<wbr>range</a></dt><dd>Generate a random value in the given range using the thread-local random number generator.</dd><dt><a class="fn" href="fn.random_ratio.html" title="fn rand::random_ratio">random_<wbr>ratio</a></dt><dd>Return a bool with a probability of <code>numerator/denominator</code> of being
|
|
true.</dd><dt><a class="fn" href="fn.rng.html" title="fn rand::rng">rng</a></dt><dd>Access a fast, pre-initialized generator</dd><dt><a class="fn" href="fn.thread_rng.html" title="fn rand::thread_rng">thread_<wbr>rng</a><wbr><span class="stab deprecated" title="">Deprecated</span></dt><dd>Access the thread-local generator</dd></dl></section></div></main></body></html> |