"My computer tells me that $\\pi$ is approximatively"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"hideCode": true,
"hidePrompt": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.141592653589793\n"
]
}
],
"source": [
"from math import *\n",
"print(pi)"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": true,
"hidePrompt": true
},
"source": [
"## Buffon's needle"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"hideCode": true,
"hidePrompt": true
},
"outputs": [
{
"data": {
"text/plain": [
"3.128911138923655"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"np.random.seed(seed=42)\n",
"N = 10000\n",
"x = np.random.uniform(size=N, low=0, high =1)\n",
"theta = np.random.uniform(size=N, low=0, high =pi/2)\n",
"2/(sum((x+np.sin(theta))>1)/N)"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": true,
"hidePrompt": true
},
"source": [
"## Using a surface fraction argument"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": true,
"hidePrompt": true
},
"source": [
"A method that is easier to understand and does not make the use of the sin function is based on the fact that if $X ~ U(0,1)$ and $Y ~ U(0,1)$, then $P[{x}^{2}+{y}^{2} = \\pi/4]$ (see [\"Monte Carlo method\" on wikipedia](https://en.wikipedia.org/wiki/Monte_Carlo_method). The following code uses this approach:"