Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. public static double berechne_pi(int tropfenzahl) {
  2.  
  3.   double pi = 0;
  4.   int innerhalb = 0;
  5.   int gesamt = tropfenzahl;
  6.  
  7.   while (tropfenzahl > 0) { // generiere Tropfen und addiere je nach Zugehörigkeit
  8.     double dotx = Math.random();
  9.     double doty = Math.random();
  10.  
  11.     if (dotx*dotx + doty*doty <= 1) {
  12.       // Punkt liegt innerhalb des Kreises
  13.       innerhalb++;
  14.     } else {
  15.       // Punkt liegt außerhalb des Kreises
  16.     }
  17.  
  18.     tropfenzahl--;
  19.   }
  20.  
  21.   pi = 4*(double)innerhalb/gesamt;
  22.   return pi;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement