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


/**
 * This class implements a random variate generator for the 
 * geometric distribution. Its has parameter p and mass function
 * 
 * 

*
* p(x) = p(1 - p)x for x = 0, 1, 2,..., *

* where * 0 <= p <= 1. * Random variates are generated by calling inversion on the distribution object. * */ public class GeometricGen extends RandomVariateGenInt { private double p; /** * Creates a geometric random variate generator with * parameter p, using stream s. * */ public GeometricGen (RandomStream s, double p) { super (s, new GeometricDist(p)); setParams (p); } /** * Creates a new generator for the distribution dist, using * stream s. * */ public GeometricGen (RandomStream s, GeometricDist dist) { super (s, dist); if (dist != null) setParams (dist.getP()); } public int nextInt () { return GeometricDist.inverseF (p, stream.nextDouble()); } /** * Generates a geometric random variate with parameter * p = p, using stream s, by inversion. * */ public static int nextInt (RandomStream s, double p) { return GeometricDist.inverseF (p, s.nextDouble()); } /** * Returns the parameter p of this object. * * */ public double getP() { return p; } /** * Sets the parameter n and 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.