All pastes #2109865 Raw Edit

Something

public text v1 · immutable
#2109865 ·published 2012-02-05 06:51 UTC
rendered paste body
import java.util.*;

public class Tester
{
  public static void main(String args[])
  {
    double  cirCount=0;
    double sqrCount=0;
    double a=0,b=0;
    boolean c = false;
    
    
    MonteCarlo mcObj = new MonteCarlo(5,3,2);
    for(int i=0;i<1000000;i++)
    {

      a=mcObj.nextRainDrop_x();
      b=mcObj.nextRainDrop_y();
      c=mcObj.insideCircle(a,b);
      if(c==true)
        cirCount++;
      sqrCount++;
      
      
    }
   
    
    double side = Math.pow(MonteCarlo.r,2);
    double n=(cirCount * (Math.pow(side,2)));
    double m=(sqrCount*(Math.pow(MonteCarlo.r,2)));
    double pi = n/m;          
    System.out.println(pi);
                                         
  }
}


public class MonteCarlo
{
  
  public MonteCarlo(double side_x, double side_y, double radius)
  {
    h=side_x;
    k=side_y;
    r=radius;
    
  }
  
  public double nextRainDrop_x()
  {
    double x_min = h-r;
    double x_max = h+r;
    double range_x = x_max - x_min;
    double pos_x = x_min + range_x *rndm.nextDouble();
    return pos_x;
       
  }

  public double nextRainDrop_y()
  {
    double y_min = k-r;
    double y_max = k+r;
    double range_y = y_max - y_min;
    double pos_y = y_min + range_y * rndm.nextDouble();
    return pos_y;
    
  }
  
  public boolean insideCircle(double x,double y)
  {
    double fpos_x = nextRainDrop_x();
    double fpos_y = nextRainDrop_y();
    
    double a = fpos_x-h;
    double aa = Math.pow(a,2);
    double b = fpos_y-k;
    double bb = Math.pow(b,2);
    double rad = Math.pow(r,2);
      
    if(aa + bb <= rad)
       return true;
    else
       return false;
  }
  
    
  
 public static double h;
 public static double k;
 public static double r;
 
 private Random rndm = new Random();
 
}