Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Program {
- static double monteCarloPI(int n) {
- int pointsIn = 0;
- for (int i = 0; i < n; i++) {
- double x = Math.random();
- double y = Math.random();
- if (Math.sqrt(x*x + y*y) < 1.0) {
- pointsIn++;
- }
- }
- return 4.0 * (double)pointsIn / n; // PI
- }
- public static void main(String[] args) {
- System.out.println("PI = " + monteCarloPI(100));
- System.out.println("PI = " + monteCarloPI(1000));
- System.out.println("PI = " + monteCarloPI(10000));
- System.out.println("PI = " + monteCarloPI(10000000));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement