-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPercolationStats.java
More file actions
90 lines (79 loc) · 1.7 KB
/
PercolationStats.java
File metadata and controls
90 lines (79 loc) · 1.7 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
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
81
82
83
84
85
86
87
88
89
90
public class PercolationStats
{
private Percolation pc;
private double[] count;
private static int T;
private static int N;
private double mean;
private double stddev;
private double confidenceLo;
private double confidenceHi;
public PercolationStats(int N,int T)
{
this.T = T;
this.N = N;
count = new double[T];
int row;
int column;
double MC_times;
for(int i = 0; i<T; i++)
{
pc = new Percolation(N);
MC_times = 0;
while( !pc.percolates() )
{
row = StdRandom.uniform(1,N+1);
column = StdRandom.uniform(1,N+1);
if( pc.isOpen(row,column) ) continue;
else
{
pc.open(row,column);
MC_times++;
}
count[i] = MC_times/(N*N);
}
}
}
public double mean()
{
double sum = 0;
for(int i = T-1; i>=0 ; i--)
{
sum += count[i];
}
this.mean = sum/T;
return mean;
}
public double stddev()
{
double sum = 0;
for(int i = T-1; i>=0 ; i--)
{
sum = sum + (count[i] - mean) * (count[i] - mean);
}
this.stddev = Math.sqrt(sum/(T-1));
return stddev;
}
public double confidenceLo()
{
this.confidenceLo = mean - (1.96 * stddev)/Math.sqrt(T);
return confidenceLo;
}
public double confidenceHi()
{
this.confidenceHi = mean + (1.96 * stddev)/Math.sqrt(T);
return confidenceHi;
}
public static void main(String[] args)
{
int n = Integer.parseInt(args[0]);
int t = Integer.parseInt(args[1]);
PercolationStats pc = new PercolationStats(n,t);
StdOut.print(" mean = " + pc.mean());
StdOut.println();
StdOut.print(" stddev = " + pc.stddev());
StdOut.println();
StdOut.print(" 95% confidence interval = " + pc.confidenceLo() + " " + pc.confidenceHi());
StdOut.println();
}
}