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


/**
 * Implements Pascal random variate generators by
 * the convolution method.
 * The method generates n geometric variates with probability p
 * and adds them up.
 * 
 * 

* The algorithm is slow if n is large. * */ public class PascalConvolutionGen extends PascalGen { /** * Creates a Pascal random variate generator with parameters * n and p, using stream s. * */ public PascalConvolutionGen (RandomStream s, int n, double p) { super (s, null); setParams (n, p); } /** * Creates a new generator for the distribution dist, using * stream s. * */ public PascalConvolutionGen (RandomStream s, PascalDist dist) { super (s, dist); } public int nextInt() { int x = 0; for (int i = 0; i < n; i++) x += GeometricDist.inverseF (p, stream.nextDouble()); return x; } public static int nextInt (RandomStream s, int n, double p) { return convolution (s, n, p); } /** * Generates a new variate from the Pascal distribution, * with parameters n = n and p = p, using the stream s. * */ private static int convolution (RandomStream stream, int n, double p) { int x = 0; for (int i = 0; i < n; i++) x += GeometricDist.inverseF (p, stream.nextDouble()); return x; } }

Ads help maintain this website.