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..7472416c7 --- /dev/null +++ b/contents/monte_carlo_integration/code/scratch/Algorithm.svg @@ -0,0 +1 @@ +defineMonteCarlowithpointnumbernumsetCountto0repeatnumsetxtopickrandom0.0to1.0setytopickrandom0.0to1.0ifx*x+y*y&lt;radius*radiusthenchangeCountby1 the radius is 1setEstPIto4*Count/numwhenclickedsetPIto3.1415926535897932384626433832795028841971493993askNumber of point?andwaitMonteCarlowithpointnumberanswersayPi estimate done.for2secondssayjoinEstimate pi: EstPIfor2secondssayjoinReal Pi: PIfor2secondssayjoinPercent Error: joinabsofEstPI-PI/PI*100%for2seconds \ No newline at end of file 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..7eab70894 --- /dev/null +++ b/contents/monte_carlo_integration/code/scratch/InCircle.svg @@ -0,0 +1 @@ +x*x+y*y<radius*radius \ 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..117ee78a2 --- /dev/null +++ b/contents/monte_carlo_integration/code/scratch/monte_carlo.txt @@ -0,0 +1,22 @@ +<(((x) * (x)) + ((y) * (y))) < ((radius) * (radius)) > + + +define Monte Carlo with point number (num) +set [Count v] to (0) +repeat (num) + set [x v] to (pick random (0.0) to (1.0)) + set [y v] to (pick random (0.0) to (1.0)) + if <(((x) * (x)) + ((y) * (y))) < ((radius) * (radius)) > then // the radius is 1 + change [Count] by (1) + end +end +set [EstPI v] to ((4) * ((Count) / (num))) + +when @greenflag clicked +set [PI v] to (3.1415926535897932384626433832795028841971493993) +ask [Number of point?] and wait +Monte Carlo with point number (answer) +say [Pi estimate done.] for (2) seconds +say (join [Estimate pi: ] (EstPI)) for (2) seconds +say (join [Real Pi: ] (PI)) for (2) seconds +say (join [Percent Error: ] (join ((([abs v] of ((EstPI) - (PI))) / (PI)) * (100)) [%])) for (2) seconds \ 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..92b47bd13 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 %}