Calculate Pi using Monte Carlo #include &lt;iostream&gt;
#include &lt;iomanip&gt;


#ifdef USE_OLD_RAND

#include &lt;stdlib.h&gt;
inline double getRandDart() {return rand() * 1.0 / RAND_MAX;}

#else

#include &lt;random&gt;
std::default_random_engine              generator;
std::uniform_real_distribution&lt;double&gt;  distribution(0,1);
inline double getRandDart() {return distribution(generator);}

#endif


// Monte Carlo Simulator to estimate the value of PI.
//
// If we have a circle with a radius of 1.
// Then the smallest square that encloses the circle as sides of length 2.
//
//      Area of circle pi r^2   = pi
//      Area of square 2.r.2.r  = 4
//
//          Ratio of overlapping area:      pi/4
//
// If we throw darts randomly at a dart board (with an even distribution) and always hit the square.
// Then the ratio of darts falling into the circle should be pi/4 of the total number of darts thrown.
//
//      pi/4 * countInSquare  = countInCircle
//
//      pi = 4 . countInCircle / countInSquare
//
// To simplify the maths.
// We will set the center point as 0,0 and only use the top right quadrant of the circle.
// We have 1/4 the size of the square and circle but the same maths still apply.
//
// A dart thrown has a random x/y value in the range 0-&gt;1 (top right quadrant).
// A dart is outside the circle if x^2 + y^2 &gt; 1  (note 1^2 is 1)
//

int main()
{

    long     countInSquare   = 0;
    long     countInCircle   = 0;

    for(long iteration = 0; iteration &lt;= 10'000'000'000; ++iteration) {
        double x = getRandDart();
        double y = getRandDart();

        double d = (x * x) + (y * y);

        countInSquare   += 1;
        countInCircle   += (d &gt;= 1.0) ? 0 : 1;

        if (iteration % 10'000'000 == 0) {
            std::cout &lt;&lt; iteration &lt;&lt; " " &lt;&lt; (4.0 * countInCircle / countInSquare) &lt;&lt; "\n";
        }
    }
    std::cout &lt;&lt; "\n\n" &lt;&lt; std::setprecision(9) &lt;&lt; (4.0 * countInCircle / countInSquare) &lt;&lt; "\n";

}


Output:

&gt; ./a.out
9990000000 3.14158
10000000000 3.14158


3.14158355
One could consider at least the following points:


Instead of including &lt;stdlib.h&gt;, I'd include &lt;cstdlib&gt;.
In getRandDart(), it might in this case be more readable to do static_cast&lt;double&gt;(rand()) / RAND_MAX; instead of multiplying by 1.0.
In the for loop, all of x, y and d can be const, so I'd make them const. This has the potential to protect the programmer from unintended mistakes, and can sometimes allow the compiler to optimize better.
When you increment by one (in countInSquare += 1;), it makes more sense to use the ++ operator, i.e., to just write ++countInSquare. This is more idiomatic and protects us from unintended mistakes: ++ conveys the meaning of increment (by one), whereas with += we might accidentally write += 2; and that would be perfectly valid (but not what we wanted).
Regardless of the above point, notice that during the for-loop, it holds that iteration == countInSquare. So strictly speaking, the variable countInSquare is unnecessary and could be replaced by just iteration when needed.
You could consider making the number of iterations and the second operand of the % operand constants to allow for easier modification and perhaps to slightly improve readability.
Instead of typing (4.0 * countInCircle / countInSquare) twice, we could make a function that takes the two variables as parameters. This would allow us to save some typing, and again to protect us from unintended mistakes.