package umontreal.iro.lecuyer.randvar;
import umontreal.iro.lecuyer.rng.*;
import umontreal.iro.lecuyer.probdist.*;
/**
* This class implements a random variate generator for the
* uniform distribution over integers, over the interval [i, j].
* Its mass function is
*
*
*
* p(x) = 1/(j - i + 1) for x = i, i + 1,…, j
*
* and 0 elsewhere.
*
*/
public class UniformIntGen extends RandomVariateGenInt {
protected int left; // the left limit of the interval
protected int right; // the right limit of the interval
/**
* Creates a uniform random variate generator over the integers
* in the closed interval [i, j], using stream s.
*
*/
public UniformIntGen (RandomStream s, int i, int j) {
super (s, new UniformIntDist (i, j));
setParams (i, j);
}
/**
* Creates a new generator for the distribution dist, using
* stream s.
*
*/
public UniformIntGen (RandomStream s, UniformIntDist dist) {
super (s, dist);
if (dist != null)
setParams (dist.getI(), dist.getJ());
}
/**
* Generates a new uniform random variate over the interval
* [i, j], using stream s, by inversion.
*
*/
public static int nextInt (RandomStream s, int i, int j) {
return UniformIntDist.inverseF (i, j, s.nextDouble());
}
/**
* Returns the parameter i.
*
*/
public int getI() {
return left;
}
/**
* Returns the parameter j.
*
*
*/
public int getJ() {
return right;
}
protected void setParams (int i, int j) {
if (j < i)
throw new IllegalArgumentException ("j < i");
left = i;
right = j;
}
}
Ads help maintain this website.