Pure Programmer
Blue Matrix

Cluster Map

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?

$ g++ -std=c++17 ComputePi4.cpp -o ComputePi4 -lfmt $ ./ComputePi4 PI0: 3.141160000000000 PI1: 3.148000000000000 PI2: 3.141080000000000 PI3: 3.136640000000000 PI4: 3.139240000000000 PI5: 3.143720000000000 PI6: 3.150360000000000 PI7: 3.140680000000000 PI8: 3.133120000000000 PI9: 3.133520000000000 PI10: 3.135000000000000 PI11: 3.138560000000000 PI12: 3.129000000000000 PI13: 3.146200000000000 PI14: 3.145920000000000 PI15: 3.140360000000000 PI16: 3.140920000000000 PI17: 3.140080000000000 PI18: 3.138240000000000 PI19: 3.136360000000000 PI: 3.139908000000000 abs(PI - π): 1.6846535898e-03