-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercolationStats.java
67 lines (63 loc) · 2.33 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
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
import edu.princeton.cs.algs4.StdOut;
public class PercolationStats {
private final double[] finished;
private final int ntrials;
public PercolationStats(int n, int trials) // perform trials independent experiments on an n-by-n grid
{
finished = new double[trials];
ntrials = trials;
if (n < 1 || trials < 1)
{
throw new java.lang.IllegalArgumentException();
}
for (int i = 0; i < trials; i++)
{
Percolation pn = new Percolation(n);
double openedSites = 0;
while (!pn.percolates())
{
int row = StdRandom.uniform(1, n + 1);
int col = StdRandom.uniform(1,n + 1);
if (!pn.isOpen(row, col)) {
pn.open(row, col);
openedSites++;
}
}
finished[i] = openedSites * 1.0 / (n * n);
}
/*
for (double i:finished) {
StdOut.println("finished = " + i);
}
*/
}
public double mean() // sample mean of percolation threshold
{
return StdStats.mean(finished);
}
public double stddev() // sample standard deviation of percolation threshold
{
return StdStats.stddev(finished);
}
public double confidenceLo() // low endpoint of 95% confidence interval
{
final double confidence = 1.96;
return mean() - ((confidence * stddev()) / Math.sqrt(ntrials));
}
public double confidenceHi() // high endpoint of 95% confidence interval
{
return mean() + ((1.96 * stddev()) / Math.sqrt(ntrials));
}
public static void main(String[] args) // test client (described below)
{
int num = Integer.parseInt(args[0]);
int trial = Integer.parseInt(args[1]);
PercolationStats ps = new PercolationStats(num, trial);
String confidence = ps.confidenceLo() + ", " + ps.confidenceHi();
StdOut.println("mean = " + ps.mean());
StdOut.println("stddev = " + ps.stddev());
StdOut.println("95% confidence interval = " + confidence);
}
}