Search code examples
c++randommontecarlopi

Constantly getting 0 when trying to compute pi


I was asked to write a program that gets N points from a user and using a continuous distribution finds an approximation of Pi using Monte Carlo technique. This is what I wrote:

        unsigned seed = chrono::steady_clock::now().time_since_epoch().count();
        default_random_engine e (seed);
        uniform_real_distribution<> dist(0,2);
        int N = atoi(argv[1]);
        int inside = 0;
        long double appPi = 0;
        for (int i = 0; i<N; i++){
            double x = dist(e);
            double y = dist(e);
            double distance = sqrt(x*x+y*y);
            if (distance <= 1){ inside++;}
        }
        appPi = (inside/N)*4;

However after printing appPi all I get is 0. I think that algorithm by itself is ok? as it prints plausible values of x and y, but it doesn't really work for me.


Solution

  • In addition to the integer division pointed out by Xatyrian, you lack a multiplicative factor. You are extracting random points in a square of size l = 2, and then you count how many points lie in a quarter of a circle of radius R = 1. If we define the fraction of such points f we can connect this value to the areas of the square and the quarter of a circle: pi R^2 / 4 = f l^2.

    If we plug in this relation the values defined above we find pi = 16 f and not 4 f as your code seems to imply. Indeed, 0.7872 * 4 = 3.1488.

    A quick and more reasonable fix than using 16 instead of 4 is to extract points in a square of size l by making the following change:

    uniform_real_distribution<> dist(0,1);