public static int nextPoisson(double xm){
  double em;
  double t, y;
  if (xm < 12.0) {
    if (xm != oldm) {
      oldm=xm;
      g=Math.exp(-xm);
    }
    em=-1.0;
    t=1.0;
    do {
      ++em;
      t*=MathUtils.nextDouble();
    }
 while (t > g);
  }
 else {
    if (xm != oldm) {
      oldm=xm;
      sq=Math.sqrt(2.0 * xm);
      alxm=Math.log(xm);
      g=xm * alxm - gammln(xm + 1.0);
    }
    do {
      do {
        y=Math.tan(Math.PI * MathUtils.nextDouble());
        em=sq * y + xm;
      }
 while (em < 0.0);
      em=Math.floor(em);
      t=0.9 * (1.0 + y * y) * Math.exp(em * alxm - gammln(em + 1.0) - g);
    }
 while (MathUtils.nextDouble() > t);
  }
  return (int)em;
}

public static int nextPoisson ( double xm ) { double em ; double t , y ; if ( xm < 12.0 ) { if ( xm != oldm ) { oldm = xm ; g = Math . exp ( - xm ) ; } em = - 1.0 ; t = 1.0 ; do { ++ em ; t *= MathUtils . nextDouble ( ) ; } while ( t > g ) ; } else { if ( xm != oldm ) { oldm = xm ; sq = Math . sqrt ( 2.0 * xm ) ; alxm = Math . log ( xm ) ; g = xm * alxm - gammln ( xm + 1.0 ) ; } do { do { y = Math . tan ( Math . PI * MathUtils . nextDouble ( ) ) ; em = sq * y + xm ; } while ( em < 0.0 ) ; em = Math . floor ( em ) ; t = 0.9 * ( 1.0 + y * y ) * Math . exp ( em * alxm - gammln ( em + 1.0 ) - g ) ; } while ( MathUtils . nextDouble ( ) > t ) ; } return ( int ) em ; }
returns an integer value that is a random deviate drawn from a poisson distribution of mean xm .
