Including Random Numbers from Poisson in Application

Including Random Numbers from Poisson in Application

How can I include random numbers drawn from a Poissondistribution in my applications? There is only a Gaussian distribution in java.util.Random.

Well, this one is a little off the beaten track. I don’t know how manypeople routinely need to generate Poisson distributed random numbers, so itisn’t too surprising that there is no standard function for it. (ThePoisson distribution describes probabilities of certain rare events. TheFrench mathematician Poisson developed the mathematical theory whenstudying the frequency of soldiers being killed by horse kicks to theirheads in the Napoleonic army.)

An excellent reference for this kind of problems is “Numerical Recipes inC”, by Press, Teukolsky, Vetterling and Flannery, published by CambridgeUniversity Press.

In the sample program, I implemented a class Poisson. To make Poissondistributed numbers, simply make an object of this class, giving the meanin the constructor:

        Poisson p = new Poisson(5);        

Then keep calling        

to get the random numbers.

Here is an applet that gives a visual idea of the Poisson distribution.Enter a mean (such as 5, 20, or 50) in the “Mean field”, click on”Generate” and wait a short while. You will get a histogram of 1,000trials, showing how many times the numbers 1, 2, 3, …, 100 were generatedby the Poisson process.

import java.applet.*;import java.awt.*;public class PoissonApplet extends Applet{  public void init()   {  graph = new HistoCanvas();      setLayout(new BorderLayout());      Panel p = new Panel();      mean = new TextField("20", 10);      p.add(new Label("Mean: "));      p.add(mean);      p.add(new Button("Generate"));      add("North", p);      add("Center", graph);   }   public void start()   {  generate();   }   public boolean action(Event evt, Object arg)   {  if (arg.equals("Generate"))      {  generate();      }      else return super.action(evt, arg);      return true;   }   public void generate()   {  int[] v = new int[100];      try      {  float m = Float.valueOf(mean.getText().trim()).floatValue();         Poisson p1 = new Poisson(m);         for (int i = 0; i  g);         return e;      }      else      {  float e;         float t;         do         {  float y;            do            {  y = (float)Math.tan(Math.PI * Math.random());               e = sq * y + mean;            } while (e  t);         return (int)e;      }   }   private static float loggamma(float xx)   {  double x = xx;      double y = x;      double tmp = x + 5.5;      tmp -= (x + 0.5) * Math.log(tmp);      double ser = 1.000000000190015;      for (int i = 0; i = 0)         {  y1 = (int)((maxValue - v) * scale);            height = yOrigin - y1;         }         else         {  y1 = yOrigin;            height = (int)(-v * scale);         }         g.setColor(;         g.fillRect(x1, y1, barWidth - 2, height);         g.setColor(;         g.drawRect(x1, y1, barWidth - 2, height);      }   }   private int[] info = null;   private int minValue;   private int maxValue;}


Share the Post: