-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
Added python3 to Monte Carlo Integration #144
Changes from all commits
812dcb7
880db16
8fc51f1
42f4e10
eb8f6a5
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 | ||
Jonas Vander Vennet |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#example submitted by Jonas Vander Vennet, Python 3.5.2 | ||
|
||
import random, math | ||
|
||
# function to determine whether an x, y point is inside a circle with given radius | ||
def in_circle(x, y, radius): | ||
return x**2 + y**2 < radius**2 | ||
|
||
# function to integrate a circle with given radius via monte carlo integration | ||
def monte_carlo(n, radius): | ||
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. Also remove |
||
count = 0 | ||
for i in range(n): | ||
count += in_circle(random.uniform(0,radius),random.uniform(0,radius),radius) | ||
return 4*(count/n)*radius**2 | ||
|
||
#estimate pi by integrating the unit circle | ||
estimated_pi = monte_carlo(10**6, 1) | ||
|
||
print("percent error: {:%}".format(abs(estimated_pi-math.pi)/math.pi)) | ||
print("Estimation for pi: {:f}".format(estimated_pi)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,8 @@ each point is tested to see whether it's in the circle or not: | |
[import:2-8, lang:"julia"](code/julia/monte_carlo.jl) | ||
{% sample lang="hs" %} | ||
[import:7-7, lang:"haskell"](code/haskell/monteCarlo.hs) | ||
{% sample lang="py3" %} | ||
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 just changed some things and settled on |
||
[import:5-7, lang:"python"](code/python3/monte_carlo.py) | ||
{% endmethod %} | ||
|
||
If it's in the circle, we increase an internal count by one, and in the end, | ||
|
@@ -78,6 +80,9 @@ Feel free to submit your version via pull request, and thanks for reading! | |
{% sample lang="hs" %} | ||
### Haskell | ||
[import, lang:"haskell"](code/haskell/monteCarlo.hs) | ||
{% sample lang="py3" %} | ||
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. Same here, change to |
||
### Python 3 | ||
[import, lang:"python"](code/python3/monte_carlo.py) | ||
{% endmethod %} | ||
|
||
|
||
|
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.
We slightly modified most implementations to hide the
radius
variable because it was confusing some people and we specified that we were working on the unit circle. Would you be able to remove theradius
variable?