-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathPercolationStats.java
More file actions
46 lines (45 loc) · 1.52 KB
/
Copy pathPercolationStats.java
File metadata and controls
46 lines (45 loc) · 1.52 KB
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
public class PercolationStats {
private double [] attemps;
// perform T independent computational experiments on an N-by-N grid
public PercolationStats(int N, int T){
if (N<=0||T<=0) throw new IllegalArgumentException();
attemps=new double [T];
for(int i=0;i<T;i++){
Percolation perc=new Percolation(N);
int steps=0;
while(!perc.percolates()){
int row=StdRandom.uniform(N)+1;
int column=StdRandom.uniform(N)+1;
if(!perc.isOpen(row,column)){
perc.open(row,column);
steps++;
}
}
//todo
attemps[i]=(double)steps/(N*N);
}
}
// sample mean of percolation threshold
public double mean(){
return StdStats.mean(attemps);
}
// sample standard deviation of percolation threshold
public double stddev(){
return StdStats.stddev(attemps);
}
// returns lower bound of the 95% confidence interval
public double confidenceLo(){
return mean()-((1.96*stddev())/Math.sqrt(attemps.length));
}
// returns upper bound of the 95% confidence interval
public double confidenceHi(){
return mean()+((1.96*stddev())/Math.sqrt(attemps.length));
}
// test client, described below
public static void main(String[] args){
PercolationStats ps=new PercolationStats(300,1000);
StdOut.print("mean = "+ps.mean()+"\n");
StdOut.print("std dev = "+ps.stddev()+"\n");
StdOut.print("95% confidence interval = "+ps.confidenceLo()+", "+ps.confidenceHi());
}
}