import java.util.Scanner; import java.util.Random; public class approximate_pi { public static void main(String[] args) { long total_points = 0; // total points counter long inside_points = 0; // counter for points inside circle Random r = new Random(); // random object double api = 0; // approximate value of pi, initially set to 0 for(;;) { double x = r.nextDouble()*2 - 1;// random numbers between -1 and 1 double y = r.nextDouble()*2 - 1; double length = Math.sqrt(x*x + y*y); // length of vector from origin if(length<1) // means point is inside circle { inside_points++; } total_points++;// total points increase api = 4*(double)(inside_points)/(double)total_points; // approximate pi if( (api>3.141) && (api<3.1419)) // if within some threshold, we stop { break; } } // report results System.out.println("Total number of points: " + total_points); System.out.println("Inside points: " + inside_points); System.out.println("Approximate value of pi is: " + api); } }