forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiApproximationMonteCarlo.js
More file actions
Latest commit
21 lines (16 loc) · 654 Bytes
/
Copy pathPiApproximationMonteCarlo.js
File metadata and controls
21 lines (16 loc) · 654 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Wikipedia: https://en.wikipedia.org/wiki/Monte_Carlo_method
// Video Explanation: https://www.youtube.com/watch?v=ELetCV_wX_c
constpiEstimation=(iterations=100000)=>{
letcircleCounter=0
for(leti=0;i<iterations;i++){
// generating random points and checking if it lies within a circle of radius 1
constx=Math.random()
consty=Math.random()
constradius=Math.sqrt(Math.pow(x,2)+Math.pow(y,2))
if(radius<1)circleCounter+=1
}
// formula for pi = (ratio of number inside circle and total iteration) x 4
constpi=(circleCounter/iterations)*4
returnpi
}
export{piEstimation}