From 369d11587339ce74f8ebc76f2607fe55545eaf7d Mon Sep 17 00:00:00 2001 From: garhve Date: Tue, 20 Dec 2022 11:04:25 +0800 Subject: Build small project following the book --- .../doc/rand/distributions/trait.Distribution.html | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/rand/distributions/trait.Distribution.html (limited to 'rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/rand/distributions/trait.Distribution.html') diff --git a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/rand/distributions/trait.Distribution.html b/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/rand/distributions/trait.Distribution.html new file mode 100644 index 0000000..32c5f9d --- /dev/null +++ b/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/rand/distributions/trait.Distribution.html @@ -0,0 +1,65 @@ +Distribution in rand::distributions - Rust
pub trait Distribution<T> {
+    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T;
+
+    fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>Notable traits for DistIter<D, R, T>impl<D, R, T> Iterator for DistIter<D, R, T>where
    D: Distribution<T>,
    R: Rng,
type Item = T;

    where
        R: Rng,
        Self: Sized
, + { ... } + fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S>
    where
        F: Fn(T) -> S,
        Self: Sized
, + { ... } +}
Expand description

Types (distributions) that can be used to create a random instance of T.

+

It is possible to sample from a distribution through both the +Distribution and Rng traits, via distr.sample(&mut rng) and +rng.sample(distr). They also both offer the sample_iter method, which +produces an iterator that samples from the distribution.

+

All implementations are expected to be immutable; this has the significant +advantage of not needing to consider thread safety, and for most +distributions efficient state-less sampling algorithms are available.

+

Implementations are typically expected to be portable with reproducible +results when used with a PRNG with fixed seed; see the +portability chapter +of The Rust Rand Book. In some cases this does not apply, e.g. the usize +type requires different sampling on 32-bit and 64-bit machines.

+

Required Methods

Generate a random value of T, using rng as the source of randomness.

+

Provided Methods

Create an iterator that generates random values of T, using rng as +the source of randomness.

+

Note that this function takes self by value. This works since +Distribution<T> is impl’d for &D where D: Distribution<T>, +however borrowing is not automatic hence distr.sample_iter(...) may +need to be replaced with (&distr).sample_iter(...) to borrow or +(&*distr).sample_iter(...) to reborrow an existing reference.

+
Example
+
use rand::thread_rng;
+use rand::distributions::{Distribution, Alphanumeric, Uniform, Standard};
+
+let mut rng = thread_rng();
+
+// Vec of 16 x f32:
+let v: Vec<f32> = Standard.sample_iter(&mut rng).take(16).collect();
+
+// String:
+let s: String = Alphanumeric
+    .sample_iter(&mut rng)
+    .take(7)
+    .map(char::from)
+    .collect();
+
+// Dice-rolling:
+let die_range = Uniform::new_inclusive(1, 6);
+let mut roll_die = die_range.sample_iter(&mut rng);
+while roll_die.next().unwrap() != 6 {
+    println!("Not a 6; rolling again!");
+}
+

Create a distribution of values of ‘S’ by mapping the output of Self +through the closure F

+
Example
+
use rand::thread_rng;
+use rand::distributions::{Distribution, Uniform};
+
+let mut rng = thread_rng();
+
+let die = Uniform::new_inclusive(1, 6);
+let even_number = die.map(|num| num % 2 == 0);
+while !even_number.sample(&mut rng) {
+    println!("Still odd; rolling again!");
+}
+

Implementations on Foreign Types

Implementors

\ No newline at end of file -- cgit v1.2.3-70-g09d2