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

/**
 * This class implements random variate generators for the 
 * binomial distribution. It has parameters n and p with
 *  mass function
 * 
 * 

*
* p(x) = nCr(n, x)px(1 - p)n-x = n!/(x!(n - x)!)  px(1 - p)n-x        for x = 0, 1, 2,..., n *

* where nCr(n, x) is the number of combinations of x objects * among n, * n is a positive integer, and * 0 <= p <= 1. * *

* The (non-static) nextInt method simply calls inverseF on the * distribution. * */ public class BinomialGen extends RandomVariateGenInt { protected int n = -1; protected double p = -1.0; /** * Creates a binomial random variate generator with parameters n and p, * using stream s. * */ public BinomialGen (RandomStream s, int n, double p) { super (s, new BinomialDist (n, p)); setParams (n, p); } /** * Creates a random variate generator for the binomial * distribution dist and the random stream s. * */ public BinomialGen (RandomStream s, BinomialDist dist) { super (s, dist); if (dist != null) setParams (dist.getN(), dist.getP()); } /** * Generates a new integer from the binomial distribution with * parameters * n = n and p = p, using the given stream s. * */ public static int nextInt (RandomStream s, int n, double p) { return BinomialDist.inverseF (n, p, s.nextDouble()); } /** * Returns the parameter n of this object. * */ public int getN() { return n; } /** * Returns the parameter p of this object. * * */ public double getP() { return p; } /** * Sets the parameter n and p of this object. * */ protected void setParams (int n, double p) { if (p < 0.0 || p > 1.0) throw new IllegalArgumentException ("p not in range [0, 1]"); if (n <= 0) throw new IllegalArgumentException ("n <= 0"); this.p = p; this.n = n; } }

Ads help maintain this website.