static int rpois_rng (lua_State *L) { nl_RNG *r = getrng(L); lua_Number mu = luaL_checknumber(L, 1); checkneg(L, mu); setdeviate(integer, ignpoi(r, mu), 2); return 1; }
double PoissonRandom(double mean) { // Call into ranlib for the Poisson deviate. if (mean == 0) { return 0; } else if (mean > 1e3) { return (mean + sqrt(mean)*snorm()); } else if (mean > 1e6) { return mean; } else return static_cast<double>(ignpoi(static_cast<float>(mean))); }
long ignnbn(lua_RNG *o,long n,double p) /* ********************************************************************** GENerate Negative BiNomial random deviate Function Generates a single random deviate from a negative binomial distribution. Arguments N --> The number of trials in the negative binomial distribution from which a random deviate is to be generated. P --> The probability of an event. Method Algorithm from page 480 of Devroye, Luc Non-Uniform Random Variate Generation. Springer-Verlag, New York, 1986. ********************************************************************** */ { static long ignnbn; static double y,a,r; /* .. .. Executable Statements .. */ /* Check Arguments */ if(n < 0) ftnstop("N < 0 in IGNNBN"); if(p <= 0.0F) ftnstop("P <= 0 in IGNNBN"); if(p >= 1.0F) ftnstop("P >= 1 in IGNNBN"); /* Generate Y, a random gamma (n,(1-p)/p) variable */ r = (float)n; a = p/(1.0F-p); y = gengam(o,a,r); /* Generate a random Poisson(y) variable */ ignnbn = ignpoi(o,y); return ignnbn; }