home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-20 | 1.7 KB | 85 lines |
-
- package simula.random;
-
- import java.util.Random;
-
- /**
- ** The class Uniform inherits from Generator and is useful to draw numbers with
- ** uniform probability in the real segment [a,b].
- */
- public class Uniform extends Generator
- {
- /**
- ** segment lower bound.
- */
- private double a;
-
- /**
- ** segment upper bound
- */
- private double b;
-
-
- /**
- ** @param a segment lower bound
- ** @param b segment upper bound
- */
- public Uniform(double a,double b)
- {
- random = new RandomEngineJava();
- setParam(a,b);
- }
- /**
- ** @param seed seed for the Java Random generator
- ** @param a segment lower bound
- ** @param b segment upper bound
- */
- public Uniform(long seed,double a,double b)
- {
- random = new RandomEngineJava(seed);
- setParam(a,b);
- }
- /**
- ** @param e Random Generator Engine instance
- ** @param a segment lower bound
- ** @param b segment upper bound
- */
- public Uniform(RandomEngine e,double a,double b)
- {
- random = e;
- setParam(a,b);
- }
- /**
- ** Return the value of the density in the given point
- ** @param x point to sample for density
- */
- public double density (double x)
- {
- double res=0;
-
- if ((x>=a) & (x<=b))
- res=1/(b-a);
- return res;
- }
- /**
- ** Draws a value with a uniform probability in real segment [a,b]
- ** @return an instance of Value containing a real point.
- ** @see Value
- */
- public Value draw()
- {
- return new RealValue( random.nextDouble() * (b-a) + a);
- }
- /**
- ** Resets generator parameters (reordered if a > b)
- **/
- public void setParam(double a,double b)
- {
- this.a = a<=b ? a : b;
- this.b = a<=b ? b : a;
- }
- public int type()
- {
- return REAL_TYPE;
- }
- }