home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-20 | 2.2 KB | 103 lines |
-
- package simula.random;
-
- import java.util.Random;
-
- /**
- * The class Poisson allows to generate integer values according to the Poisson
- * distribution.
- */
- public class Poisson extends Generator
- {
- /**
- ** Parameter of the Poisson distribution.
- */
- private double lambda;
-
-
- /**
- * Creates a new Poisson generator using the standard engine and the current time as seed.
- * @param l Parameter of the Poisson random variable.
- */
- public Poisson(double l)
- {
- lambda = l;
- random = new RandomEngineJava();
- }
- /**
- * Creates a new Poisson generator using the standard engine and the given seed.
- ** @param seed Initial seed for the pseudo-random engine.
- ** @param l Parameter of the Poisson random variable.
- */
- public Poisson(long seed,double l)
- {
- lambda = l;
- random = new RandomEngineJava(seed);
- }
- /**
- * Creates a new Poisson generator using the given engine.
- ** @param e Pseudo-random bit generator.
- * @param l Parameter of the Poisson random variable.
- */
- public Poisson(RandomEngine e,double l)
- {
- lambda = l;
- random = e;
- }
- /**
- ** Returns the value of the density in the given point.
- ** @param x Point to value the density of.
- */
- public double density (int x)
- {
- double res=0;
- int fact=1;
- int i;
-
-
- if (x >= 0) {
- for (i=2;i<=x;i++)
- fact*=i;
- res=Math.pow(Math.E,-lambda)*Math.pow(lambda,x)/fact;
- }
- return res;
- }
- /**
- * Draws a value from the generator.
- * @return An instance of Value that encapsulates an integer value.
- * @see IntegerValue
- * @see Value
- */
- public Value draw()
- {
- double r = random.nextDouble();
- int i = 0;
- double p = Math.exp(-lambda);
-
- long fact = 1; // fattoriale di 0
-
- while (p < r)
- {
- i++;
- fact *= i;
- p += Math.exp(-lambda)*Math.pow(lambda,(double)i/fact);
- }
-
- return new IntegerValue( i );
- }
- /**
- * Change the Poisson parameter.
- * @param l Parameter of the Poisson random variable.
- */
- public void setParam(double l)
- {
- lambda = l;
- }
- /**
- * Returns the type of the extracted value.
- */
- public int type()
- {
- return INTEGER_TYPE;
- }
- }