Skip to content

Commit f0ba24b

Browse files
committed
deploy: 94c065e
1 parent a4c9ac2 commit f0ba24b

File tree

4 files changed

+74
-96
lines changed

4 files changed

+74
-96
lines changed

algorithms/randomness.html

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -162,38 +162,35 @@ <h1 class="menu-title">Rust Cookbook</h1>
162162
<h1 id="generate-random-values"><a class="header" href="#generate-random-values">Generate Random Values</a></h1>
163163
<h2 id="generate-random-numbers"><a class="header" href="#generate-random-numbers">Generate random numbers</a></h2>
164164
<p><a href="https://docs.rs/rand/"><img src="https://badge-cache.kominick.com/crates/v/rand.svg?label=rand" alt="rand-badge" /></a> <a href="https://crates.io/categories/science"><img src="https://badge-cache.kominick.com/badge/science--x.svg?style=social" alt="cat-science-badge" /></a></p>
165-
<p>Generates random numbers with help of random-number
166-
generator <a href="https://docs.rs/rand/0.9/rand/trait.Rng.html"><code>rand::Rng</code></a> obtained via <a href="https://docs.rs/rand/0.9/rand/trait.Rng.html"><code>rand::rng</code></a>. Each thread has an
167-
initialized generator. Integers are uniformly distributed over the range of the
168-
type, and floating point numbers are uniformly distributed from 0 up to but not
165+
<p>Generates random numbers with help of random-number generator <a href="https://docs.rs/rand/latest/rand/trait.Rng.html"><code>rand::Rng</code></a> obtained via
166+
<a href="https://docs.rs/rand/latest/rand/fn.rng.html"><code>rand::rng()</code></a>. Each thread has an initialized generator. Integers are uniformly distributed over the
167+
range of the type, and floating point numbers are uniformly distributed from 0 up to but not
169168
including 1.</p>
170169
<pre><pre class="playground"><code class="language-rust edition2018">use rand::Rng;
171170

172171
fn main() {
173-
let mut rng = rand::thread_rng();
174-
let random_number: u32 = rng.gen();
175-
println!("Random number: {}", random_number);
172+
let mut rng = rand::rng();
173+
let random_number: u32 = rng.random();
174+
println!("Random number: {random_number}");
176175
}</code></pre></pre>
177176
<h2 id="generate-random-numbers-within-a-range"><a class="header" href="#generate-random-numbers-within-a-range">Generate random numbers within a range</a></h2>
178177
<p><a href="https://docs.rs/rand/"><img src="https://badge-cache.kominick.com/crates/v/rand.svg?label=rand" alt="rand-badge" /></a> <a href="https://crates.io/categories/science"><img src="https://badge-cache.kominick.com/badge/science--x.svg?style=social" alt="cat-science-badge" /></a></p>
179-
<p>Generates a random value within half-open <code>[0, 10)</code> range (not including <code>10</code>) with [<code>Rng::gen_range</code>].</p>
178+
<p>Generates a random value within half-open <code>[0, 10)</code> range (not including <code>10</code>) with <a href="https://docs.rs/rand/latest/rand/trait.Rng.html#method.random_range"><code>Rng::random_range</code></a>.</p>
180179
<pre><pre class="playground"><code class="language-rust edition2018">use rand::Rng;
181180

182181
fn main() {
183182
let mut rng = rand::rng();
184-
println!("Integer: {}", rng.gen_range(0..10));
185-
println!("Float: {}", rng.gen_range(0.0..10.0));
183+
println!("Integer: {}", rng.random_range(0..10));
184+
println!("Float: {}", rng.random_range(0.0..10.0));
186185
}</code></pre></pre>
187-
<p>[<code>Uniform</code>] can obtain values with <a href="https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)">uniform distribution</a>.
188-
This has the same effect, but may be faster when repeatedly generating numbers
189-
in the same range.</p>
190-
<pre><pre class="playground"><code class="language-rust edition2018">use rand::Rng;
191-
use rand_distr::{Distribution, Uniform};
186+
<p><a href="https://docs.rs/rand_distr/latest/rand_distr/struct.Uniform.html"><code>Uniform</code></a> can obtain values with <a href="https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)">uniform distribution</a>. This has the same effect, but may be
187+
faster when repeatedly generating numbers in the same range.</p>
188+
<pre><pre class="playground"><code class="language-rust edition2018">use rand_distr::{Distribution, Uniform};
192189

193190
fn main() {
194191
let mut rng = rand::rng();
195-
let die = Uniform::new_inclusive(1, 6)
196-
.expect("Failed to create uniform distribution: invalid range");
192+
let die =
193+
Uniform::new_inclusive(1, 6).expect("Failed to create uniform distribution: invalid range");
197194

198195
loop {
199196
let throw = die.sample(&amp;mut rng);
@@ -205,23 +202,18 @@ <h2 id="generate-random-numbers-within-a-range"><a class="header" href="#generat
205202
}</code></pre></pre>
206203
<h2 id="generate-random-numbers-with-given-distribution"><a class="header" href="#generate-random-numbers-with-given-distribution">Generate random numbers with given distribution</a></h2>
207204
<p><a href="https://docs.rs/rand_distr/"><img src="https://badge-cache.kominick.com/crates/v/rand_distr.svg?label=rand_distr" alt="rand_distr-badge" /></a> <a href="https://crates.io/categories/science"><img src="https://badge-cache.kominick.com/badge/science--x.svg?style=social" alt="cat-science-badge" /></a></p>
208-
<p>By default, random numbers in the <code>rand</code> crate have
209-
<a href="https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)">uniform distribution</a>. The <a href="https://docs.rs/rand_distr/*/rand_distr/index.html"><code>rand_distr</code></a> crate provides
210-
other kinds of distributions. To use them, you instantiate
211-
a distribution, then sample from that distribution using
212-
<a href="https://docs.rs/rand/0.9/rand/distr/trait.Distribution.html#tymethod.sample"><code>Distribution::sample</code></a> with help of a random-number
213-
generator <a href="https://docs.rs/rand/0.9/rand/trait.Rng.html"><code>rand::Rng</code></a>.</p>
214-
<p>The <a href="https://docs.rs/rand_distr/*/rand_distr/index.html">distributions available are documented here</a>.
215-
An example using the <a href="https://docs.rs/rand_distr/*/rand_distr/struct.Normal.html"><code>Normal</code></a> distribution is shown below.</p>
216-
<pre><pre class="playground"><code class="language-rust edition2018">use rand::Rng;
217-
use rand_distr::{Distribution, LogNormal, Normal};
205+
<p>By default, random numbers in the <code>rand</code> crate have <a href="https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)">uniform distribution</a>. The <a href="https://docs.rs/rand_distr/*/rand_distr/index.html"><code>rand_distr</code></a> crate
206+
provides other kinds of distributions. To use them, you instantiate a distribution, then sample from
207+
that distribution using <a href="https://docs.rs/rand/0.9/rand/distr/trait.Distribution.html#tymethod.sample"><code>Distribution::sample</code></a> with help of a random-number generator
208+
<a href="https://docs.rs/rand/latest/rand/trait.Rng.html"><code>rand::Rng</code></a>.</p>
209+
<p>The <a href="https://docs.rs/rand_distr/*/rand_distr/index.html">distributions available are documented here</a>. An example using the
210+
<a href="https://docs.rs/rand_distr/*/rand_distr/struct.Normal.html"><code>Normal</code></a> distribution is shown below.</p>
211+
<pre><pre class="playground"><code class="language-rust edition2018">use rand_distr::{Distribution, LogNormal, Normal};
218212

219213
fn main() {
220214
let mut rng = rand::rng();
221-
let normal = Normal::new(2.0, 3.0)
222-
.expect("Failed to create normal distribution");
223-
let log_normal = LogNormal::new(1.0, 0.5)
224-
.expect("Failed to create log-normal distribution");
215+
let normal = Normal::new(2.0, 3.0).expect("Failed to create normal distribution");
216+
let log_normal = LogNormal::new(1.0, 0.5).expect("Failed to create log-normal distribution");
225217

226218
let v = normal.sample(&amp;mut rng);
227219
println!("{} is from a N(2, 9) distribution", v);
@@ -230,9 +222,10 @@ <h2 id="generate-random-numbers-with-given-distribution"><a class="header" href=
230222
}</code></pre></pre>
231223
<h2 id="generate-random-values-of-a-custom-type"><a class="header" href="#generate-random-values-of-a-custom-type">Generate random values of a custom type</a></h2>
232224
<p><a href="https://docs.rs/rand/"><img src="https://badge-cache.kominick.com/crates/v/rand.svg?label=rand" alt="rand-badge" /></a> <a href="https://crates.io/categories/science"><img src="https://badge-cache.kominick.com/badge/science--x.svg?style=social" alt="cat-science-badge" /></a></p>
233-
<p>Randomly generates a tuple <code>(i32, bool, f64)</code> and variable of user defined type <code>Point</code>.
234-
Implements the [<code>Distribution</code>] trait on type Point for [<code>Standard</code>] in order to allow random generation.</p>
235-
<pre><pre class="playground"><code class="language-rust edition2018">use rand::Rng;
225+
<p>Randomly generates a tuple <code>(i32, bool, f64)</code> and variable of user defined type <code>Point</code>. Implements
226+
the <a href="https://docs.rs/rand/latest/rand/distr/trait.Distribution.html"><code>Distribution</code></a> trait on type Point for <a href="https://docs.rs/rand/latest/rand/distr/struct.Standard.html"><code>Standard</code></a> in order to allow random generation.</p>
227+
<pre><pre class="playground"><code class="language-rust edition2018">#![allow(dead_code)]
228+
use rand::Rng;
236229

237230
#[derive(Debug)]
238231
struct Point {
@@ -258,29 +251,25 @@ <h2 id="generate-random-values-of-a-custom-type"><a class="header" href="#genera
258251
}</code></pre></pre>
259252
<h2 id="create-random-passwords-from-a-set-of-alphanumeric-characters"><a class="header" href="#create-random-passwords-from-a-set-of-alphanumeric-characters">Create random passwords from a set of alphanumeric characters</a></h2>
260253
<p><a href="https://docs.rs/rand/"><img src="https://badge-cache.kominick.com/crates/v/rand.svg?label=rand" alt="rand-badge" /></a> <a href="https://crates.io/categories/os"><img src="https://badge-cache.kominick.com/badge/OS--x.svg?style=social" alt="cat-os-badge" /></a></p>
261-
<p>Randomly generates a string of given length ASCII characters in the range <code>A-Z, a-z, 0-9</code>, with <a href="https://docs.rs/rand/0.9/rand/distr/struct.Alphanumeric.html"><code>Alphanumeric</code></a> sample.</p>
254+
<p>Randomly generates a string of given length ASCII characters in the range <code>A-Z, a-z, 0-9</code>, with
255+
<a href="https://docs.rs/rand/0.9/rand/distr/struct.Alphanumeric.html"><code>Alphanumeric</code></a> sample.</p>
262256
<pre><pre class="playground"><code class="language-rust edition2018">use rand::Rng;
257+
use rand_distr::Alphanumeric;
263258

264259
fn main() {
265-
const CHARSET: &amp;[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
266-
abcdefghijklmnopqrstuvwxyz\
267-
0123456789";
268260
const PASSWORD_LEN: usize = 30;
269261
let mut rng = rand::rng();
270262

271263
let password: String = (0..PASSWORD_LEN)
272-
.map(|_| {
273-
let idx = rng.random_range(0..CHARSET.len());
274-
CHARSET[idx] as char
275-
})
264+
.map(|_| rng.sample(Alphanumeric) as char)
276265
.collect();
277266

278-
println!("{}", password);
267+
println!("{password}");
279268
}</code></pre></pre>
280269
<h2 id="create-random-passwords-from-a-set-of-user-defined-characters"><a class="header" href="#create-random-passwords-from-a-set-of-user-defined-characters">Create random passwords from a set of user-defined characters</a></h2>
281270
<p><a href="https://docs.rs/rand/"><img src="https://badge-cache.kominick.com/crates/v/rand.svg?label=rand" alt="rand-badge" /></a> <a href="https://crates.io/categories/os"><img src="https://badge-cache.kominick.com/badge/OS--x.svg?style=social" alt="cat-os-badge" /></a></p>
282-
<p>Randomly generates a string of given length ASCII characters with custom
283-
user-defined bytestring, with <a href="https://docs.rs/rand/0.9/rand/trait.Rng.html#method.gen_range"><code>gen_range</code></a>.</p>
271+
<p>Randomly generates a string of given length ASCII characters with custom user-defined bytestring,
272+
with <a href="https://docs.rs/rand/latest/rand/trait.Rng.html#method.random_range"><code>random_range</code></a>.</p>
284273
<pre><pre class="playground"><code class="language-rust edition2018">use rand::Rng;
285274

286275
const CHARSET: &amp;[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
@@ -293,12 +282,12 @@ <h2 id="create-random-passwords-from-a-set-of-user-defined-characters"><a class=
293282

294283
let password: String = (0..PASSWORD_LEN)
295284
.map(|_| {
296-
let idx = rng.gen_range(0..CHARSET.len());
285+
let idx = rng.random_range(0..CHARSET.len());
297286
char::from(CHARSET[idx])
298287
})
299288
.collect();
300289

301-
println!("{:?}", password);
290+
println!("{password}");
302291
}</code></pre></pre>
303292
<!--
304293
Links, in a few categories. Follow the existing structure.

0 commit comments

Comments
 (0)