From 2eee7043988e4289f16b5deca9f3b36e2516362b Mon Sep 17 00:00:00 2001 From: Tianyi Shi Date: Sat, 18 Apr 2020 23:27:52 +0800 Subject: [PATCH] update rand-dist (#581) * update rand-dist * updates rand but still fails * Update rand-dist.md Co-authored-by: Andrew Gauger --- Cargo.toml | 3 ++- src/algorithms.md | 2 +- src/algorithms/randomness/rand-dist.md | 34 ++++++++++++++------------ src/links.md | 2 ++ 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 21d25de..aacb2f7 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,8 @@ num_cpus = "1.8" percent-encoding = "2.1" petgraph = "0.4" postgres = "0.15" -rand = "0.6" +rand = "0.7.3" +rand_distr = "0.2.2" rayon = "1.0" regex = "1.0" reqwest = "0.9" diff --git a/src/algorithms.md b/src/algorithms.md index cc5e0e0..bd373d9 100644 --- a/src/algorithms.md +++ b/src/algorithms.md @@ -4,7 +4,7 @@ |--------|--------|------------| | [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 given distribution][ex-rand-dist] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | +| [Generate random numbers with given distribution][ex-rand-dist] | [![rand-badge]][rand] [![rand_distr-badge]][rand_distr] | [![cat-science-badge]][cat-science] | | [Generate random values of a custom type][ex-rand-custom] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | [Create random passwords from a set of alphanumeric characters][ex-rand-passwd] | [![rand-badge]][rand] | [![cat-os-badge]][cat-os] | | [Create random passwords from a set of user-defined characters][ex-rand-choose] | [![rand-badge]][rand] | [![cat-os-badge]][cat-os] | diff --git a/src/algorithms/randomness/rand-dist.md b/src/algorithms/randomness/rand-dist.md index 9c3ae9a..be311a1 100644 --- a/src/algorithms/randomness/rand-dist.md +++ b/src/algorithms/randomness/rand-dist.md @@ -1,32 +1,36 @@ ## Generate random numbers with given distribution -[![rand-badge]][rand] [![cat-science-badge]][cat-science] +[![rand_distr-badge]][rand_distr] [![cat-science-badge]][cat-science] -By default, random numbers have [uniform distribution]. -To generate numbers with other distributions you instantiate a -distribution, then sample from that distribution using +By default, random numbers in the `rand` crate have +[uniform distribution]. The [`rand_distr`] crate provides +other kinds of distrubutions. To use them, you instantiate +a distribution, then sample from that distribution using [`Distribution::sample`] with help of a random-number generator [`rand::Rng`]. -The [distributions available are documented here][rand-distributions]. An example using the -[`Normal`] distribution is shown below. +The [distributions available are documented here][rand-distributions]. +An example using the [`Normal`] distribution is shown below. -``` +```rust,no_run +extern crate rand_distr; extern crate rand; -use rand::distributions::{Normal, Distribution}; +use rand_distr::{Distribution, Normal, NormalError}; -fn main() { - let mut rng = rand::thread_rng(); - let normal = Normal::new(2.0, 3.0); - let v = normal.sample(&mut rng); - println!("{} is from a N(2, 9) distribution", v) +fn main() -> Result<(), NormalError> { + let mut rng = rand::thread_rng(); + let normal = Normal::new(2.0, 3.0)?; + let v = normal.sample(&mut rng); + println!("{} is from a N(2, 9) distribution", v); + Ok(()) } ``` [`Distribution::sample`]: https://docs.rs/rand/*/rand/distributions/trait.Distribution.html#tymethod.sample -[`Normal`]: https://docs.rs/rand/*/rand/distributions/normal/struct.Normal.html +[`Normal`]: https://docs.rs/rand_distr/*/rand_distr/struct.Normal.html [`rand::Rng`]: https://docs.rs/rand/*/rand/trait.Rng.html -[rand-distributions]: https://docs.rs/rand/*/rand/distributions/index.html +[`rand_distr`]: https://docs.rs/rand_distr/*/rand_distr/index.html +[rand-distributions]: https://docs.rs/rand_distr/*/rand_distr/index.html [uniform distribution]: https://en.wikipedia.org/wiki/Uniform_distribution_(continuous) diff --git a/src/links.md b/src/links.md index d3867e8..382a3cf 100644 --- a/src/links.md +++ b/src/links.md @@ -104,6 +104,8 @@ Keep lines sorted. [postgres]: https://docs.rs/postgres/0.15.2/postgres/ [rand-badge]: https://badge-cache.kominick.com/crates/v/rand.svg?label=rand [rand]: https://docs.rs/rand/ +[rand_distr-badge]: https://badge-cache.kominick.com/crates/v/rand.svg?label=rand_distr +[rand_distr]: https://docs.rs/rand_distr/ [rayon-badge]: https://badge-cache.kominick.com/crates/v/rayon.svg?label=rayon [rayon]: https://docs.rs/rayon/ [regex-badge]: https://badge-cache.kominick.com/crates/v/regex.svg?label=regex