Project: Compute π (Monte Carlo with Central Limit Theorem)
Write a program that computes and prints π using the Monte Carlo method. See ComputePi2. Compute π 20 times using the Monte Carlo method and take the average to get a better approximation. Use a function to encapsulate the Monte Carlo simulation.
The idea that the average of multiple trials yields a better approximation is called the [[Central Limit Theorem]]. Is this approach any better than just computing one estimate with a larger number of samples?
Output
$ node ComputePi4.js
PI0: 3.129560000000000
PI1: 3.141280000000000
PI2: 3.142840000000000
PI3: 3.138480000000000
PI4: 3.137400000000000
PI5: 3.144080000000000
PI6: 3.146280000000000
PI7: 3.143040000000000
PI8: 3.143240000000000
PI9: 3.148720000000000
PI10: 3.147760000000000
PI11: 3.138160000000000
PI12: 3.136560000000000
PI13: 3.131480000000000
PI14: 3.151440000000000
PI15: 3.138800000000000
PI16: 3.150400000000000
PI17: 3.137720000000000
PI18: 3.138760000000000
PI19: 3.132200000000000
PI: 3.140909999999999
abs(PI - π): 6.8265358979e-4