From 9bdc7c0a3d2aa79c878b24c77fc92a1722bdaed8 Mon Sep 17 00:00:00 2001 From: dovisutu <9709021@qq.com> Date: Wed, 3 Jul 2019 18:13:26 +0800 Subject: [PATCH 01/10] Implement Monte Carlo in Scratch --- .../code/scratch/Algorithm.svg | 112 ++++++++++++++++++ .../code/scratch/InCircle.svg | 112 ++++++++++++++++++ .../code/scratch/monte_carlo.txt | 13 ++ .../monte_carlo_integration.md | 9 ++ 4 files changed, 246 insertions(+) create mode 100644 contents/monte_carlo_integration/code/scratch/Algorithm.svg create mode 100644 contents/monte_carlo_integration/code/scratch/InCircle.svg create mode 100644 contents/monte_carlo_integration/code/scratch/monte_carlo.txt diff --git a/contents/monte_carlo_integration/code/scratch/Algorithm.svg b/contents/monte_carlo_integration/code/scratch/Algorithm.svg new file mode 100644 index 000000000..8b9c7935e --- /dev/null +++ b/contents/monte_carlo_integration/code/scratch/Algorithm.svg @@ -0,0 +1,112 @@ +defineMonteCarlowithpointnumernumsetCountto0repeatnumsetxtopickrandom0to10000000/10000000setytopickrandom0to10000000/10000000ifx*x+y*y<1thenchangeCountby1setEstPIto4*Count/num diff --git a/contents/monte_carlo_integration/code/scratch/InCircle.svg b/contents/monte_carlo_integration/code/scratch/InCircle.svg new file mode 100644 index 000000000..75c918331 --- /dev/null +++ b/contents/monte_carlo_integration/code/scratch/InCircle.svg @@ -0,0 +1,112 @@ +x*x+y*y<1 \ No newline at end of file diff --git a/contents/monte_carlo_integration/code/scratch/monte_carlo.txt b/contents/monte_carlo_integration/code/scratch/monte_carlo.txt new file mode 100644 index 000000000..6d48dc8f7 --- /dev/null +++ b/contents/monte_carlo_integration/code/scratch/monte_carlo.txt @@ -0,0 +1,13 @@ +<(((x) * (x)) + ((y) * (y))) < (1) > + + +define Monte Carlo with point numer (num) +set [Count v] to (0) +repeat (num) + set [x v] to ((pick random (0) to (10000000)) / (10000000)) + 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))) \ No newline at end of file diff --git a/contents/monte_carlo_integration/monte_carlo_integration.md b/contents/monte_carlo_integration/monte_carlo_integration.md index c3a50dd36..186f7347d 100644 --- a/contents/monte_carlo_integration/monte_carlo_integration.md +++ b/contents/monte_carlo_integration/monte_carlo_integration.md @@ -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" %} +

+ +

{% endmethod %} If it's in the circle, we increase an internal count by one, and in the end, @@ -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) +

+ +

{% endmethod %}