Java Source Code for Coin Toss


import java.math.*;
import java.applet.*;
import java.awt.*;
import java.util.Random;

class CoinTossPlot extends Canvas {

        private int max_iterations = 3200;

        private void plot(Graphics g, int x, int y, int iter) {
                
                g.setColor(Color.blue);
                if(iter<400) g.drawLine(x, 50, x, 50-y);
		    else if(iter<800) g.drawLine(x, 100, x, 100-y);
		    else if(iter<1200) g.drawLine(x, 150, x, 150-y);
		    else if(iter<1600) g.drawLine(x, 200, x, 200-y);
		    else if(iter<2000) g.drawLine(x, 250, x, 250-y);
		    else if(iter<2400) g.drawLine(x, 300, x, 300-y);
		    else if(iter<2800) g.drawLine(x, 350, x, 350-y);
		    else if(iter<3200) g.drawLine(x, 400, x, 400-y);
        }

        public void paint(Graphics g) {

            double xn=0., hazard;
            int iter, xplot, yplot, plotColor=0;
		Random ran = new Random();

        
            for (iter=0;iter<=max_iterations;iter++) {

                    hazard = ran.nextDouble();
                    if(hazard<0.5) xn = xn + 1.0;
                    else xn = xn - 1.0;

	            xplot = 10+iter%400;
                    yplot = (int)xn;
                    plot(g,xplot,yplot,iter);
                }
        }
        } 

        public class CoinToss extends Applet {

                private CoinTossPlot canvas;

                public boolean handleEvent(Event e) {
                        if(e.id==Event.WINDOW_DESTROY) System.exit(0);
                        return super.handleEvent(e);
                }
                
                public void init() {

                        setLayout(new BorderLayout());
                        canvas = new CoinTossPlot();
                        add("Center", canvas);
                        }

        }