Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Tools.IO.*;
- public class MonteCarlo {
- public static void main (String[] args)
- {
- //Gesamtanzahl der Punkte
- int n=IOTools.readInteger ("Bitte die Anzahl der Punkte eingeben : ");
- //Zählvariable, die die Punkte im Viertelkreis zählt
- int zaehler=0;
- for(int i=0 ; i<n ; i++)
- {
- //Erzeugung der Zufallskoordinaten
- double x = Math.random();
- double y = Math.random();
- // Wenn die Zufallskoordinaten einen Punkt im Kreis ergeben, dann wird der Zähler für die Punkte
- //im Kreis um einen hochgezählt.
- if (Math.sqrt((x*x)+(y*y))<=1)
- {
- zaehler ++;
- }
- }
- System.out.println( "Der Zähler liegt bei " + zaehler );
- System.out.println(" Die Gesamtpunktzahl beträgt: " +n);
- double p = ((zaehler) /(n))*4 ;
- System.out.println("Pi ist durch Näherung des Monte Carlo Verfahrens etwa : " + p);
- System.out.println("Der absolute Fehler ist : " + ((Math.PI)-(p)) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement