A method that is easier to understand and does not make use of the <code>sin</code> function is based on the fact that if \(X \approx U(0,1)\) and \(Y \approx U(0,1)\), then \(P[X^2 + Y^2 \leq 1] = \pi/4\) (see <ahref="https://en.wikipedia.org/wiki/Monte_Carlo_method">“Monte Carlo method on Wikipedia”</a>). The following code uses this approach:
#begin<sub>src</sub> python :results output :exports both
<h2id="org8060f5c"><spanclass="section-number-2">3</span> 3. Using a surface fraction argument</h2>
<divclass="outline-text-2"id="text-3">
<p>
np.random.seed(seed=42)
N = 1000
x = np.random.uniform(size=N, low=0, high=1)
y = np.random.uniform(size=N, low=0, high=1)
A method that is easier to understand and does not make use of the <code>sin</code> function is based on the fact that if \(X \approx U(0,1)\) and \(Y \approx U(0,1)\), then \(P[X^2 + Y^2 \leq 1] = \pi/4\) (see <ahref="https://en.wikipedia.org/wiki/Monte_Carlo_method">“Monte Carlo method on Wikipedia”</a>). The following code uses this approach:
It is then straightforward to obtain a (not really good) approximation to π by counting how many times, on average, \(X^2 + Y^2\) is smaller than 1:
#begin<sub>src</sub> python :results output :exports both
A method that is easier to understand and does not make use of the =sin= function is based on the fact that if $X \approx U(0,1)$ and $Y \approx U(0,1)$, then $P[X^2 + Y^2 \leq 1] = \pi/4$ (see [[https://en.wikipedia.org/wiki/Monte_Carlo_method]["Monte Carlo method on Wikipedia"]]). The following code uses this approach:
#begin_src python :results output :exports both
import matplotlib.pyplot as plt
# #+PROPERTY: header-args :session :exports both
* 1 Asking the math library
My computer tells me that \pi is approximatively
#+begin_src python :results value :session *python* :exports both
from math import *
pi
#+end_src
#+RESULTS:
: 3.141592653589793
* 2 * Buffon's needle
Applying the method of [[https://en.wikipedia.org/wiki/Buffon%27s_needle_problem][Buffon's needle]], we get the *approximation*
#+begin_src python :results value :session :*python* :exports both
A method that is easier to understand and does not make use of the =sin= function is based on the fact that if $X \approx U(0,1)$ and $Y \approx U(0,1)$, then $P[X^2 + Y^2 \leq 1] = \pi/4$ (see [[https://en.wikipedia.org/wiki/Monte_Carlo_method]["Monte Carlo method on Wikipedia"]]). The following code uses this approach: