package umontreal.iro.lecuyer.randvar;
import umontreal.iro.lecuyer.rng.RandomStream;
import umontreal.iro.lecuyer.probdist.DiscreteDistributionInt;

/**
 * This is the base class for all generators of discrete random variates
 * over the set of integers.
 * Similar to {@link RandomVariateGen}, except that the generators
 * produce integers, via the {@link #nextInt nextInt} method, instead of real numbers.
 * 
 */
public class RandomVariateGenInt extends RandomVariateGen {



   protected RandomVariateGenInt() {}

   /**
    * Creates a new random variate generator for the discrete
    *     distribution dist, using stream s.
    *  
    * @param s random stream used for generating uniforms
    * 
    *    @param dist discrete distribution object of the generated values
    * 
    */
   public RandomVariateGenInt (RandomStream s, DiscreteDistributionInt dist)  {
      this.stream = s;
      this.dist   = dist;
   }


   /**
    * Generates a random number (an integer) from the discrete
    *     distribution contained in this object.
    *     By default, this method uses inversion by calling the inverseF
    *     method of the distribution object.
    *     Alternative generating methods are provided in subclasses.
    *  
    * @return the generated value
    * 
    */
   public int nextInt()  {
      return ((DiscreteDistributionInt) dist).inverseFInt (stream.nextDouble());
   }


   /**
    * Generates n random numbers from the discrete distribution
    *     contained in this object.  The results are stored into the array v,
    *     starting from index start.
    *     By default, this method calls {@link #nextInt() nextInt()} n
    *    times, but one can reimplement it in subclasses for better efficiency.
    *  
    * @param v array into which the variates will be stored
    * 
    *    @param start starting index, in v, of the new variates
    * 
    *    @param n number of variates being generated
    * 
    * 
    */
   public void nextArrayOfInt (int[] v, int start, int n)  {
      if (n < 0)
         throw new IllegalArgumentException ("n must be positive.");
      for (int i = 0; i < n; i++)
         v[start + i] = nextInt();
   }


   /**
    * Returns the {@link DiscreteDistributionInt} used by this generator.
    * 
    * @return the distribution associated to that object
    * 
    */
   public DiscreteDistributionInt getDistribution()  {
      return (DiscreteDistributionInt) dist;
   }

}

 

Ads help maintain this website.