Finally I am successful in calculating pi value — less than 0.3% error, by using random number generation. Although my computer needs some fixation on its compiler or path definition etc, there are very good online compilers which helps in testing and running c++ codes: try the given link.
Computing the value of pi using std::rand()
Enter number of trials: 10000
Enter number of random (x,y) points per trial: 10
pi = 3.14376 +- 0.00519107
average – exact = 0.00216735
CPU time = 0.004027 secs
Here is the code I found by searching a good deal on the web. Yes I did tinker around but only because my own compiler (Turbo C++ on windows 10, 64 bits) was throwing some exceptions on the included headers.
using namespace std;
double pi_estimate(const unsigned long points) More…