Skip to content

Commit

Permalink
update rand-dist (rust-lang-nursery#581)
Browse files Browse the repository at this point in the history
* update rand-dist

* updates rand but still fails

* Update rand-dist.md

Co-authored-by: Andrew Gauger <[email protected]>
  • Loading branch information
TianyiShi2001 and AndyGauge authored Apr 18, 2020
1 parent 74e9382 commit 2eee704
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] |
Expand Down
34 changes: 19 additions & 15 deletions src/algorithms/randomness/rand-dist.md
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions src/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2eee704

Please sign in to comment.