-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercolationStats.java
80 lines (71 loc) · 2.24 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
69
70
71
72
73
74
75
76
77
78
79
80
package lab1;
/**
* Created by wcf on 15-9-19.
*/
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
public class PercolationStats {
private int pN;
private int times;
private double[] record;
// perform T independent experiments on an N-by-N gird
public PercolationStats(int N, int T) {
if (N <= 0 || T <= 0) {
throw new IllegalArgumentException();
}
pN = N;
times = T;
record = new double[T];
result();
}
//every time the percolation's result
private double once() {
int count = 0;
int rol, col;
Percolation pField = new Percolation(pN);
while (true) {
if (count > 0 && pField.percolates()) {
break;
}
rol = StdRandom.uniform(1, pN + 1);
col = StdRandom.uniform(1, pN + 1);
if (!pField.isOpen(rol, col)) {
pField.open(rol, col);
count++;
}
}
return count * 1.0 / (pN * pN);
}
//the record of the simulation
private void result() {
for (int i = 0; i < times; i++) {
record[i] = once();
}
}
// sample mean of percolation threshold
public double mean() {
return StdStats.mean(record);
}
// sample standard deviation of percolation threshold
public double stddev() {
return StdStats.stddev(record);
}
// low endpoint of 95% confidence interval
public double confidenceLo() {
return mean() - 1.96 * stddev() / Math.sqrt(times);
}
// high endpoint of 95% confidence interval
public double confidenceHi() {
return mean() + 1.96 * stddev() / Math.sqrt(times);
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
int T = Integer.parseInt(args[1]);
PercolationStats sample = new PercolationStats(N, T);
StdOut.println("mean = " + sample.mean());
StdOut.println("stddev = " + sample.stddev());
StdOut.println("95% confidence interval = " + sample.confidenceLo() + ", "
+ sample.confidenceHi());
}
}