From e070bc9b8a92736372c144e399d0cebd40a635e3 Mon Sep 17 00:00:00 2001 From: asuresh213 <31808179+asuresh213@users.noreply.github.com> Date: Mon, 2 Jul 2018 22:15:32 -0400 Subject: [PATCH 1/3] Add files via upload --- chapters/monte_carlo/code/MonteCarlo.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 chapters/monte_carlo/code/MonteCarlo.py diff --git a/chapters/monte_carlo/code/MonteCarlo.py b/chapters/monte_carlo/code/MonteCarlo.py new file mode 100644 index 000000000..cced2bcd2 --- /dev/null +++ b/chapters/monte_carlo/code/MonteCarlo.py @@ -0,0 +1,25 @@ +import math +import random + + +def in_circle(x, y, rad): + if x**2 + y**2 < rad**2: + return True + else: + return False + + +def monte_carlo_integration(sample, rad): + count = 0 + for i in range(sample): + x = random.random()*2 - 1 + y = random.random()*2 - 1 + if in_circle(x, y, rad): + count += 1 + + estimate = (4*count)/(sample*rad*rad) + print("The Estimate of Pi is: %s" % estimate) + print("The error is: %s" % (math.pi - estimate)) + + +monte_carlo_integration(1000000, 1) From 530dc007bed01f09903d1441ad70fffcf1b7ee76 Mon Sep 17 00:00:00 2001 From: asuresh213 <31808179+asuresh213@users.noreply.github.com> Date: Mon, 2 Jul 2018 22:16:03 -0400 Subject: [PATCH 2/3] Delete MonteCarlo.py --- chapters/monte_carlo/code/MonteCarlo.py | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 chapters/monte_carlo/code/MonteCarlo.py diff --git a/chapters/monte_carlo/code/MonteCarlo.py b/chapters/monte_carlo/code/MonteCarlo.py deleted file mode 100644 index cced2bcd2..000000000 --- a/chapters/monte_carlo/code/MonteCarlo.py +++ /dev/null @@ -1,25 +0,0 @@ -import math -import random - - -def in_circle(x, y, rad): - if x**2 + y**2 < rad**2: - return True - else: - return False - - -def monte_carlo_integration(sample, rad): - count = 0 - for i in range(sample): - x = random.random()*2 - 1 - y = random.random()*2 - 1 - if in_circle(x, y, rad): - count += 1 - - estimate = (4*count)/(sample*rad*rad) - print("The Estimate of Pi is: %s" % estimate) - print("The error is: %s" % (math.pi - estimate)) - - -monte_carlo_integration(1000000, 1) From 7b40449a5e37860fc6c64b7c19b7f77bee2d6506 Mon Sep 17 00:00:00 2001 From: asuresh213 <31808179+asuresh213@users.noreply.github.com> Date: Mon, 2 Jul 2018 22:16:34 -0400 Subject: [PATCH 3/3] MonteCarlo Integration using Python --- chapters/monte_carlo/code/MonteCarlo.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 chapters/monte_carlo/code/MonteCarlo.py diff --git a/chapters/monte_carlo/code/MonteCarlo.py b/chapters/monte_carlo/code/MonteCarlo.py new file mode 100644 index 000000000..cced2bcd2 --- /dev/null +++ b/chapters/monte_carlo/code/MonteCarlo.py @@ -0,0 +1,25 @@ +import math +import random + + +def in_circle(x, y, rad): + if x**2 + y**2 < rad**2: + return True + else: + return False + + +def monte_carlo_integration(sample, rad): + count = 0 + for i in range(sample): + x = random.random()*2 - 1 + y = random.random()*2 - 1 + if in_circle(x, y, rad): + count += 1 + + estimate = (4*count)/(sample*rad*rad) + print("The Estimate of Pi is: %s" % estimate) + print("The error is: %s" % (math.pi - estimate)) + + +monte_carlo_integration(1000000, 1)