-
-
Notifications
You must be signed in to change notification settings - Fork 359
Python3 monte carlo #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python3 monte carlo #148
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ Gathros | |
Jeremie Gillet (- Jie -) | ||
Salim Khatib | ||
Hitesh C | ||
name | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have moved away from using |
||
""" | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then in this line you don't need |
||
pi_erro = 100*(math.pi - pi_estimate)/math.pi | ||
return pi_estimate, pi_erro | ||
|
||
print(monte_carlo(10000000, 0.5)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The last thing is, it's best if you have a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably better if you put in your actual name :)