/** * 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); } }
/** * 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); } }
/** * 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); }