mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-22 19:43:06 +00:00
Added another rand example
This commit is contained in:
parent
d66b7fbf58
commit
308e35f2ba
3 changed files with 126 additions and 3 deletions
|
@ -90,7 +90,12 @@ MathJax.Hub.Queue(function() {
|
||||||
<p><a href="https://doc.rust-lang.org/rand/rand/index.html"><img src="https://img.shields.io/crates/v/rand.svg?label=rand" alt="rand-badge" /></a></p>
|
<p><a href="https://doc.rust-lang.org/rand/rand/index.html"><img src="https://img.shields.io/crates/v/rand.svg?label=rand" alt="rand-badge" /></a></p>
|
||||||
<a class="header" href="#example-monte-carlo-integration" name="example-monte-carlo-integration"><h3>Example: Monte carlo integration</h3></a>
|
<a class="header" href="#example-monte-carlo-integration" name="example-monte-carlo-integration"><h3>Example: Monte carlo integration</h3></a>
|
||||||
<p>Use the <code>rand</code> crate to generate random samples and approximate
|
<p>Use the <code>rand</code> crate to generate random samples and approximate
|
||||||
$\int_{0}^{\pi} sin(x) dx$ using monte carlo method.</p>
|
$\int_{0}^{\pi} sin(x) dx$ using monte carlo.</p>
|
||||||
|
<p><em>Key concepts:</em></p>
|
||||||
|
<ul>
|
||||||
|
<li>Creating thread-specific RNG</li>
|
||||||
|
<li>Generating real numbers over an interval</li>
|
||||||
|
</ul>
|
||||||
<pre><code class="language-rust">extern crate rand;
|
<pre><code class="language-rust">extern crate rand;
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
@ -127,6 +132,41 @@ fn main() {
|
||||||
println!("{}", monte_carlo(0., f32::consts::PI, 200_000));
|
println!("{}", monte_carlo(0., f32::consts::PI, 200_000));
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
<a class="header" href="#example-generating-random-rgb-colors" name="example-generating-random-rgb-colors"><h3>Example: Generating random RGB colors</h3></a>
|
||||||
|
<p>A <em>trait</em> is a language feature that tells the Rust compiler about functionality a type must provide.</p>
|
||||||
|
<p>Rust has the powerful ability to create traits for your own types.
|
||||||
|
One example is <code>rand::Rand</code>. Any type that implements Rand can use the
|
||||||
|
polymorphic function <code>Rng::gen()</code> to generate random types.</p>
|
||||||
|
<p><em>Key concepts:</em></p>
|
||||||
|
<ul>
|
||||||
|
<li>Generating a random structure</li>
|
||||||
|
</ul>
|
||||||
|
<pre><code class="language-rust">extern crate rand;
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
use rand::Rand;
|
||||||
|
|
||||||
|
#[derive(Debug)] // Allows us to print using {:?} format specifier
|
||||||
|
struct Color { // RGB Color struct
|
||||||
|
r: f64,
|
||||||
|
g: f64,
|
||||||
|
b: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implementing Rand for type Color
|
||||||
|
impl Rand for Color {
|
||||||
|
fn rand<R: Rng>(rng: &mut R) -> Self {
|
||||||
|
Color {r: rng.next_f64(), b: rng.next_f64(), g: rng.next_f64()}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Generate a random Color and print to stdout
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let c: Color = rng.gen();
|
||||||
|
println!("{:?}", c);
|
||||||
|
}
|
||||||
|
</code></pre>
|
||||||
<!-- Links -->
|
<!-- Links -->
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -144,7 +144,12 @@ MathJax.Hub.Queue(function() {
|
||||||
<p><a href="https://doc.rust-lang.org/rand/rand/index.html"><img src="https://img.shields.io/crates/v/rand.svg?label=rand" alt="rand-badge" /></a></p>
|
<p><a href="https://doc.rust-lang.org/rand/rand/index.html"><img src="https://img.shields.io/crates/v/rand.svg?label=rand" alt="rand-badge" /></a></p>
|
||||||
<a class="header" href="#example-monte-carlo-integration" name="example-monte-carlo-integration"><h3>Example: Monte carlo integration</h3></a>
|
<a class="header" href="#example-monte-carlo-integration" name="example-monte-carlo-integration"><h3>Example: Monte carlo integration</h3></a>
|
||||||
<p>Use the <code>rand</code> crate to generate random samples and approximate
|
<p>Use the <code>rand</code> crate to generate random samples and approximate
|
||||||
$\int_{0}^{\pi} sin(x) dx$ using monte carlo method.</p>
|
$\int_{0}^{\pi} sin(x) dx$ using monte carlo.</p>
|
||||||
|
<p><em>Key concepts:</em></p>
|
||||||
|
<ul>
|
||||||
|
<li>Creating thread-specific RNG</li>
|
||||||
|
<li>Generating real numbers over an interval</li>
|
||||||
|
</ul>
|
||||||
<pre><code class="language-rust">extern crate rand;
|
<pre><code class="language-rust">extern crate rand;
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
@ -181,6 +186,41 @@ fn main() {
|
||||||
println!("{}", monte_carlo(0., f32::consts::PI, 200_000));
|
println!("{}", monte_carlo(0., f32::consts::PI, 200_000));
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
<a class="header" href="#example-generating-random-rgb-colors" name="example-generating-random-rgb-colors"><h3>Example: Generating random RGB colors</h3></a>
|
||||||
|
<p>A <em>trait</em> is a language feature that tells the Rust compiler about functionality a type must provide.</p>
|
||||||
|
<p>Rust has the powerful ability to create traits for your own types.
|
||||||
|
One example is <code>rand::Rand</code>. Any type that implements Rand can use the
|
||||||
|
polymorphic function <code>Rng::gen()</code> to generate random types.</p>
|
||||||
|
<p><em>Key concepts:</em></p>
|
||||||
|
<ul>
|
||||||
|
<li>Generating a random structure</li>
|
||||||
|
</ul>
|
||||||
|
<pre><code class="language-rust">extern crate rand;
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
use rand::Rand;
|
||||||
|
|
||||||
|
#[derive(Debug)] // Allows us to print using {:?} format specifier
|
||||||
|
struct Color { // RGB Color struct
|
||||||
|
r: f64,
|
||||||
|
g: f64,
|
||||||
|
b: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implementing Rand for type Color
|
||||||
|
impl Rand for Color {
|
||||||
|
fn rand<R: Rng>(rng: &mut R) -> Self {
|
||||||
|
Color {r: rng.next_f64(), b: rng.next_f64(), g: rng.next_f64()}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Generate a random Color and print to stdout
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let c: Color = rng.gen();
|
||||||
|
println!("{:?}", c);
|
||||||
|
}
|
||||||
|
</code></pre>
|
||||||
<!-- Links -->
|
<!-- Links -->
|
||||||
<a class="header" href="#byteorder" name="byteorder"><h1>Byteorder</h1></a>
|
<a class="header" href="#byteorder" name="byteorder"><h1>Byteorder</h1></a>
|
||||||
<p><a href="https://docs.rs/byteorder"><img src="https://img.shields.io/crates/v/rustc-serialize.svg?label=byteorder" alt="byteorder-badge" /></a></p>
|
<p><a href="https://docs.rs/byteorder"><img src="https://img.shields.io/crates/v/rustc-serialize.svg?label=byteorder" alt="byteorder-badge" /></a></p>
|
||||||
|
|
|
@ -23,7 +23,11 @@ MathJax.Hub.Queue(function() {
|
||||||
|
|
||||||
### Example: Monte carlo integration
|
### Example: Monte carlo integration
|
||||||
Use the `rand` crate to generate random samples and approximate
|
Use the `rand` crate to generate random samples and approximate
|
||||||
$\int_{0}^{\pi} sin(x) dx$ using monte carlo method.
|
$\int_{0}^{\pi} sin(x) dx$ using monte carlo.
|
||||||
|
|
||||||
|
*Key concepts:*
|
||||||
|
* Creating thread-specific RNG
|
||||||
|
* Generating real numbers over an interval
|
||||||
```rust
|
```rust
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
|
||||||
|
@ -62,6 +66,45 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Example: Generating random RGB colors
|
||||||
|
A *trait* is a language feature that tells the Rust compiler about functionality a type must provide.
|
||||||
|
|
||||||
|
Rust has the powerful ability to create traits for your own types.
|
||||||
|
One example is `rand::Rand`. Any type that implements Rand can use the
|
||||||
|
polymorphic function `Rng::gen()` to generate random types.
|
||||||
|
|
||||||
|
*Key concepts:*
|
||||||
|
* Generating a random structure
|
||||||
|
|
||||||
|
```rust
|
||||||
|
extern crate rand;
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
use rand::Rand;
|
||||||
|
|
||||||
|
#[derive(Debug)] // Allows us to print using {:?} format specifier
|
||||||
|
struct Color { // RGB Color struct
|
||||||
|
r: f64,
|
||||||
|
g: f64,
|
||||||
|
b: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implementing Rand for type Color
|
||||||
|
impl Rand for Color {
|
||||||
|
fn rand<R: Rng>(rng: &mut R) -> Self {
|
||||||
|
Color {r: rng.next_f64(), b: rng.next_f64(), g: rng.next_f64()}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Generate a random Color and print to stdout
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let c: Color = rng.gen();
|
||||||
|
println!("{:?}", c);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
<!-- Links -->
|
<!-- Links -->
|
||||||
|
|
||||||
[rand-badge]: https://img.shields.io/crates/v/rand.svg?label=rand
|
[rand-badge]: https://img.shields.io/crates/v/rand.svg?label=rand
|
||||||
|
|
Loading…
Reference in a new issue