import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
/*
*How do I generate a site uniformly at random among all blocked sites for use in PercolationStats?
* Pick a site at random (by using StdRandom to generate two integers between 1 and N)
* and use this site if it is blocked; if not, repeat.
*/
public class PercolationStats {
private int T; //T independent experiments
private double[] fraction; public PercolationStats(int N, int T) { // perform T independent experiments on an N-by-N grid
if (N <= 0 || T <= 0) {
throw new IllegalArgumentException("N and T must be bigger than 0");
}
this.T = T;
fraction = new double[T]; for (int count = 0; count < T; count++) {
Percolation pr = new Percolation(N);
int openedSites = 0;
while (!pr.percolates()) {
int i = StdRandom.uniform(1, N+1);
int j = StdRandom.uniform(1, N+1);
if (!pr.isOpen(i, j)) {
pr.open(i, j);
openedSites++;
}
}
fraction[count] = (double) openedSites / (N * N);
}
} public double mean() { // sample mean of percolation threshold
return StdStats.mean(fraction);
} public double stddev() { // sample standard deviation of percolation threshold
return StdStats.stddev(fraction);
} public double confidenceLo() { // low endpoint of 95% confidence interval
return mean() - 1.96 * stddev() / Math.sqrt(T);
} public double confidenceHi() { // high endpoint of 95% confidence interval
return mean() + 1.96 * stddev() / Math.sqrt(T);
} public static void main(String[] args) // test client (described below)
{
int N = Integer.parseInt(args[0]);
int T = Integer.parseInt(args[1]);
PercolationStats ps = new PercolationStats(N, T);
StdOut.println("mean = " + ps.mean());
StdOut.println("stddev = " + ps.stddev());
StdOut.println("95% confidence interval = "+ps.confidenceLo()+", "+ ps.confidenceHi());
}
}

最新文章

  1. Linux posix线程库总结
  2. remote desktop connect btw Mac, Windows, Linux(Ubuntu) Mac,Windows,Linux之间的远程桌面连接
  3. CSS的一些规范
  4. MongoDB学习笔记05
  5. Tomcat启动失败的解决方法
  6. Eclipse3.6 添加JUnit源代码
  7. (JavaScript插件——下拉菜单)
  8. 将非常规Json字符串转换为常用的json对象
  9. 工作流引擎 Flowable 6.0.0.RC1 release,完全兼容Activi
  10. win10下配置默认软件(转)
  11. Linux查看版本信息
  12. BZOJ4083 : [Wf2014]Wire Crossing
  13. hadoop-mapreduce-(1)-统计单词数量
  14. Hadoop-1.2.1伪分布下 hive-0.10.0内嵌模式安装
  15. Javascript非构造函数的继承
  16. gcc6.3的安装
  17. js 操作数字类型
  18. poj2462
  19. sendsms短信验证功能实现代码
  20. ISE14.7生成.bit文件和mcs文件

热门文章

  1. a标签的背景图在ie8下显示问题
  2. 一次利用MSSQL的SA账户提权获取服务器权限
  3. 那些年,我们一起学WCF--(7)PerSession实例行为
  4. 开发自己的cordova插件
  5. 从XML文件中获取格式化的文本信息
  6. 226. Invert Binary Tree(C++)
  7. JQuery 实现鼠标经过图片高亮显示,其余图片变暗
  8. php之分页类代码
  9. php之文件上传简单介绍
  10. MySql数据库3【优化4】连接设置的优化