290 Chapter 7. Random Numbers
} while (rsq >= 1.0 || rsq == 0.0); and if they are not, try again.
fac=sqrt(-2.0*log(rsq)/rsq);
Now make the Box-Muller transformation to get two normal deviates. Return one and
save the other for next time.
gset=v1*fac;
iset=1; Set ﬂag.
return v2*fac;
} else { We have an extra deviate handy,
visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).
readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs
Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-
Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.
Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)
iset=0; so unset the ﬂag,
return gset; and return it.
}
}
See Devroye [1] and Bratley [2] for many additional algorithms.
CITED REFERENCES AND FURTHER READING:
Devroye, L. 1986, Non-Uniform Random Variate Generation (New York: Springer-Verlag), §9.1.
[1]
Bratley, P., Fox, B.L., and Schrage, E.L. 1983, A Guide to Simulation (New York: Springer-
Verlag). [2]
Knuth, D.E. 1981, Seminumerical Algorithms, 2nd ed., vol. 2 of The Art of Computer Programming
(Reading, MA: Addison-Wesley), pp. 116ff.
7.3 Rejection Method: Gamma, Poisson,
Binomial Deviates
The rejection method is a powerful, general technique for generating random
deviates whose distribution function p(x)dx (probability of a value occurring between
x and x + dx) is known and computable. The rejection method does not require
that the cumulative distribution function [indeﬁnite integral of p(x)] be readily
computable, much less the inverse of that function — which was required for the
transformation method in the previous section.
The rejection method is based on a simple geometrical argument:
Draw a graph of the probability distribution p(x) that you wish to generate, so
that the area under the curve in any range of x corresponds to the desired probability
of generating an x in that range. If we had some way of choosing a random point in
two dimensions, with uniform probability in the area under your curve, then the x
value of that random point would have the desired distribution.
Now, on the same graph, draw any other curve f(x) which has ﬁnite (not
inﬁnite) area and lies everywhere above your original probability distribution. (This
is always possible, because your original curve encloses only unit area, by deﬁnition
of probability.) We will call this f(x) the comparison function. Imagine now
that you have some way of choosing a random point in two dimensions that is
uniform in the area under the comparison function. Whenever that point lies outside
the area under the original probability distribution, we will reject it and choose
another random point. Whenever it lies inside the area under the original probability
distribution, we will accept it. It should be obvious that the accepted points are
uniform in the accepted area, so that their x values have the desired distribution. It

7.3 Rejection Method: Gamma, Poisson, Binomial Deviates 291
A
first random
deviate in
⌠x
⌡0 f(x)dx
reject x0
visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).
readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs
Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-
Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.
Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)
f(x0 )
f (x)
accept x0
second random
p(x) deviate in
0 0
x0
Figure 7.3.1. Rejection method for generating a random deviate x from a known probability distribution
p(x) that is everywhere less than some other function f (x). The transformation method is ﬁrst used to
generate a random deviate x of the distribution f (compare Figure 7.2.1). A second uniform deviate is
used to decide whether to accept or reject that x. If it is rejected, a new deviate of f is found; and so on.
The ratio of accepted to rejected points is the ratio of the area under p to the area between p and f .
should also be obvious that the fraction of points rejected just depends on the ratio
of the area of the comparison function to the area of the probability distribution
function, not on the details of shape of either function. For example, a comparison
function whose area is less than 2 will reject fewer than half the points, even if it
approximates the probability function very badly at some values of x, e.g., remains
ﬁnite in some region where x is zero.
It remains only to suggest how to choose a uniform random point in two
dimensions under the comparison function f(x). A variant of the transformation
method (§7.2) does nicely: Be sure to have chosen a comparison function whose
indeﬁnite integral is known analytically, and is also analytically invertible to give x
as a function of “area under the comparison function to the left of x.” Now pick a
uniform deviate between 0 and A, where A is the total area under f(x), and use it
to get a corresponding x. Then pick a uniform deviate between 0 and f(x) as the y
value for the two-dimensional point. You should be able to convince yourself that the
point (x, y) is uniformly distributed in the area under the comparison function f(x).
An equivalent procedure is to pick the second uniform deviate between zero
and one, and accept or reject according to whether it is respectively less than or
greater than the ratio p(x)/f(x).
So, to summarize, the rejection method for some given p(x) requires that one
ﬁnd, once and for all, some reasonably good comparison function f(x). Thereafter,
each deviate generated requires two uniform random deviates, one evaluation of f (to
get the coordinate y), and one evaluation of p (to decide whether to accept or reject
the point x, y). Figure 7.3.1 illustrates the procedure. Then, of course, this procedure
must be repeated, on the average, A times before the ﬁnal deviate is obtained.
Gamma Distribution
The gamma distribution of integer order a > 0 is the waiting time to the ath
event in a Poisson random process of unit mean. For example, when a = 1, it is just
the exponential distribution of §7.2, the waiting time to the ﬁrst event.

292 Chapter 7. Random Numbers
A gamma deviate has probability pa (x)dx of occurring with a value between
x and x + dx, where
xa−1 e−x
pa (x)dx = dx x>0 (7.3.1)
Γ(a)
visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).
readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs
Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-
Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.
Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)
To generate deviates of (7.3.1) for small values of a, it is best to add up a
exponentially distributed waiting times, i.e., logarithms of uniform deviates. Since
the sum of logarithms is the logarithm of the product, one really has only to generate
the product of a uniform deviates, then take the log.
For larger values of a, the distribution (7.3.1) has a typically “bell-shaped”
√
form, with a peak at x = a and a half-width of about a.
We will be interested in several probability distributions with this same qual-
itative form. A useful comparison function in such cases is derived from the
Lorentzian distribution
1 1
p(y)dy = dy (7.3.2)
π 1 + y2
whose inverse indeﬁnite integral is just the tangent function. It follows that the
x-coordinate of an area-uniform random point under the comparison function
c0
f(x) = (7.3.3)
1 + (x − x0 )2 /a2
0
for any constants a0 , c0 , and x0 , can be generated by the prescription
x = a0 tan(πU ) + x0 (7.3.4)
where U is a uniform deviate between 0 and 1. Thus, for some speciﬁc “bell-shaped”
p(x) probability distribution, we need only ﬁnd constants a0 , c0 , x0 , with the product
a0 c0 (which determines the area) as small as possible, such that (7.3.3) is everywhere
greater than p(x).
Ahrens has done this for the gamma distribution, yielding the following
algorithm (as described in Knuth [1]):
#include
float gamdev(int ia, long *idum)
Returns a deviate distributed as a gamma distribution of integer order ia, i.e., a waiting time
to the iath event in a Poisson process of unit mean, using ran1(idum) as the source of
uniform deviates.
{
float ran1(long *idum);
void nrerror(char error_text[]);
int j;
float am,e,s,v1,v2,x,y;
if (ia < 1) nrerror("Error in routine gamdev");
if (ia < 6) { Use direct method, adding waiting
x=1.0; times.
for (j=1;j

7.3 Rejection Method: Gamma, Poisson, Binomial Deviates 293
do {
do {
do { These four lines generate the tan-
v1=ran1(idum); gent of a random angle, i.e., they
v2=2.0*ran1(idum)-1.0; are equivalent to
} while (v1*v1+v2*v2 > 1.0); y = tan(π * ran1(idum)).
y=v2/v1;
am=ia-1;
visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).
readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs
Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-
Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.
Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)
s=sqrt(2.0*am+1.0);
x=s*y+am; We decide whether to reject x:
} while (x e); Reject on basis of a second uniform
} deviate.
return x;
}
Poisson Deviates
The Poisson distribution is conceptually related to the gamma distribution. It
gives the probability of a certain integer number m of unit rate Poisson random
events occurring in a given interval of time x, while the gamma distribution was the
probability of waiting time between x and x + dx to the mth event. Note that m takes
on only integer values ≥ 0, so that the Poisson distribution, viewed as a continuous
distribution function px(m)dm, is zero everywhere except where m is an integer
≥ 0. At such places, it is inﬁnite, such that the integrated probability over a region
containing the integer is some ﬁnite number. The total probability at an integer j is
j+
xj e−x
Prob(j) = px (m)dm = (7.3.5)
j− j!
At ﬁrst sight this might seem an unlikely candidate distribution for the rejection
method, since no continuous comparison function can be larger than the inﬁnitely
tall, but inﬁnitely narrow, Dirac delta functions in px (m). However, there is a trick
that we can do: Spread the ﬁnite area in the spike at j uniformly into the interval
between j and j + 1. This deﬁnes a continuous distribution qx (m)dm given by
x[m] e−x
qx (m)dm = dm (7.3.6)
[m]!
where [m] represents the largest integer less than m. If we now use the rejection
method to generate a (noninteger) deviate from (7.3.6), and then take the integer
part of that deviate, it will be as if drawn from the desired distribution (7.3.5). (See
Figure 7.3.2.) This trick is general for any integer-valued probability distribution.
For x large enough, the distribution (7.3.6) is qualitatively bell-shaped (albeit
with a bell made out of small, square steps), and we can use the same kind of
Lorentzian comparison function as was already used above. For small x, we can
generate independent exponential deviates (waiting times between events); when the
sum of these ﬁrst exceeds x, then the number of events that would have occurred in
waiting time x becomes known and is one less than the number of terms in the sum.
These ideas produce the following routine:

294 Chapter 7. Random Numbers
1
in
visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).
readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs
Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-
Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.
Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)
reject
accept
0
1 2 3 4 5
Figure 7.3.2. Rejection method as applied to an integer-valued distribution. The method is performed
on the step function shown as a dashed line, yielding a real-valued deviate. This deviate is rounded
down to the next lower integer, which is output.
#include
#define PI 3.141592654
float poidev(float xm, long *idum)
Returns as a ﬂoating-point number an integer value that is a random deviate drawn from a
Poisson distribution of mean xm, using ran1(idum) as a source of uniform random deviates.
{
float gammln(float xx);
float ran1(long *idum);
static float sq,alxm,g,oldm=(-1.0); oldm is a ﬂag for whether xm has changed
float em,t,y; since last call.
if (xm < 12.0) { Use direct method.
if (xm != oldm) {
oldm=xm;
g=exp(-xm); If xm is new, compute the exponential.
}
em = -1;
t=1.0;
do { Instead of adding exponential deviates it is equiv-
++em; alent to multiply uniform deviates. We never
t *= ran1(idum); actually have to take the log, merely com-
} while (t > g); pare to the pre-computed exponential.
} else { Use rejection method.
if (xm != oldm) { If xm has changed since the last call, then pre-
oldm=xm; compute some functions that occur below.
sq=sqrt(2.0*xm);
alxm=log(xm);
g=xm*alxm-gammln(xm+1.0);
The function gammln is the natural log of the gamma function, as given in §6.1.
}
do {
do { y is a deviate from a Lorentzian comparison func-
y=tan(PI*ran1(idum)); tion.

7.3 Rejection Method: Gamma, Poisson, Binomial Deviates 295
em=sq*y+xm; em is y, shifted and scaled.
} while (em < 0.0); Reject if in regime of zero probability.
em=floor(em); The trick for integer-valued distributions.
t=0.9*(1.0+y*y)*exp(em*alxm-gammln(em+1.0)-g);
The ratio of the desired distribution to the comparison function; we accept or
reject by comparing it to another uniform deviate. The factor 0.9 is chosen so
that t never exceeds 1.
} while (ran1(idum) > t);
visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).
readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs
Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-
Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.
Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)
}
return em;
}
Binomial Deviates
If an event occurs with probability q, and we make n trials, then the number of
times m that it occurs has the binomial distribution,
j+
n j
pn,q (m)dm = q (1 − q)n−j (7.3.7)
j− j
The binomial distribution is integer valued, with m taking on possible values
from 0 to n. It depends on two parameters, n and q, so is correspondingly a
bit harder to implement than our previous examples. Nevertheless, the techniques
already illustrated are sufﬁciently powerful to do the job:
#include
#define PI 3.141592654
float bnldev(float pp, int n, long *idum)
Returns as a ﬂoating-point number an integer value that is a random deviate drawn from
a binomial distribution of n trials each of probability pp, using ran1(idum) as a source of
uniform random deviates.
{
float gammln(float xx);
float ran1(long *idum);
int j;
static int nold=(-1);
float am,em,g,angle,p,bnl,sq,t,y;
static float pold=(-1.0),pc,plog,pclog,en,oldg;
p=(pp