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

/**
 * This class implements random variate generators for the 
 * Bernoulli distribution (see class
 *  {@link umontreal.iro.lecuyer.probdist.BernoulliDist BernoulliDist}).
 * 
 */
public class BernoulliGen extends RandomVariateGenInt  {
   protected double p;    
    


   /**
    * Creates a Bernoulli random variate generator with parameter p,
    *   using stream s.
    * 
    */
   public BernoulliGen (RandomStream s, double p) {
      super (s, new BernoulliDist (p));
      setParams (p);
   }


   /**
    * Creates a random variate generator for the Bernoulli 
    *     distribution dist and the random stream s.
    * 
    */
   public BernoulliGen (RandomStream s, BernoulliDist dist)  {
      super (s, dist);
      if (dist != null)
         setParams (dist.getP());
   }


   /**
    * Generates a new integer from the Bernoulli distribution with
    *   parameter p = p, using the given stream s.
    * 
    */
   public static int nextInt (RandomStream s, double p) {
      return BernoulliDist.inverseF (p, s.nextDouble());
   }


   /**
    * Returns the parameter p of this object.
    * 
    * 
    */
   public double getP() {
      return p;
   }
   


   /**
    * Sets the parameter p of this object.
    * 
    */
   protected void setParams (double p) {
      if (p < 0.0 || p > 1.0)
         throw new IllegalArgumentException ("p not in range [0, 1]");
      this.p = p;
   }

}

 

Ads help maintain this website.