home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-20 | 2.3 KB | 106 lines |
-
- package simula.random;
-
- import java.util.Random;
-
- /**
- * The class Erlang allows to generate psudo-random real values distributed
- * like the sum of a certain amount of exponential random variables.
- */
- public class Erlang extends Generator {
- /**
- ** Number of exponentials.
- */
- private int b;
-
- /**
- * Parameter of the exponentials.
- */
- private double lambda;
- private Negexp exp;
-
-
- /**
- * Creates a new Erlang generator using the standard engine and the current time as seed.
- * @param n Number of exponential random variables to be summed.
- * @param l Parameter of the exponentials.
- */
- public Erlang(int n,int l)
- {
- b = n;
- lambda = l;
-
- random = new RandomEngineJava();
-
- exp = new Negexp(random,lambda);
- }
- /**
- * Creates a new Erlang generator using the standard engine with the given seed.
- * @param seed Initial seed for the pseudo-random engine.
- * @param n Number of exponential random variables to be summed.
- * @param l Parameter of the exponentials.
- */
- public Erlang(long seed,int n,int l)
- {
- random = new RandomEngineJava(seed);
- b = n;
- lambda = l;
-
- exp = new Negexp(random,lambda);
- }
- /**
- * Creates a new Erlang generator using the given engine.
- * @param e Pseudo-random bit generator.
- * @param n Number of exponential random variables to be summed.
- * @param l Parameter of the exponentials.
- */
- public Erlang(RandomEngine e,int n,int l)
- {
- b = n;
- lambda = l;
-
- random = e;
-
- exp = new Negexp(e,lambda);
- }
- /**
- * Draws a value from the generator.
- * @return An instance of Value that encapsulates a real value.
- * @see RealValue
- * @see Value
- */
- public Value draw()
- {
- int i;
- double sum = 0.0;
- try {
- for(i=0;i<b;i++)
- sum += exp.draw().asReal();
- }
- catch(TypeDrawnException e)
- {
- System.out.println("E' accaduto l'impossibile");
- }
- return new RealValue( sum );
-
- }
- /**
- * Change the number of exponentials and their parameter.
- * @param n Number of exponential random variables to be summed.
- * @param l Parameter of the exponentials.
- */
- public void setParam(int n,double l)
- {
- b = n;
- lambda = l;
-
- exp = new Negexp(random,lambda);
- }
- /**
- * Returns the type of the extracted value.
- */
- public int type()
- {
- return REAL_TYPE;
- }
- }