Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d8eee0c commit 4203835Copy full SHA for 4203835
1 file changed
Maths/PiApproximationMonteCarlo.js
@@ -0,0 +1,25 @@
1
+// Wikipedia: https://en.wikipedia.org/wiki/Monte_Carlo_method
2
+// Video Explaination: https://www.youtube.com/watch?v=ELetCV_wX_c
3
+
4
+function piEstimation (iterations = 100000) {
5
+ let circleCounter = 0
6
7
+ for (let i = 0; i < iterations; i++) {
8
+ // generating random points and checking if it lies within a circle of radius 1
9
+ const x = Math.random()
10
+ const y = Math.random()
11
+ const radius = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))
12
13
+ if (radius < 1) circleCounter += 1
14
+ }
15
16
+ // fomula for pi = (ratio of number inside circle and total iteration) x 4
17
+ const pi = (circleCounter / iterations) * 4
18
+ return pi
19
+}
20
21
+function main () {
22
+ console.log(piEstimation())
23
24
25
+main()
0 commit comments