package umontreal.iro.lecuyer.randvar;
import umontreal.iro.lecuyer.rng.*;
import umontreal.iro.lecuyer.probdist.*;


/**
 * This class implements random variate generators for
 * the (continuous) uniform distribution over the interval (a, b),
 * where a and b are real numbers with a < b.
 * The density is 
 * 
 * 

*
* f (x) = 1/(b - a)         for a <= x <= b. *

* *

* The (non-static) nextDouble method simply calls inverseF on the * distribution. * */ public class UniformGen extends RandomVariateGen { private double a; private double b; /** * Creates a uniform random variate generator over the interval * (a, b), using stream s. * */ public UniformGen (RandomStream s, double a, double b) { super (s, new UniformDist(a, b)); setParams (a, b); } /** * Creates a uniform random variate generator over the interval * (0, 1), using stream s. * */ public UniformGen (RandomStream s) { this (s, 0.0, 1.0); } /** * Creates a new generator for the uniform distribution dist * and stream s. * */ public UniformGen (RandomStream s, UniformDist dist) { super (s, dist); if (dist != null) setParams (dist.getA(), dist.getB()); } /** * Generates a uniform random variate over the interval * (a, b) by inversion, using stream s. * */ static public double nextDouble (RandomStream s, double a, double b) { return UniformDist.inverseF (a, b, s.nextDouble()); } /** * Returns the value of a for this object. * */ public double getA() { return a; } /** * Returns the value of b for this object. * * */ public double getB() { return b; } /** * Sets the value of the parameters a and b for this object. * */ private void setParams (double a, double b) { if (b <= a) throw new IllegalArgumentException ("b <= a"); this.a = a; this.b = b; } }

Ads help maintain this website.