package umontreal.iro.lecuyer.randvar;
import umontreal.iro.lecuyer.rng.*;
import umontreal.iro.lecuyer.probdist.*;
/**
* This class implements random variate generators for the
* triangular distribution. Its density is
*
*
*
*
* f (x) =
* 2(x - a)/[(b - a)(m - a)]
* for a <= x <= m,
*
* f (x) =
* 2(b - x)/[(b - a)(b - m)]
* for m <= x <= b,
*
* f (x) =
* 0
* elsewhere,
*
*
*
* where
* a <= m <= b (see, e.g.,).
*
*
* The (non-static) nextDouble method simply calls inverseF on the
* distribution.
*
*/
public class TriangularGen extends RandomVariateGen {
private double a;
private double b;
private double m;
/**
* Creates a triangular random variate generator over the interval
* (a, b), with parameter m, using stream s.
*
*/
public TriangularGen (RandomStream s, double a, double b, double m) {
super (s, new TriangularDist(a, b, m));
setParams (a, b, m);
}
/**
* Creates a triangular random variate generator over the interval
* (0, 1), with parameter m, using stream s.
*
*/
public TriangularGen (RandomStream s, double m) {
this (s, 0.0, 1.0, m);
}
/**
* Creates a new generator for the triangular distribution
* dist and stream s.
*
*/
public TriangularGen (RandomStream s, TriangularDist dist) {
super (s, dist);
if (dist != null)
setParams (dist.getA(), dist.getB(), dist.getM());
}
/**
* Generates a new variate from the triangular distribution with parameters
* a = a, b = b and m = m and stream s,
* using inversion.
*
*/
public static double nextDouble (RandomStream s, double a,
double b, double m) {
// the code is taken and adapted from unuran
// file /distributions/c_triangular_gen.c
return TriangularDist.inverseF (a, b, m, 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;
}
/**
* Returns the value of m for this object.
*
*
*/
public double getM() {
return m;
}
/**
* Sets the value of the parameters a, b and m for this object.
*
*/
private void setParams (double a, double b, double m) {
if ((a == 0.0 && b == 1.0) && (m < 0 || m > 1))
throw new IllegalArgumentException ("m is not in [0,1]");
else if (a >= b)
throw new IllegalArgumentException ("a >= b");
else if (m < a || m > b)
throw new IllegalArgumentException ("m is not in [a,b]");
this.a = a;
this.b = b;
this.m = m;
}
}
Ads help maintain this website.