Skip to content
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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Gathros
Jeremie Gillet (- Jie -)
Salim Khatib
Hitesh C
Jonas Vander Vennet
20 changes: 20 additions & 0 deletions chapters/monte_carlo/code/python3/monte_carlo.py
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):
Copy link
Member

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 the radius variable?

return x**2 + y**2 < radius**2

# function to integrate a circle with given radius via monte carlo integration
def monte_carlo(n, radius):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also remove radius here

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))
5 changes: 5 additions & 0 deletions chapters/monte_carlo/monte_carlo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just changed some things and settled on py instead of py3, basically just requiring code to be submitted in python3. Would you be able to change this to py?

[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,
Expand Down Expand Up @@ -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" %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, change to py

### Python 3
[import, lang:"python"](code/python3/monte_carlo.py)
{% endmethod %}


Expand Down