From 3bd158c665f7f464ed9e0d5b02a408eedbf4d610 Mon Sep 17 00:00:00 2001 From: nham Date: Fri, 8 Aug 2014 15:37:06 -0400 Subject: [PATCH 1/4] Add example of estimating pi using Monte Carlo simulation to std::rand --- src/libstd/rand/mod.rs | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/libstd/rand/mod.rs b/src/libstd/rand/mod.rs index 40d8f80171c4..06b9b464ebde 100644 --- a/src/libstd/rand/mod.rs +++ b/src/libstd/rand/mod.rs @@ -70,6 +70,51 @@ //! println!("{}", tuple) //! ``` //! +//! ## Monte Carlo estimation of pi +//! +//! For this example, imagine we have a square with sides of length 2 and a unit +//! circle, both centered at the origin. Since the area of a unit circle is pi, +//! the ratio +//! +//! (area of unit circle) / (area of square) +//! +//! is equal to pi / 4. So if we sample many points randomly from the square, +//! roughly pi / 4 of them should be inside the circle. +//! +//! We can use the above fact to estimate the value of pi: pick many points in the +//! square at random, calculate the fraction that fall within the circle, and +//! multiply this fraction by 4. +//! +//! ``` +//! use std::rand; +//! use std::rand::distributions::{IndependentSample, Range}; +//! +//! fn dist(x: f64, y: f64) -> f64 { +//! (x*x + y*y).sqrt() +//! } +//! +//! fn main() { +//! let between = Range::new(-1f64, 1.); +//! let mut rng = rand::task_rng(); +//! +//! let total = 1_000_000u; +//! let mut in_circle = 0u; +//! +//! for _ in range(0u, total) { +//! let a = between.ind_sample(&mut rng); +//! let b = between.ind_sample(&mut rng); +//! if dist(a, b) <= 1. { +//! in_circle += 1; +//! } +//! } +//! +//! // prints something close to 3.14159... +//! println!("{}", 4. * (in_circle as f64) / (total as f64)); +//! } +//! ``` +//! +//! ## Monty Hall Problem +//! //! This is a simulation of the [Monty Hall Problem][]: //! //! > Suppose you're on a game show, and you're given the choice of three doors: From 9315d841c7f2041e3b6960a5f5400747b1989647 Mon Sep 17 00:00:00 2001 From: nham Date: Fri, 8 Aug 2014 21:02:51 -0400 Subject: [PATCH 2/4] Remove the dist function; it is more efficient to compare squared distances --- src/libstd/rand/mod.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/libstd/rand/mod.rs b/src/libstd/rand/mod.rs index 06b9b464ebde..b9066c2f338f 100644 --- a/src/libstd/rand/mod.rs +++ b/src/libstd/rand/mod.rs @@ -89,10 +89,6 @@ //! use std::rand; //! use std::rand::distributions::{IndependentSample, Range}; //! -//! fn dist(x: f64, y: f64) -> f64 { -//! (x*x + y*y).sqrt() -//! } -//! //! fn main() { //! let between = Range::new(-1f64, 1.); //! let mut rng = rand::task_rng(); @@ -103,7 +99,7 @@ //! for _ in range(0u, total) { //! let a = between.ind_sample(&mut rng); //! let b = between.ind_sample(&mut rng); -//! if dist(a, b) <= 1. { +//! if a*a + b*b <= 1. { //! in_circle += 1; //! } //! } From 348132196a1a25336d2df6b6e0989540cdc8f7e1 Mon Sep 17 00:00:00 2001 From: nham Date: Thu, 21 Aug 2014 02:08:17 -0400 Subject: [PATCH 3/4] Surround formula in a 'notrust' code block --- src/libstd/rand/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libstd/rand/mod.rs b/src/libstd/rand/mod.rs index b9066c2f338f..00abd84d5c27 100644 --- a/src/libstd/rand/mod.rs +++ b/src/libstd/rand/mod.rs @@ -76,7 +76,9 @@ //! circle, both centered at the origin. Since the area of a unit circle is pi, //! the ratio //! +//! ```notrust //! (area of unit circle) / (area of square) +//! ``` //! //! is equal to pi / 4. So if we sample many points randomly from the square, //! roughly pi / 4 of them should be inside the circle. From 86587224d0b99c69da01ebfaf98052694036302b Mon Sep 17 00:00:00 2001 From: nham Date: Thu, 21 Aug 2014 02:42:15 -0400 Subject: [PATCH 4/4] Use unicode pi symbol in pi estimation example. Additional tweaks --- src/libstd/rand/mod.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libstd/rand/mod.rs b/src/libstd/rand/mod.rs index 00abd84d5c27..0739d91b5840 100644 --- a/src/libstd/rand/mod.rs +++ b/src/libstd/rand/mod.rs @@ -70,20 +70,20 @@ //! println!("{}", tuple) //! ``` //! -//! ## Monte Carlo estimation of pi +//! ## Monte Carlo estimation of π //! //! For this example, imagine we have a square with sides of length 2 and a unit -//! circle, both centered at the origin. Since the area of a unit circle is pi, -//! the ratio +//! circle, both centered at the origin. Since the area of a unit circle is π, +//! we have: //! //! ```notrust -//! (area of unit circle) / (area of square) +//! (area of unit circle) / (area of square) = π / 4 //! ``` //! -//! is equal to pi / 4. So if we sample many points randomly from the square, -//! roughly pi / 4 of them should be inside the circle. +//! So if we sample many points randomly from the square, roughly π / 4 of them +//! should be inside the circle. //! -//! We can use the above fact to estimate the value of pi: pick many points in the +//! We can use the above fact to estimate the value of π: pick many points in the //! square at random, calculate the fraction that fall within the circle, and //! multiply this fraction by 4. //!