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

  1.  
  2. package simula.random;
  3.  
  4. import java.util.Random;
  5.  
  6. /**
  7.  * The class Erlang allows to generate psudo-random real values distributed
  8.  * like the sum of a certain amount of exponential random variables.
  9.  */
  10. public class Erlang extends Generator {
  11. /**
  12. ** Number of exponentials.
  13. */  
  14.   private int b;
  15.  
  16. /**
  17.  * Parameter of the exponentials.
  18.  */
  19.   private double lambda;
  20.   private Negexp exp;
  21.  
  22.  
  23. /**
  24.  * Creates a new Erlang generator using the standard engine and the current time as seed.
  25.  * @param n    Number of exponential random variables to be summed.
  26.  * @param l    Parameter of the exponentials.
  27.  */
  28.   public Erlang(int n,int l)
  29.   {
  30.     b = n;
  31.     lambda = l;
  32.  
  33.     random = new RandomEngineJava();
  34.  
  35.     exp = new Negexp(random,lambda);
  36.   }  
  37. /**
  38.  * Creates a new Erlang generator using the standard engine with the given seed.
  39.  * @param seed Initial seed for the pseudo-random engine.
  40.  * @param n    Number of exponential random variables to be summed.
  41.  * @param l    Parameter of the exponentials.
  42.  */
  43.   public Erlang(long seed,int n,int l)
  44.   {
  45.     random = new RandomEngineJava(seed);
  46.     b = n;
  47.     lambda = l;
  48.  
  49.     exp = new Negexp(random,lambda);
  50.   }  
  51. /**
  52.  * Creates a new Erlang generator using the given engine.
  53.  * @param e    Pseudo-random bit generator.
  54.  * @param n    Number of exponential random variables to be summed.
  55.  * @param l    Parameter of the exponentials.
  56.  */
  57.   public Erlang(RandomEngine e,int n,int l)
  58.   {
  59.     b = n;
  60.     lambda = l;
  61.  
  62.     random = e;
  63.  
  64.     exp = new Negexp(e,lambda);
  65.   }  
  66. /**
  67.  * Draws a value from the generator.
  68.  * @return An instance of Value that encapsulates a real value.
  69.  * @see RealValue
  70.  * @see Value
  71.  */
  72.   public Value draw()
  73.   {
  74.     int i;
  75.     double sum = 0.0;
  76.     try {
  77.       for(i=0;i<b;i++)
  78.         sum += exp.draw().asReal();
  79.     }
  80.     catch(TypeDrawnException e)
  81.     {
  82.       System.out.println("E' accaduto l'impossibile");
  83.     }
  84.     return new RealValue( sum );
  85.    
  86.   }  
  87. /**
  88.  * Change the number of exponentials and their parameter.
  89.  * @param n    Number of exponential random variables to be summed.
  90.  * @param l    Parameter of the exponentials.
  91.  */
  92.   public void setParam(int n,double l)
  93.   {
  94.     b = n;
  95.     lambda = l;
  96.  
  97.     exp = new Negexp(random,lambda);
  98.   }  
  99. /**
  100.  * Returns the type of the extracted value.
  101.  */
  102.   public int type()
  103.   {
  104.       return REAL_TYPE;
  105.   }  
  106. }