-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiRegen.java
40 lines (34 loc) · 946 Bytes
/
PiRegen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package demos;
import java.awt.Color;
import java.awt.Rectangle;
import java.util.Random;
import plotter.Graphic;
import plotter.LineStyle;
import plotter.Plotter;
public class PiRegen {
public static void main(String[] args) {
int anzahl = 30000;
Graphic graphic = new Graphic("Zufallsregen");
graphic.setBounds(new Rectangle(0,0,500,500));
Plotter plotter = graphic.getPlotter();
plotter.setXrange(0, 1);
plotter.setYrange(0, 1);
plotter.setDataLineStyle(LineStyle.DOT);
plotter.setDataColor("in", Color.RED);
Random random = new Random();
int drin = 0;
for (int n = 0; n < anzahl ; n++) {
double x = random.nextFloat();
double y = random.nextFloat();
if (x * x + y * y < 1) {
plotter.add("in", x, y);
++drin;
} else {
// plotter.add("out", x, y);
}
if( n % 1000 == 0 & n > 0) {
System.out.println( n + " " + 4. * drin / n );
}
}
}
}