-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPercolationStats.java
More file actions
77 lines (65 loc) · 2.36 KB
/
PercolationStats.java
File metadata and controls
77 lines (65 loc) · 2.36 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
package A01Percolation;
import edu.princeton.cs.introcs.StdRandom;
import edu.princeton.cs.introcs.StdStats;
/**
* Created by Adam Gardner on 8/31/16.
*/
public class PercolationStats {
private double[] threshold;
private int runTimes, size;
/*
assigns threshold[] the percThreshold values for each instance of A01Percolation that is ran.
*/
public PercolationStats(int N, int T) {
if (N <= 0 || T <= 0) {
throw new IllegalArgumentException("The first parameter value is the size of the & T must be more than 0, " +
"preferably higher than 3.");
}
runTimes = T;
size = N;
threshold = new double[runTimes];
for (int i = 0; i < T; i++) {
threshold[i] = percThreshold();
}
}
public double mean() {
return StdStats.mean(threshold);
}
public double stddev() {
return StdStats.stddev(threshold);
}
public double confidenceLow() {
return mean() - ((1.96*stddev()/Math.sqrt(runTimes)));
}
public double confidenceHigh() {
return mean() + ((1.96*stddev()/Math.sqrt(runTimes)));
}
/*
Creates instances of A01Percolation and returns the ratio of the amount of sites that were open before percolation
is true and divides it by the amount n X n grid locations.
*/
public double percThreshold() {
int row, col;
int count = 0;
Percolation percolation = new Percolation(size);
while (!percolation.percolates()) {
row = StdRandom.uniform(size) + 1;
col = StdRandom.uniform(size) + 1;
if(!percolation.isOpen(row, col)) {
percolation.open(row, col);
count++;
}
}
return count / (size*size + 2);
}
public static void main(String[] args) {
int size = 200;
int runtimes = 100;
PercolationStats percolationStats = new PercolationStats(size, runtimes);
System.out.println("values after creating PercolationStats(size, runtimes)");
System.out.printf("mean: %f\n", percolationStats.mean());
System.out.printf("stddev: %f\n", percolationStats.stddev());
System.out.printf("confidenceLow: %f\n", percolationStats.confidenceLow());
System.out.printf("confidenceHigh: %f", percolationStats.confidenceHigh());
}
}