Estimating Pi Using Monte Carlo Simulation
Theo McGee
8/22/2020
Generating Random Numbers
- I am testing if the random number generator in javascript is good because if it isn't, the estimation for Pi won't be accurate
- I generated 100,000 numbers between 0 and 100 using the javascript Math.random function.
- Afterwards, I put the numbers into ten buckets. The first bucket holds numbers 0 - 10, the second 11 - 20, the third 21 - 30...
- I expect that each bucket will be about the same height.
Click on canvas to start simulation
Estimating Pi using Monte Carlo simulation
- A Monte Carlo simulation uses random inputs to obtain results
- I generated 500000 random points in the square and detected how far away they are from the top left corner.
- number of points < r from origin / total number of points = (1/4*Pi*r^2) / r^2
- Therefore, we can estimate Pi from the number of points in the quarter circle.
- The estimation is pretty close to Pi after the simulation.
- I expected the estimation to be closer to Pi as I generate more points but this is not always the case. I do not know why.
Click on the canvas to start simulation
Epilogue
- After, I wanted to see if the estimation of Pi would be more accurate if I were to use Python.
- I tested it out and figured out that Python was not much different from Javascript. The results were pretty similar.
❮
❯