mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-21 19:13:07 +00:00
update rand-dist (#581)
* update rand-dist * updates rand but still fails * Update rand-dist.md Co-authored-by: Andrew Gauger <andygauge@gmail.com>
This commit is contained in:
parent
74e9382d18
commit
2eee704398
4 changed files with 24 additions and 17 deletions
|
@ -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"
|
||||
|
|
|
@ -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] |
|
||||
|
|
|
@ -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() {
|
||||
fn main() -> Result<(), NormalError> {
|
||||
let mut rng = rand::thread_rng();
|
||||
let normal = Normal::new(2.0, 3.0);
|
||||
let normal = Normal::new(2.0, 3.0)?;
|
||||
let v = normal.sample(&mut rng);
|
||||
println!("{} is from a N(2, 9) distribution", v)
|
||||
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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue