home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / JSL.ZIP / JSL20 / simula / random / Poisson.java < prev    next >
Encoding:
Java Source  |  1998-02-20  |  2.2 KB  |  103 lines

  1.  
  2. package simula.random;
  3.  
  4. import java.util.Random;
  5.  
  6. /**
  7.  * The class Poisson allows to generate integer values according to the Poisson
  8.  * distribution.
  9.  */
  10. public class Poisson extends Generator
  11. {
  12. /**
  13. ** Parameter of the Poisson distribution.
  14. */
  15.    private double lambda;
  16.  
  17.  
  18. /**
  19.  * Creates a new Poisson generator using the standard engine and the current time as seed.
  20.  * @param l    Parameter of the Poisson random variable.
  21. */
  22.    public Poisson(double l)
  23.    {
  24.      lambda = l;
  25.      random = new RandomEngineJava();
  26.    }   
  27. /**
  28.  * Creates a new Poisson generator using the standard engine and the given seed.
  29. ** @param seed Initial seed for the pseudo-random engine.
  30. ** @param l    Parameter of the Poisson random variable.
  31. */
  32.    public Poisson(long seed,double l)
  33.    {
  34.      lambda = l;
  35.      random = new RandomEngineJava(seed);
  36.    }   
  37. /**
  38.  * Creates a new Poisson generator using the given engine.
  39. ** @param e    Pseudo-random bit generator.
  40.  * @param l    Parameter of the Poisson random variable.
  41. */
  42.    public Poisson(RandomEngine e,double l)
  43.    {
  44.      lambda = l;
  45.      random = e;
  46.    }   
  47. /**
  48. ** Returns the value of the density in the given point.
  49. ** @param x     Point to value the density of.
  50. */
  51.   public double density (int x)
  52.   {
  53.      double res=0;    
  54.      int fact=1;
  55.      int i; 
  56.      
  57.   
  58.      if (x >= 0) {
  59.        for (i=2;i<=x;i++)
  60.          fact*=i;
  61.        res=Math.pow(Math.E,-lambda)*Math.pow(lambda,x)/fact;
  62.      }
  63.      return res;
  64.   }  
  65. /**
  66.  * Draws a value from the generator.
  67.  * @return An instance of Value that encapsulates an integer value.
  68.  * @see IntegerValue
  69.  * @see Value
  70.  */
  71.    public Value draw()
  72.    {
  73.      double r = random.nextDouble();
  74.      int i = 0;
  75.      double p = Math.exp(-lambda);     
  76.  
  77.      long fact = 1; // fattoriale di 0
  78.      
  79.      while (p < r)
  80.      {
  81.        i++;
  82.        fact *= i;
  83.        p += Math.exp(-lambda)*Math.pow(lambda,(double)i/fact);
  84.      }
  85.      
  86.      return new IntegerValue( i );
  87.    }   
  88. /**
  89.  * Change the Poisson parameter.
  90.  * @param l    Parameter of the Poisson random variable.
  91. */
  92.    public void setParam(double l)
  93.    {
  94.      lambda = l;
  95.    }   
  96. /**
  97.  * Returns the type of the extracted value.
  98.  */
  99.    public int type()
  100.    {
  101.          return INTEGER_TYPE;
  102.    }   
  103. }