From ccc8cf648a21d7a1e0b45c2bb04af374c4faca24 Mon Sep 17 00:00:00 2001 From: RonPeleg Date: Thu, 28 Jun 2018 11:02:56 +0300 Subject: [PATCH 1/2] Adding name to contributors file --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index a1ad4161d..41a137217 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -5,3 +5,4 @@ Gathros Jeremie Gillet (- Jie -) Salim Khatib Hitesh C +name From 048afb7c0d6a11c8b3885c9a5086c834a4578285 Mon Sep 17 00:00:00 2001 From: RonPeleg Date: Thu, 28 Jun 2018 13:44:51 +0300 Subject: [PATCH 2/2] Python3 monte carlo --- .../monte_carlo/code/Python3/monte_carlo.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 chapters/monte_carlo/code/Python3/monte_carlo.py diff --git a/chapters/monte_carlo/code/Python3/monte_carlo.py b/chapters/monte_carlo/code/Python3/monte_carlo.py new file mode 100644 index 000000000..ac6420b31 --- /dev/null +++ b/chapters/monte_carlo/code/Python3/monte_carlo.py @@ -0,0 +1,34 @@ +import random +import math + + +def in_circle(x, y, r): + """ + return True if (x,y) is inside a circle with radius r + :param x: int or float, x position of a point + :param y: int or float, y position of a point + :param r: int or float, radius of circle + :return: True or false + """ + return x ** 2 + y ** 2 < r ** 2 + + +def monte_carlo(n, r): + """ + calculate pi + :param n: int, number of points + :param r: radius of circle + :return: estimate of pi, + """ + points_in_circle = 0 + for _dummy in range(n): + x = random.random() + y = random.random() + if in_circle(x, y, r): + points_in_circle += 1 + + pi_estimate = 4.0 * points_in_circle / (n * r ** 2) + pi_erro = 100*(math.pi - pi_estimate)/math.pi + return pi_estimate, pi_erro + +print(monte_carlo(10000000, 0.5))