import java.util.Scanner; class HW2p7 { static double pdfOld=-1.; static double xOld=0.; public static void main(String[] args) { Scanner sc=new Scanner(System.in); //********************************************************************** // * // Get the number of histories desired * // * //********************************************************************** while(true) { System.out.println("\n Number of bins?"); int nbin=sc.nextInt(); System.out.println("\n Lower limit of bins?"); double a=sc.nextDouble(); System.out.println("\n Upper limit of bins?"); double b=sc.nextDouble(); double dx=(b-a)/(double)nbin; int n=0; System.out.println("\n Number of histories?"); int ntry=sc.nextInt(); if(ntry < 0)System.exit(0); if(ntry >0)n=ntry; //********************************************************************** // * // Initialize the two bins we will be collecting information in. * // * //********************************************************************** double[] total_score=new double[nbin]; double[] total_squared=new double[nbin]; //********************************************************************** // * // For each history: * // * //********************************************************************** for(int ihistory=0;ihistory-1 && ibin < nbin) { total_score[ibin]+=1.; total_squared[ibin]+=1.*1.; } } //********************************************************************** // * // After all histories have been run: * // * // Find the estimate of the average * // * //********************************************************************** for(int i=0;i %4$10.5f +/- %5$10.5f (%6$6.3f percent)\n", (i+1),i*dx,(i+1)*dx,xhat*nbin,mean_sd*nbin,mean_sd/xhat*100.); } } } //********************************************************************** //********************************************************************** static double getX() { double a=0.; double b=Math.PI; double xtry=Math.random()*(b-a)+a; double ret=xtry; double pdfNew=2.*xtry+Math.sin(xtry); //********************************************************************** // * // Rule 1 if new PDF is greater, go to it * // * //********************************************************************** if(pdfNew>pdfOld) { ret=xtry; pdfOld=pdfNew; } else { //********************************************************************** // * // Rule 2 if not, go to new PDF with probability pdfNew/pdfOld * // * //********************************************************************** if(Math.random()