Algorithmsi Programming Assignment 1:percolationstats.java

Source: Internet
Author: User

ImportEdu.princeton.cs.algs4.StdOut; ImportEdu.princeton.cs.algs4.StdRandom;ImportEdu.princeton.cs.algs4.StdStats; /**how do I generate a site uniformly at the random among all blocked sites for use in Percolationstats? * Pick a site at random (by using stdrandom to generate, integers between 1 and N) * and use this site if it is Blocke D  If not, repeat. */ Public classPercolationstats {Private intT//T Independent Experiments   Private Double[] fraction;  PublicPercolationstats (intNintT) {//perform T independent experiments on an n-by-n grid       if(N <= 0 | | T <= 0) {           Throw NewIllegalArgumentException ("N and T must be bigger than 0"); }        This. T =T; Fraction=New Double[T];  for(intCount = 0; Count < T; count++) {percolation PR=Newpercolation (N); intOpenedsites = 0;  while(!pr.percolates ()) {               inti = Stdrandom.uniform (1, n+1); intj = Stdrandom.uniform (1, n+1); if(!Pr.isopen (i, J))                   {Pr.open (I, j); Openedsites++; }} Fraction[count]= (Double) Openedsites/(N *N); }   }      Public DoubleMean () {//sample mean of percolation threshold       returnStdstats.mean (fraction); }       Public DoubleStdDev () {//sample standard deviation of percolation threshold       returnStdstats.stddev (fraction); }       Public DoubleConfidencelo () {//Low endpoint of 95% confidence interval       returnMean ()-1.96 * StdDev ()/math.sqrt (T); }       Public DoubleConfidencehi () {//High endpoint of 95% confidence interval       returnMean () + 1.96 * StdDev ()/math.sqrt (T); }       Public Static voidMain (string[] args)//test Client (described below)   {       intN = Integer.parseint (args[0]); intT = Integer.parseint (args[1]); Percolationstats PS=Newpercolationstats (N, T); Stdout.println ("mean =" +Ps.mean ()); Stdout.println ("StdDev =" +Ps.stddev ()); Stdout.println ("95% Confidence interval =" +ps.confidencelo () + "," +Ps.confidencehi ()); }}

Algorithmsi Programming Assignment 1:percolationstats.java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.