Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mooc-rr
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
8fa9baf0ed308f30e75fd49c37689832
mooc-rr
Commits
cdd8eb7f
Commit
cdd8eb7f
authored
Mar 21, 2025
by
8fa9baf0ed308f30e75fd49c37689832
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
"Completed Buffon’s Needle and Monte Carlo estimation of π"
parent
5daec0a2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
3 deletions
+50
-3
toy_notebook_en.ipynb
module2/exo1/toy_notebook_en.ipynb
+50
-3
No files found.
module2/exo1/toy_notebook_en.ipynb
View file @
cdd8eb7f
{
{
"cells": [],
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import the numpy library\n",
"import numpy as np\n",
"# On the Computation of π\n",
"# Buffon's Needle Method\n",
"import numpy as np\n",
"\n",
"# Set a random seed for reproducibility\n",
"np.random.seed(42)\n",
"\n",
"# Number of points to simulate\n",
"n = 1000000 \n",
"\n",
"# Generate random points in the square [-1, 1] x [-1, 1]\n",
"x = np.random.uniform(-1, 1, n)\n",
"y = np.random.uniform(-1, 1, n)\n",
"\n",
"# Check if points are inside the circle\n",
"inside = (x**2 + y**2) <= 1\n",
"\n",
"# Estimate π\n",
"pi_estimate = 4 * np.sum(inside) / n\n",
"print(\"Buffon's Needle π estimate:\", pi_estimate)\n",
"# Monte Carlo Integration Approach\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Generate points and determine those inside the circle\n",
"plt.figure(figsize=(6, 6))\n",
"plt.scatter(x[inside], y[inside], color='blue', alpha=0.5, label='Inside Circle')\n",
"plt.scatter(x[~inside], y[~inside], color='red', alpha=0.5, label='Outside Circle')\n",
"plt.legend()\n",
"plt.title(\"Monte Carlo Method - Points inside/outside the circle\")\n",
"plt.show()\n",
"\n",
"# Final π Estimate\n",
"pi_estimate_mc = 4 * np.sum(inside) / n\n",
"print(\"Monte Carlo π estimate:\", pi_estimate_mc)\n",
"## Conclusion\n",
"Both Buffon’s Needle and Monte Carlo Integration provide useful approximations of π.\n",
"In this case, the estimated value using Monte Carlo is closer to the known value of π ≈ 3.14159.\n"
]
}
],
"metadata": {
"metadata": {
"kernelspec": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3",
...
@@ -16,10 +64,9 @@
...
@@ -16,10 +64,9 @@
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"pygments_lexer": "ipython3",
"version": "3.6.
3
"
"version": "3.6.
4
"
}
}
},
},
"nbformat": 4,
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 2
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment