package edu.bloomu.ch6c;

import java.util.concurrent.ThreadLocalRandom;

/**
 * Approximate the value of π by Monte Carlo simulation.
 *
 * @author Drue Coles
 */
public class PiApproximator2 {

    public static void main(String[] args) {

        final int darts = 100_000_000;
        int hits = 0;

        ThreadLocalRandom rand = ThreadLocalRandom.current();
        for (int i = 0; i < darts; i++) {

            // position of a randomly thrown dart
            double x = rand.nextDouble();
            double y = rand.nextDouble();

            if (Math.hypot(x, y) < 1) {
                hits++;
            }
        }

        // Area of bullseye: π/4
        // Area of dart board: 1
        // Probability that a random dart lands in the bulleye: π/4
        // Expected number of hits in the bullseye: (π/4) * NUM_DARTS
        // We can use the actual number of hits and solve for π.
        double π = 4.0 * hits / darts;
        System.out.println("   Approximated value of π = " + π);
        System.out.println("Correct rounded value of π = " + Math.PI);
    }
}