diff --git a/src/basics.md b/src/basics.md index fadd542..0f4751f 100644 --- a/src/basics.md +++ b/src/basics.md @@ -4,9 +4,9 @@ |--------|--------|------------| | [Read lines of strings from a file][ex-std-read-lines] | [![std-badge]][std] | [![cat-filesystem-badge]][cat-filesystem] | | [Read and write integers in little-endian byte order][ex-byteorder-le] | [![byteorder-badge]][byteorder] | [![cat-encoding-badge]][cat-encoding] | -| [Generate random numbers for simple types][ex-rand-simple] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | +| [Generate random numbers][ex-rand] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | [Generate random numbers within a range][ex-rand-range] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | -| [Generate random numbers with normal distribution][ex-rand-dist] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | +| [Generate random distributions][ex-rand-dist] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | [Generate random values of a custom type][ex-rand-custom] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | [Run an external command and process stdout][ex-parse-subprocess-output] | [![regex-badge]][regex] | [![cat-os-badge]][cat-os] [![cat-text-processing-badge]][cat-text-processing] | | [Filter a log file by matching multiple regular expressions][ex-regex-filter-log] | [![regex-badge]][regex] | [![cat-text-processing-badge]][cat-text-processing] @@ -115,11 +115,11 @@ fn decode(mut bytes: &[u8]) -> Result { # quick_main!(run); ``` -[ex-rand-simple]: #ex-rand-simple - -## Generate random numbers for simple types +[ex-rand]: #ex-rand + +## Generate random numbers -The `rand` crate provides a convenient source of psuedo-random numbers. +The [`rand`] crate provides a convenient source of psuedo-random numbers. [![rand-badge]][rand] [![cat-science-badge]][cat-science] @@ -132,26 +132,16 @@ fn main() { let mut rng = rand::thread_rng(); // Integers are uniformly distributed over the type's whole range: - let n1: u32 = rng.gen(); + let n1: u8 = rng.gen(); let n2: u16 = rng.gen(); - let n3: u8 = rng.gen(); - println!("Random u32, u16, u8: {}, {}, {}", n1, n2, n3); + println!("Random u8: {}; random u16: {}", n1, n2); println!("Random i32: {}", rng.gen::()); // Floating point numbers are uniformly distributed in the half-open range [0, 1) println!("Random float: {}", rng.gen::()); - println!("Random floats: {:?}", (0..5).map(|_| rng.gen()).collect::>()); } ``` -Example output: -``` -Random u32, u16, u8: 1224630198, 31187, 43 -Random i32: -515338046 -Random float: 0.7258180295601702 -Random floats: [0.6633928, 0.4400041, 0.49633145, 0.02997303, 0.41612816] -``` - [ex-rand-range]: #ex-rand-range ## Generate random numbers within a range @@ -172,7 +162,7 @@ fn main() { } ``` -Alternatively, one can use the [`Range`](https://doc.rust-lang.org/rand/rand/distributions/range/struct.Range.html) distribution. +Alternatively, one can use the [`Range`] distribution (otherwise known as uniform). This has the same effect, but may be faster when repeatedly generating numbers in the same range. @@ -198,12 +188,17 @@ fn main() { [ex-rand-dist]: #ex-rand-dist -## Generate random numbers with normal distribution +## Random number distributions [![rand-badge]][rand] [![cat-science-badge]][cat-science] -Creates a [`Normal`] distribution with mean `3` and standard deviation `5` -and generates a random value with [`IndependentSample::ind_sample`]. +You've already seen how to create numbers with uniform distribution [above][ex-rand-range] +(using [`Range`]). Other distributions are used in the same way: create a distribution, then +sample from that distribution (using [`IndependentSample::ind_sample`]) with the help of +a random-number generator (`rng`). + +The [distributions available are documented here][rand-distributions]. An example using the +[`Normal`] distribution is shown below. ```rust extern crate rand; @@ -211,10 +206,12 @@ extern crate rand; use rand::distributions::{Normal, IndependentSample}; fn main() { - let normal = Normal::new(3.0, 5.0); let mut rng = rand::thread_rng(); + + // mean 2, standard deviation 3: + let normal = Normal::new(2.0, 3.0); let v = normal.ind_sample(&mut rng); - println!("{} is from a N(3, 25) distribution", v) + println!("{} is from a N(2, 9) distribution", v) } ``` @@ -800,9 +797,13 @@ fn run() -> Result<()> { [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html [`Normal`]: https://doc.rust-lang.org/rand/rand/distributions/normal/struct.Normal.html [`IndependentSample::ind_sample`]: https://doc.rust-lang.org/rand/rand/distributions/trait.IndependentSample.html#tymethod.ind_sample +[`rand`]: https://doc.rust-lang.org/rand [`Rng::gen_range`]: https://doc.rust-lang.org/rand/rand/trait.Rng.html#method.gen_range [`Regex::captures_iter`]: https://doc.rust-lang.org/regex/regex/struct.Regex.html#method.captures_iter [`Regex::replace_all`]: https://docs.rs/regex/0.2.2/regex/struct.Regex.html#method.replace_all +[`rand::Rand`]: https://doc.rust-lang.org/rand/rand/trait.Rand.html +[`Range`]: https://doc.rust-lang.org/rand/rand/distributions/range/struct.Range.html +[rand-distributions]: https://doc.rust-lang.org/rand/rand/distributions/index.html [`Regex`]: https://doc.rust-lang.org/regex/regex/struct.Regex.html [`rand::Rand`]: https://doc.rust-lang.org/rand/rand/trait.Rand.html [`regex::RegexSetBuilder`]: https://doc.rust-lang.org/regex/regex/struct.RegexSetBuilder.html diff --git a/src/intro.md b/src/intro.md index 1438f40..988899c 100644 --- a/src/intro.md +++ b/src/intro.md @@ -22,9 +22,9 @@ community. It needs and welcomes help. For details see |--------|--------|------------| | [Read lines of strings from a file][ex-std-read-lines] | [![std-badge]][std] | [![cat-filesystem-badge]][cat-filesystem] | | [Read and write integers in little-endian byte order][ex-byteorder-le] | [![byteorder-badge]][byteorder] | [![cat-encoding-badge]][cat-encoding] | -| [Generate random floating point numbers][ex-rand-float] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | +| [Generate random numbers][ex-rand] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | [Generate random numbers within a range][ex-rand-range] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | -| [Generate random numbers with normal distribution][ex-rand-dist] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | +| [Generate random number distributions][ex-rand-dist] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | [Generate random values of a custom type][ex-rand-custom] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | [Run an external command and process stdout][ex-parse-subprocess-output] | [![regex-badge]][regex] | [![cat-os-badge]][cat-os] [![cat-text-processing-badge]][cat-text-processing] | | [Filter a log file by matching multiple regular expressions][ex-regex-filter-log] | [![regex-badge]][regex] | [![cat-text-processing-badge]][cat-text-processing] @@ -264,7 +264,7 @@ Keep lines sorted. [ex-phone]: basics.html#ex-phone [ex-rand-custom]: basics.html#ex-rand-custom [ex-rand-dist]: basics.html#ex-rand-dist -[ex-rand-float]: basics.html#ex-rand-float +[ex-rand]: basics.html#ex-rand [ex-rand-range]: basics.html#ex-rand-range [ex-random-port-tcp]: net.html#ex-random-port-tcp [ex-rayon-iter-mut]: concurrency.html#ex-rayon-iter-mut