Problem (Buffon, 1777). A floor has parallel lines at distance . A needle of length is dropped at random. What is the probability that it crosses a line?

Answer. For a short needle (): . Dropping needles and counting crossings gives the estimate .

VariantResult
short needle,
long needle,
expected number of crossings, any
Buffon’s noodle: any rigid curve of length expected crossings , whatever its shape
Barbier’s theoremevery convex curve of constant width has perimeter
estimating with throws, relative standard error : six digits need about throws
Laplace’s grid (two families of lines, cells , )
const double PI = acos(-1.0);
 
double buffonProb(double l, double d) {             // probability that a needle of length l crosses a line (spacing d)
    if (l <= d) return 2 * l / (PI * d);
    double r = d / l;
    return 1 + 2 / PI * (l / d * (1 - sqrt(1 - r * r)) - asin(r));
}
 
// Monte Carlo estimate of pi. The direction is sampled by rejection from the unit disk, so pi is never used.
double estimatePi(long long n, double l, double d, mt19937_64& rng) {
    uniform_real_distribution<double> U(-1, 1), X(0, d / 2);
    long long hits = 0;
    for (long long i = 0; i < n; i++) {
        double ux, uy, r2;
        do { ux = U(rng); uy = U(rng); r2 = ux * ux + uy * uy; } while (r2 > 1 || r2 == 0);
        double s = fabs(uy) / sqrt(r2);              // |sin theta| of a uniform direction
        hits += X(rng) <= l / 2 * s;                 // centre-to-nearest-line distance vs half the vertical extent
    }
    return 2 * l * n / (d * hits);                   // valid for l <= d
}