-
Notifications
You must be signed in to change notification settings - Fork 14
/
PercolationStats.java
68 lines (53 loc) · 2.06 KB
/
PercolationStats.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class PercolationStats {
private double[] threshold;
// perform T independent computational experiments on an N-by-N grid
public PercolationStats(int N, int T) {
int openCount, row, column;
if (N <= 0 || T <= 0)
throw new IllegalArgumentException("Arguments out of bound");
threshold = new double[T];
openCount = 0;
for (int i = 0; i < T; i++) {
Percolation pl = new Percolation(N);
do {
row = StdRandom.uniform(1, N+1);
column = StdRandom.uniform(1, N+1);
if (pl.isOpen(row, column))
continue;
pl.open(row, column);
openCount++;
} while (!pl.percolates());
threshold[i] = (double) openCount / (N * N);
openCount = 0;
//System.out.printf("threshold[%03d] = %f\n", i, threshold[i]);
}
}
// sample mean of percolation threshold
public double mean() {
return StdStats.mean(threshold);
}
// sample standard deviation of percolation threshold
public double stddev() {
return StdStats.stddev(threshold);
}
private double halfInterval() {
return 1.96 * stddev() / Math.sqrt(threshold.length);
}
// returns lower bound of the 95% confidence interval
public double confidenceLo() {
return mean() - halfInterval();
}
// returns upper bound of the 95% confidence interval
public double confidenceHi() {
return mean() + halfInterval();
}
// test client, described below
public static void main(String[] args) {
PercolationStats pls = new PercolationStats(Integer.parseInt(args[0]),
Integer.parseInt(args[1]));
System.out.printf("mean = %f\n", pls.mean());
System.out.printf("stddev = %f\n", pls.stddev());
System.out.printf("95%% confidence Interval = %f, %f\n",
pls.confidenceLo(), pls.confidenceHi());
}
}