Skip to content

Monte Carlo Scratch Implementation #619

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 10 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions contents/monte_carlo_integration/code/scratch/InCircle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions contents/monte_carlo_integration/code/scratch/monte_carlo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<(((x) * (x)) + ((y) * (y))) < (1) >


define Monte Carlo with point number (num)
set [Count v] to (0)
repeat (num)
set [x v] to ((pick random (0) to (10000000)) / (10000000)) // 10000000 is arbitarily chosen
set [y v] to ((pick random (0) to (10000000)) / (10000000))
if <(((x) * (x)) + ((y) * (y))) < (1) > then
change [Count] by (1)
end
end
set [EstPI] to ((4) * ((Count) / (num)))
9 changes: 9 additions & 0 deletions contents/monte_carlo_integration/monte_carlo_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ each point is tested to see whether it's in the circle or not:
[import:2-10, lang:"bash"](code/bash/monte_carlo.bash)
{% sample lang="kotlin" %}
[import:3-3, lang:"kotlin"](code/kotlin/MonteCarlo.kt)
{% sample lang="scratch" %}
<p>
<img class="center" src="code/scratch/InCircle.svg" width="300" />
</p>
{% endmethod %}

If it's in the circle, we increase an internal count by one, and in the end,
Expand Down Expand Up @@ -189,6 +193,11 @@ Feel free to submit your version via pull request, and thanks for reading!
[import, lang:"bash"](code/bash/monte_carlo.bash)
{% sample lang="kotlin" %}
[import, lang:"kotlin"](code/kotlin/MonteCarlo.kt)
{% sample lang="scratch" %}
The code snippets were taken from this [scratch project](https://scratch.mit.edu/projects/319610349)
<p>
<img class="center" src="code/scratch/Algorithm.svg" width="600" />
</p>
{% endmethod %}

<script>
Expand Down