mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-22 11:33:09 +00:00
Basics: update random number distributions; tweaks to other prior rand sections
This commit is contained in:
parent
b3b5154521
commit
c399dc4b7d
2 changed files with 28 additions and 27 deletions
|
@ -4,9 +4,9 @@
|
||||||
|--------|--------|------------|
|
|--------|--------|------------|
|
||||||
| [Read lines of strings from a file][ex-std-read-lines] | [![std-badge]][std] | [![cat-filesystem-badge]][cat-filesystem] |
|
| [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] |
|
| [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 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] |
|
| [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] |
|
| [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]
|
| [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<Payload> {
|
||||||
# quick_main!(run);
|
# quick_main!(run);
|
||||||
```
|
```
|
||||||
|
|
||||||
[ex-rand-simple]: #ex-rand-simple
|
[ex-rand]: #ex-rand
|
||||||
<a name="ex-rand-simple"></a>
|
<a name="ex-rand"></a>
|
||||||
## Generate random numbers for simple types
|
## 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]
|
[![rand-badge]][rand] [![cat-science-badge]][cat-science]
|
||||||
|
|
||||||
|
@ -132,26 +132,16 @@ fn main() {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
// Integers are uniformly distributed over the type's whole range:
|
// 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 n2: u16 = rng.gen();
|
||||||
let n3: u8 = rng.gen();
|
println!("Random u8: {}; random u16: {}", n1, n2);
|
||||||
println!("Random u32, u16, u8: {}, {}, {}", n1, n2, n3);
|
|
||||||
println!("Random i32: {}", rng.gen::<i32>());
|
println!("Random i32: {}", rng.gen::<i32>());
|
||||||
|
|
||||||
// Floating point numbers are uniformly distributed in the half-open range [0, 1)
|
// Floating point numbers are uniformly distributed in the half-open range [0, 1)
|
||||||
println!("Random float: {}", rng.gen::<f64>());
|
println!("Random float: {}", rng.gen::<f64>());
|
||||||
println!("Random floats: {:?}", (0..5).map(|_| rng.gen()).collect::<Vec<f32>>());
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
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
|
[ex-rand-range]: #ex-rand-range
|
||||||
<a name="ex-rand-range"></a>
|
<a name="ex-rand-range"></a>
|
||||||
## Generate random numbers within a 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
|
This has the same effect, but may be faster when repeatedly generating numbers
|
||||||
in the same range.
|
in the same range.
|
||||||
|
|
||||||
|
@ -198,12 +188,17 @@ fn main() {
|
||||||
|
|
||||||
[ex-rand-dist]: #ex-rand-dist
|
[ex-rand-dist]: #ex-rand-dist
|
||||||
<a name="ex-rand-dist"></a>
|
<a name="ex-rand-dist"></a>
|
||||||
## Generate random numbers with normal distribution
|
## Random number distributions
|
||||||
|
|
||||||
[![rand-badge]][rand] [![cat-science-badge]][cat-science]
|
[![rand-badge]][rand] [![cat-science-badge]][cat-science]
|
||||||
|
|
||||||
Creates a [`Normal`] distribution with mean `3` and standard deviation `5`
|
You've already seen how to create numbers with uniform distribution [above][ex-rand-range]
|
||||||
and generates a random value with [`IndependentSample::ind_sample`].
|
(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
|
```rust
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
@ -211,10 +206,12 @@ extern crate rand;
|
||||||
use rand::distributions::{Normal, IndependentSample};
|
use rand::distributions::{Normal, IndependentSample};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let normal = Normal::new(3.0, 5.0);
|
|
||||||
let mut rng = rand::thread_rng();
|
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);
|
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
|
[`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
|
||||||
[`Normal`]: https://doc.rust-lang.org/rand/rand/distributions/normal/struct.Normal.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
|
[`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
|
[`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::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
|
[`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
|
[`Regex`]: https://doc.rust-lang.org/regex/regex/struct.Regex.html
|
||||||
[`rand::Rand`]: https://doc.rust-lang.org/rand/rand/trait.Rand.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
|
[`regex::RegexSetBuilder`]: https://doc.rust-lang.org/regex/regex/struct.RegexSetBuilder.html
|
||||||
|
|
|
@ -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 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] |
|
| [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 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] |
|
| [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] |
|
| [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]
|
| [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-phone]: basics.html#ex-phone
|
||||||
[ex-rand-custom]: basics.html#ex-rand-custom
|
[ex-rand-custom]: basics.html#ex-rand-custom
|
||||||
[ex-rand-dist]: basics.html#ex-rand-dist
|
[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-rand-range]: basics.html#ex-rand-range
|
||||||
[ex-random-port-tcp]: net.html#ex-random-port-tcp
|
[ex-random-port-tcp]: net.html#ex-random-port-tcp
|
||||||
[ex-rayon-iter-mut]: concurrency.html#ex-rayon-iter-mut
|
[ex-rayon-iter-mut]: concurrency.html#ex-rayon-iter-mut
|
||||||
|
|
Loading…
Reference in a new issue