mpvrc/fastrand/index.html

66 lines
No EOL
15 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="A simple and fast random number generator."><title>fastrand - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.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" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="fastrand" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.1 (e71f9a9a9 2025-01-27)" data-channel="1.84.1" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="icon" href="https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"></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]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="../fastrand/index.html"><img src="https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../fastrand/index.html"><img src="https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png" alt="logo"></a><h2><a href="../fastrand/index.html">fastrand</a><span class="version">2.3.0</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="#examples" title="Examples">Examples</a></li><li><a href="#features" title="Features">Features</a></li><li><a href="#webassembly-notes" title="WebAssembly Notes">WebAssembly Notes</a></li></ul><h3><a href="#structs">Crate Items</a></h3><ul class="block"><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#functions" title="Functions">Functions</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>fastrand</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/fastrand/lib.rs.html#1-689">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A simple and fast random number generator.</p>
<p>The implementation uses <a href="https://github.com/wangyi-fudan/wyhash">Wyrand</a>, a simple and fast
generator but <strong>not</strong> cryptographically secure.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<p>Flip a coin:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">if </span>fastrand::bool() {
<span class="macro">println!</span>(<span class="string">"heads"</span>);
} <span class="kw">else </span>{
<span class="macro">println!</span>(<span class="string">"tails"</span>);
}</code></pre></div>
<p>Generate a random <code>i32</code>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>num = fastrand::i32(..);</code></pre></div>
<p>Choose a random element in an array:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>v = <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];
<span class="kw">let </span>i = fastrand::usize(..v.len());
<span class="kw">let </span>elem = v[i];</code></pre></div>
<p>Sample values from an array with <code>O(n)</code> complexity (<code>n</code> is the length of array):</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code>fastrand::choose_multiple([<span class="number">1</span>, <span class="number">4</span>, <span class="number">5</span>], <span class="number">2</span>);
fastrand::choose_multiple(<span class="number">0</span>..<span class="number">20</span>, <span class="number">12</span>);</code></pre></div>
<p>Shuffle an array:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>v = <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];
fastrand::shuffle(<span class="kw-2">&amp;mut </span>v);</code></pre></div>
<p>Generate a random <a href="https://doc.rust-lang.org/1.84.1/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec"><code>Vec</code></a> or [<code>String</code>]:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::iter::repeat_with;
<span class="kw">let </span>v: Vec&lt;i32&gt; = repeat_with(|| fastrand::i32(..)).take(<span class="number">10</span>).collect();
<span class="kw">let </span>s: String = repeat_with(fastrand::alphanumeric).take(<span class="number">10</span>).collect();</code></pre></div>
<p>To get reproducible results on every run, initialize the generator with a seed:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// Pick an arbitrary number as seed.
</span>fastrand::seed(<span class="number">7</span>);
<span class="comment">// Now this prints the same number on every run:
</span><span class="macro">println!</span>(<span class="string">"{}"</span>, fastrand::u32(..));</code></pre></div>
<p>To be more efficient, create a new <a href="struct.Rng.html" title="struct fastrand::Rng"><code>Rng</code></a> instance instead of using the thread-local
generator:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::iter::repeat_with;
<span class="kw">let </span><span class="kw-2">mut </span>rng = fastrand::Rng::new();
<span class="kw">let </span><span class="kw-2">mut </span>bytes: Vec&lt;u8&gt; = repeat_with(|| rng.u8(..)).take(<span class="number">10_000</span>).collect();</code></pre></div>
<p>This crate aims to expose a core set of useful randomness primitives. For more niche algorithms,
consider using the <a href="https://crates.io/crates/fastrand-contrib"><code>fastrand-contrib</code></a> crate alongside this one.</p>
<h2 id="features"><a class="doc-anchor" href="#features">§</a>Features</h2>
<ul>
<li><code>std</code> (enabled by default): Enables the <code>std</code> library. This is required for the global
generator and global entropy. Without this feature, <a href="struct.Rng.html" title="struct fastrand::Rng"><code>Rng</code></a> can only be instantiated using
the <a href="struct.Rng.html#method.with_seed" title="associated function fastrand::Rng::with_seed"><code>with_seed</code></a> method.</li>
<li><code>js</code>: Assumes that WebAssembly targets are being run in a JavaScript environment. See the
<a href="#webassembly-notes">WebAssembly Notes</a> section for more information.</li>
</ul>
<h2 id="webassembly-notes"><a class="doc-anchor" href="#webassembly-notes">§</a>WebAssembly Notes</h2>
<p>For non-WASI WASM targets, there is additional sublety to consider when utilizing the global RNG.
By default, <code>std</code> targets will use entropy sources in the standard library to seed the global RNG.
However, these sources are not available by default on WASM targets outside of WASI.</p>
<p>If the <code>js</code> feature is enabled, this crate will assume that it is running in a JavaScript
environment. At this point, the <a href="https://crates.io/crates/getrandom"><code>getrandom</code></a> crate will be used in order to access the available
entropy sources and seed the global RNG. If the <code>js</code> feature is not enabled, the global RNG will
use a predefined seed.</p>
</div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.Rng.html" title="struct fastrand::Rng">Rng</a></div><div class="desc docblock-short">A random number generator.</div></li></ul><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="fn" href="fn.alphabetic.html" title="fn fastrand::alphabetic">alphabetic</a></div><div class="desc docblock-short">Generates a random <code>char</code> in ranges a-z and A-Z.</div></li><li><div class="item-name"><a class="fn" href="fn.alphanumeric.html" title="fn fastrand::alphanumeric">alphanumeric</a></div><div class="desc docblock-short">Generates a random <code>char</code> in ranges a-z, A-Z and 0-9.</div></li><li><div class="item-name"><a class="fn" href="fn.bool.html" title="fn fastrand::bool">bool</a></div><div class="desc docblock-short">Generates a random <code>bool</code>.</div></li><li><div class="item-name"><a class="fn" href="fn.char.html" title="fn fastrand::char">char</a></div><div class="desc docblock-short">Generates a random <code>char</code> in the given range.</div></li><li><div class="item-name"><a class="fn" href="fn.choice.html" title="fn fastrand::choice">choice</a></div><div class="desc docblock-short">Choose an item from an iterator at random.</div></li><li><div class="item-name"><a class="fn" href="fn.choose_multiple.html" title="fn fastrand::choose_multiple">choose_<wbr>multiple</a></div><div class="desc docblock-short">Collects <code>amount</code> values at random from the iterable into a vector.</div></li><li><div class="item-name"><a class="fn" href="fn.digit.html" title="fn fastrand::digit">digit</a></div><div class="desc docblock-short">Generates a random digit in the given <code>base</code>.</div></li><li><div class="item-name"><a class="fn" href="fn.f32.html" title="fn fastrand::f32">f32</a></div><div class="desc docblock-short">Generates a random <code>f32</code> in range <code>0..1</code>.</div></li><li><div class="item-name"><a class="fn" href="fn.f64.html" title="fn fastrand::f64">f64</a></div><div class="desc docblock-short">Generates a random <code>f64</code> in range <code>0..1</code>.</div></li><li><div class="item-name"><a class="fn" href="fn.fill.html" title="fn fastrand::fill">fill</a></div><div class="desc docblock-short">Fill a byte slice with random data.</div></li><li><div class="item-name"><a class="fn" href="fn.get_seed.html" title="fn fastrand::get_seed">get_<wbr>seed</a></div><div class="desc docblock-short">Gives back <strong>current</strong> seed that is being held by the thread-local generator.</div></li><li><div class="item-name"><a class="fn" href="fn.i8.html" title="fn fastrand::i8">i8</a></div><div class="desc docblock-short">Generates a random <code>i8</code> in the given range.</div></li><li><div class="item-name"><a class="fn" href="fn.i16.html" title="fn fastrand::i16">i16</a></div><div class="desc docblock-short">Generates a random <code>i16</code> in the given range.</div></li><li><div class="item-name"><a class="fn" href="fn.i32.html" title="fn fastrand::i32">i32</a></div><div class="desc docblock-short">Generates a random <code>i32</code> in the given range.</div></li><li><div class="item-name"><a class="fn" href="fn.i64.html" title="fn fastrand::i64">i64</a></div><div class="desc docblock-short">Generates a random <code>i64</code> in the given range.</div></li><li><div class="item-name"><a class="fn" href="fn.i128.html" title="fn fastrand::i128">i128</a></div><div class="desc docblock-short">Generates a random <code>i128</code> in the given range.</div></li><li><div class="item-name"><a class="fn" href="fn.isize.html" title="fn fastrand::isize">isize</a></div><div class="desc docblock-short">Generates a random <code>isize</code> in the given range.</div></li><li><div class="item-name"><a class="fn" href="fn.lowercase.html" title="fn fastrand::lowercase">lowercase</a></div><div class="desc docblock-short">Generates a random <code>char</code> in range a-z.</div></li><li><div class="item-name"><a class="fn" href="fn.seed.html" title="fn fastrand::seed">seed</a></div><div class="desc docblock-short">Initializes the thread-local generator with the given seed.</div></li><li><div class="item-name"><a class="fn" href="fn.shuffle.html" title="fn fastrand::shuffle">shuffle</a></div><div class="desc docblock-short">Shuffles a slice randomly.</div></li><li><div class="item-name"><a class="fn" href="fn.u8.html" title="fn fastrand::u8">u8</a></div><div class="desc docblock-short">Generates a random <code>u8</code> in the given range.</div></li><li><div class="item-name"><a class="fn" href="fn.u16.html" title="fn fastrand::u16">u16</a></div><div class="desc docblock-short">Generates a random <code>u16</code> in the given range.</div></li><li><div class="item-name"><a class="fn" href="fn.u32.html" title="fn fastrand::u32">u32</a></div><div class="desc docblock-short">Generates a random <code>u32</code> in the given range.</div></li><li><div class="item-name"><a class="fn" href="fn.u64.html" title="fn fastrand::u64">u64</a></div><div class="desc docblock-short">Generates a random <code>u64</code> in the given range.</div></li><li><div class="item-name"><a class="fn" href="fn.u128.html" title="fn fastrand::u128">u128</a></div><div class="desc docblock-short">Generates a random <code>u128</code> in the given range.</div></li><li><div class="item-name"><a class="fn" href="fn.uppercase.html" title="fn fastrand::uppercase">uppercase</a></div><div class="desc docblock-short">Generates a random <code>char</code> in range A-Z.</div></li><li><div class="item-name"><a class="fn" href="fn.usize.html" title="fn fastrand::usize">usize</a></div><div class="desc docblock-short">Generates a random <code>usize</code> in the given range.</div></li></ul></section></div></main></body></html>