Skip to content

implemented monte carlo integration in nim #356

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

Merged
Merged
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
27 changes: 27 additions & 0 deletions contents/monte_carlo_integration/code/nim/monte_carlo.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import random
import math

randomize()

proc in_circle(x,y,radius: float): bool =
return x * x + y * y < radius * radius

proc monte_carlo(samples: int): float =
const radius: float = 1
var count: int = 0

for i in 0 .. < samples:
Copy link
Member

Choose a reason for hiding this comment

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

This notation is very clean, I like Nim :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah a cool thing about nim is that it is incredibly flexible, you can compile it to c, c++, or javascript. Because of this you can use any existing libraries for c or c++ or javascript.

Copy link
Member

Choose a reason for hiding this comment

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

That's awesome! Thanks for the info and for adding nim to the AAA :)

var
x: float = random(radius)
y: float = random(radius)

if in_circle(x,y,radius):
count += 1

var pi_estimate: float = 4 * count / samples
return pi_estimate

let estimate: float = monte_carlo(1000000)

echo "the estimate of pi is ", estimate
echo "percent error: ", 100 * (abs(estimate - PI)/PI)
5 changes: 5 additions & 0 deletions contents/monte_carlo_integration/monte_carlo_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ each point is tested to see whether it's in the circle or not:
[import:5-7, lang:"python"](code/python/monte_carlo.py)
{% sample lang="cs" %}
[import:23-23, lang:"csharp"](code/csharp/Circle.cs)
{% sample lang="nim" %}
[import:6-7, lang:"nim"](code/nim/monte_carlo.nim)
{% endmethod %}

If it's in the circle, we increase an internal count by one, and in the end,
Expand Down Expand Up @@ -129,9 +131,12 @@ Feel free to submit your version via pull request, and thanks for reading!
[import, lang:"csharp"](code/csharp/Circle.cs)
##### Program.cs
[import, lang:"csharp"](code/csharp/Program.cs)
{% sample lang="nim" %}
[import, lang:"nim"](code/nim/monte_carlo.nim)
{% endmethod %}



<script>
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
</script>