/** * Returns a random number from the distribution with the given parameters n * and p. * * @param n * the number of trials * @param p * the probability of success. */ public static int staticNextInt(int n, double p) { synchronized (shared) { return shared.nextInt(n, p); } }
/** * Sets the uniform random number generated shared by all <b>static</b> * methods. * * @param randomGenerator * the new uniform random number generator to be shared. */ private static void xstaticSetRandomGenerator(DoubleRandomEngine randomGenerator) { synchronized (shared) { shared.setRandomGenerator(randomGenerator); } } }
/** * Constructs a Negative Binomial distribution. Example: n=1, p=0.5. * * @param n * the number of trials. * @param p * the probability of success. * @param randomGenerator * a uniform random number generator. */ public NegativeBinomial(int n, double p, DoubleRandomEngine randomGenerator) { setRandomGenerator(randomGenerator); setNandP(n, p); this.gamma = new Gamma(n, 1.0, randomGenerator); this.poisson = new Poisson(0.0, randomGenerator); }
/** * Returns a deep copy of the receiver; the copy will produce identical * sequences. After this call has returned, the copy and the receiver have * equal but separate state. * * @return a copy of the receiver. */ public Object clone() { NegativeBinomial copy = (NegativeBinomial) super.clone(); if (this.poisson != null) copy.poisson = (Poisson) this.poisson.clone(); copy.poisson.setRandomGenerator(copy.getRandomGenerator()); if (this.gamma != null) copy.gamma = (Gamma) this.gamma.clone(); copy.gamma.setRandomGenerator(copy.getRandomGenerator()); return copy; }
/** * Constructs a Negative Binomial distribution. Example: n=1, p=0.5. * * @param n * the number of trials. * @param p * the probability of success. * @param randomGenerator * a uniform random number generator. */ public NegativeBinomial(int n, double p, DoubleRandomEngine randomGenerator) { setRandomGenerator(randomGenerator); setNandP(n, p); this.gamma = new Gamma(n, 1.0, randomGenerator); this.poisson = new Poisson(0.0, randomGenerator); }
/** * Returns a deep copy of the receiver; the copy will produce identical * sequences. After this call has returned, the copy and the receiver have * equal but separate state. * * @return a copy of the receiver. */ public Object clone() { NegativeBinomial copy = (NegativeBinomial) super.clone(); if (this.poisson != null) copy.poisson = (Poisson) this.poisson.clone(); copy.poisson.setRandomGenerator(copy.getRandomGenerator()); if (this.gamma != null) copy.gamma = (Gamma) this.gamma.clone(); copy.gamma.setRandomGenerator(copy.getRandomGenerator()); return copy; }
/** * Returns a random number from the distribution with the given parameters n * and p. * * @param n * the number of trials * @param p * the probability of success. */ public static int staticNextInt(int n, double p) { synchronized (shared) { return shared.nextInt(n, p); } }
/** * Sets the uniform random number generated shared by all <b>static</b> * methods. * * @param randomGenerator * the new uniform random number generator to be shared. */ private static void xstaticSetRandomGenerator(DoubleRandomEngine randomGenerator) { synchronized (shared) { shared.setRandomGenerator(randomGenerator); } } }
/** * Returns a random number from the distribution; bypasses the internal * state. */ public int nextInt(int n, double p) { /*********************************************************************** * * Negative Binomial Distribution - Compound method * * * ***************************************************************** * * FUNCTION: - nbp samples a random number from the Negative * Binomial * distribution with parameters r (no. of * failures given) and p * (probability of success) * valid for r > 0, 0 < p < 1. * If G from * Gamma(r) then K from Poiss(pG/(1-p)) * is NB(r,p)--distributed. * * REFERENCE: - J.H. Ahrens, U. Dieter (1974): Computer methods * for * sampling from gamma, beta, Poisson and * binomial distributions, * Computing 12, 223--246. * SUBPROGRAMS: - drand(seed) ... * (0,1)-Uniform generator with * unsigned long integer *seed * - * Gamma(seed,a) ... Gamma generator for a > 0 * unsigned long *seed, * double a * - Poisson(seed,a) ...Poisson generator for a > 0 * * unsigned long *seed, double a. * * **********************************************************************/ double x = p / (1.0 - p); double p1 = p; double y = x * this.gamma.nextDouble(n, 1.0); return this.poisson.nextInt(y); }
/** * Returns a random number from the distribution; bypasses the internal * state. */ public int nextInt(int n, double p) { /*********************************************************************** * * Negative Binomial Distribution - Compound method * * * ***************************************************************** * * FUNCTION: - nbp samples a random number from the Negative * Binomial * distribution with parameters r (no. of * failures given) and p * (probability of success) * valid for r > 0, 0 < p < 1. * If G from * Gamma(r) then K from Poiss(pG/(1-p)) * is NB(r,p)--distributed. * * REFERENCE: - J.H. Ahrens, U. Dieter (1974): Computer methods * for * sampling from gamma, beta, Poisson and * binomial distributions, * Computing 12, 223--246. * SUBPROGRAMS: - drand(seed) ... * (0,1)-Uniform generator with * unsigned long integer *seed * - * Gamma(seed,a) ... Gamma generator for a > 0 * unsigned long *seed, * double a * - Poisson(seed,a) ...Poisson generator for a > 0 * * unsigned long *seed, double a. * * **********************************************************************/ double x = p / (1.0 - p); double p1 = p; double y = x * this.gamma.nextDouble(n, 1.0); return this.poisson.nextInt(y); }