{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Week 6 Bonus - Compute PI with a random generator\n",
"The number PI can be calculated by random numbers. Suppose you have a circle with radius 1 which lies in a square with a side length of 2. Then the square has an area of 4 (2 * 2) and the circle has an area of PI (r = 1, thus PI*r^2 = PI). \n",
"\n",
"![Figure1](img1.png)\n",
"\n",
"If thousands of points are randomly created within the square, then some lie in the square AND in the circle, others lie in the square and not in the circle. The situation can be simplified if you just take the upper right quadrant of the above figure. This square has a side length of 1. Each point within the square can be described by the coordinates `x` and `y` (0 < x, y < 1). If x\\*\\*2 + y\\*\\*2 < 1, then the point specified by (x,y) is as well within the circle.\n",
"\n",
"![Figure2](img2.png)\n",
"\n",
"As the relation between the sizes of circle and square is PI/4, the relation between the points in the circle and the points in the square must be PI/4 as well. Using this formula, one can now use a random number generator to calculate PI.\n",
"\n",
"## Your Task\n",
"Using the library `random` create 10.000 points in the square, that means generate values for `x` and `y` (The random value must be between 0 and 1). Check, if x\\*\\*2 + y\\*\\*2 is < 1. If this is the case, then the point lies within the circle. Count the total number of points and the points which lie in the circle. Use these numbers to calculate PI. Compare your random PI with the real PI, which can be found in the `math` library.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"import math\n",
"\n",
"hit = 0\n",
"total = 0\n",
"\n",
"for _ in range(1000000):\n",
" x = random.random()\n",
" y = random.random()\n",
" if x ** 2 + y ** 2 < 1:\n",
" hit += 1\n",
" total += 1\n",
" if total % 100000 == 0:\n",
" print(4 * hit / total - math.pi)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}