lundi 23 mars 2020

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.




Aucun commentaire:

Enregistrer un commentaire