From f3963c08c099757a2b137fdab097d11680116d5b Mon Sep 17 00:00:00 2001 From: Pierrick Lebourgeois Date: Thu, 5 Jul 2018 14:26:06 +0200 Subject: [PATCH] new file: chapters/monte_carlo/code/python/monte_carlo.py --- .../monte_carlo/code/python/monte_carlo.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 chapters/monte_carlo/code/python/monte_carlo.py diff --git a/chapters/monte_carlo/code/python/monte_carlo.py b/chapters/monte_carlo/code/python/monte_carlo.py new file mode 100644 index 00000000..95570ae7 --- /dev/null +++ b/chapters/monte_carlo/code/python/monte_carlo.py @@ -0,0 +1,29 @@ +# submitted by Pierrick Lebourgeois + +import math +import random + +def inCircle(x, y, r): + return x**2 + y**2 < r**2 + + +def monteCarlo(n): + piCount = 0; + + for i in xrange(n): + + x = random.random() + y = random.random() + + if inCircle(x, y, 1): + piCount += 1 + + piEstimate = 4.0 * piCount / n; + error = 100.0 * (math.pi - piEstimate) / math.pi + + if error < 0.1 : + print("The estimate of pi is %f \t Percent error is: %f" % (piEstimate, error) ) + + +if __name__ == "__main__": + monteCarlo(1000000)