forked from rust-lang-nursery/rust-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update rand-dist (rust-lang-nursery#581)
* update rand-dist * updates rand but still fails * Update rand-dist.md Co-authored-by: Andrew Gauger <[email protected]>
- Loading branch information
1 parent
74e9382
commit 2eee704
Showing
4 changed files
with
24 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters