-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercolationStats.java
59 lines (52 loc) · 1.51 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
import java.io.*;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
import edu.princeton.cs.algs4.Stopwatch;
import java.util.*;
public class PercolationStats {
private Percolation per;
private int expers;
private int s;
private double[] pro;
public PercolationStats(int N,int T){
s=N;
expers=T;
if(s<=0||expers<=0) throw new IllegalArgumentException();
pro=new double[expers];
for(int times=0;times<expers;times++){
per=new Percolation(s);
int opensites=0;
while(!per.percolates()){
int i=StdRandom.uniform(1,N+1);
int j=StdRandom.uniform(1,N+1);
if(!per.isOpen(i, j)){
per.open(i, j);
opensites++;
}
}
pro[times]=(double)opensites/(s*s);
}
}
public double mean(){
return StdStats.mean(pro);
}
public double stddev(){
return StdStats.stddev(pro);
}
public double confidenceLo(){
return mean()-(1.96*stddev())/Math.sqrt(expers);
}
public double confidenceHi(){
return mean()+(1.96*stddev())/Math.sqrt(expers);
}
public static void main(String[] args){
int N=Integer.parseInt(args[0]);
int T=Integer.parseInt(args[1]);
Stopwatch watch=new Stopwatch();
PercolationStats perstas=new PercolationStats(N,T);
System.out.println("mean ="+perstas.mean());
System.out.println("stddev ="+perstas.stddev());
System.out.println("95% confidence interval ="+perstas.confidenceLo()+","+perstas.confidenceHi());
System.out.println("Running time ="+watch.elapsedTime());
}
}