Monte Carlo Java Calculating the value of pi

MonteCarlo Java

Monte Carlo. Can’t get enough of it,  so here’s the Java version of the program that calculates value of pi with Monte Carlo.

Have done it in Fortran using pgplot as the GUI engine. You can see it action here.

See the Java applet in action

One thing though, the Java program takes more time to converge on the  value of pi than the Fortran. Maybe my rusty Java program.


Neutron Collisions, Stanislaw Ulam , Solitares and my Pgplot practice.


Java code for calculating the value of pi using Monte Carlo.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MonteCarlo extends JApplet implements Runnable  {

    private static final long serialVersionUID = 1L;

    int width,height,pcnt=1,pin=1;
    double pival,rad=100.0,radi;
    double eps=0.000001,krad;
    double diff;
    int ranx,rany;
    Thread t = null;
    boolean threadSuspended;
    Image offscreenImage;
    Graphics offscreenGraphics;
    int xbar=2,ybar=2,barwid,barheigt=21;
    double bartop =3.04;
    double barbot = 3.21;
    double barmid = 3.14;
    double barval;
    int line;

   public void init(){
       setSize((int)(2*rad),(int) (2*rad+25));

       width= getSize().width;
       height = getSize().height;
       barwid = width -4;
       offscreenImage = createImage(width,height);
       offscreenGraphics = offscreenImage.getGraphics();


   public void start() {
          if ( t == null ) {
             t = new Thread( this );
             t.setPriority( Thread.MIN_PRIORITY );
             threadSuspended = false;
          else {
             if ( threadSuspended ) {
                threadSuspended = false;
                synchronized( this ) {

   public void stop() {
          threadSuspended = true;
public void paint(Graphics g){

         int x0,y0,tick=1;

          ranx=x0+(int) (2*rad*Math.random()-rad);
          rany=y0+ (int) (2*rad*Math.random()-rad);


          offscreenGraphics.drawLine(ranx, rany, ranx, rany);
          radi=Math.pow(ranx-x0, 2.0)+Math.pow(rany-y0, 2.0);

          if(radi < krad){
              offscreenGraphics.drawLine(ranx, rany, ranx, rany);
              pival=4.0 * (double)pin/pcnt;




          offscreenGraphics.fillRect(xbar, ybar, barwid, barheigt);
          offscreenGraphics.drawString(""+bartop, xbar, ybar+8);
          offscreenGraphics.drawString(""+barbot, barwid-22, ybar+8);
          offscreenGraphics.drawString(""+barmid, (barwid-22)/2, ybar+8);

          barval = ((barwid-xbar-22)/(barbot-bartop))*(pival-bartop)  ;
              line=(int) barval;
          offscreenGraphics.drawLine(line, ybar, line,barheigt);

           g.drawImage(offscreenImage, 0, 0, this);

public void run() {
    try {

    } catch (InterruptedException e) {
        // TODO: handle exception


    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub


    private void pause(int pauseLength) {
        try { Thread.sleep(pauseLength); } catch (InterruptedException e) {}



3 thoughts on “Monte Carlo Java Calculating the value of pi

  1. Pingback: An Excel App for Your Brain! |

  2. Pingback: Pick-up Sticks |

  3. Pingback: A Simple Fortran code to Simulate Stochastic Stock Price Movement |

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s