//GEN-BEGIN:header
import chess.*;
import java.awt.*;

/**
 * CalculatePiRandom.java
 *
 * Created on June 17, 2006, 5:36 AM by Jeremy
 */
public class CalculatePiRandom {
    public static void main(String[] args) {//GEN-END:header
        Screen screen= Chess.newScreen(400,400);
        
        long insideCount;
        long totalCount;
        
        insideCount= 0;
        totalCount= 0;
        while ( true ) {
            double x= Math.random();
            double y= Math.random();
            
            totalCount= totalCount + 1;
            
            double dist= Math.sqrt( Math.pow( x-0.5, 2 ) + Math.pow( y-0.5, 2 ) );
            if ( dist < 0.5 ) {
                insideCount= insideCount + 1;
                screen.setColor( Color.red );
            } else {
                screen.setColor( Color.blue );
            }
            screen.drawRect( x*400, y*400, 1, 1 );
            
            /* pi is the ratio between the area of the circle 
             * ( pi * (0.5)^2 => 0.25 * pi) and square ( 1 ).  The chance of
             * the random number being within the circle is proportional to 
             * the area, so the ratio of the counts is the same as the ratio
             * of the areas.
             */
            double pi= 4. * insideCount / totalCount;
            screen.report( "pi=" + pi );
        }
        
    } //main//GEN-BEGIN:footer
    
} //class
//GEN-END:footer
