A gamma variate generator with shape parameter less than unity
Abstract
Algorithms for generating random numbers that follow a gamma distribution with shape parameter less than unity are proposed. Acceptance-rejection algorithms are developed, based on the generalized exponential distribution. The squeeze technique is applied to our method, and then piecewise envelope functions are further considered. The proposed methods are excellent in acceptance efficiency and promising in speed.
keywords:
Random number generation , gamma distribution , rejection sampling , Monte Carlo methodorganization=Space Research Institute, Austrian Academy of Sciences,addressline=Schmiedlstraße 6, city=Graz, postcode=8042, country=Austria
1 Introduction
The gamma distribution is one of the most important probability distributions in statistics. It has applications in a broad range of fields in natural sciences, engineering, and social sciences. The probability density is defined by
(1) |
where () is the shape parameter, () the scale parameter, and the gamma function.
Generating a gamma variate, a random number drawn from a gamma distribution, is an important and fundamental problem in statistical computing. Owing to its importance, various numerical algorithms have been developed since the early days of computer age [1, 3, 4, 8, 7, 9, 10, 6].
The nature of the gamma distribution is controlled by the shape parameter . Accordingly, we need to use different gamma variate generators. When , the distribution function increases from zero at , has a maximum at , and then decays as further increases. To generate a gamma variate with , probably Marsaglia & Tsang [8]’s algorithm is one of the best, as detailed in literature [8, 10, 6]. When , the distribution is reduced to the exponential distribution, and it is easy to generate a variate.
When , which is the range of our interest, the gamma distribution monotonically decreases from positive infinity at to zero at . To generate a gamma variate for , various acceptance-rejection methods have been developed [1, 3, 4, 7]. Combining power-law and exponential distributions, Ahrens & Dieter [1] proposed a piecewise rejection method. Best [3] extended the Ahrens & Dieter [1] method, by adjusting a switching point and introducing a squeeze technique. Devroye [4] developed a different rejection method, based on the exponential power distribution. As of 2024, it is employed by a Python package, NumPy [2]. Using a generalized exponential distribution [5], Kundu & Gupta [7] developed a rejection method and its piecewise extensions. Finally, Tanizaki [9] developed a ratio-of-uniforms method, which works either for and for .
In this note, we propose efficient gamma generators for , advancing an earlier study [7]. In Section 2, we develop an acceptance-rejection algorithm, using the generalized exponential distribution. In Section 3, we construct fractional functions for the squeeze technique. In Section 4, we discuss piecewise envelope functions. In Section 5, we evaluate the performance of the proposed and previous methods. Section 6 contains discussions and the summary.
2 Base algorithm
We construct a base algorithm to generate a random variate drawn from the gamma distribution with shape . We focus on the case, because we can obtain the results for , by multiplying the outputs by .
(2) |
In line of Kundu & Gupta [7], we employ the generalized exponential (GE) distribution [5],
(3) |
where () and () are the shape and scale parameters. Note that Refs. [5] and [7] use different conventions for the scale parameter, and we employ Gupta & Kundu [5]’s. The cumulative distribution function (CDF) of the GE distribution is
(4) |
Hereafter we consider the GE distribution with . We generate a uniform random variate in the range:
(5) |
Equating with (4) with , we find that a random variate that follows the generalized exponential distribution is drawn by
(6) |
To help our discussion, we use a parameter , a function , and a rejection function .
(7) | ||||
(8) |
The function is continuous at . When , as and , we find . Since , we find
(9) |
All the equal signs are met when .
Comparing the GE (3) and gamma distributions (2), we rewrite (2) as follows.
(10) |
Here, the relation is used. Eq. (10) suggests that we can apply a rejection method to the GE-distributed variate , to obtain a gamma-distributed variate. Using another uniform variate , we evaluate the acceptance condition:
(11) |
or an equivalent inequality:
(12) |
If either (11) or (12) is satisfied, then we take this number . If it is not satisfied, we discard the number, and then we go back to the beginning. The final output follows a gamma distribution with shape . These procedures are summarized in Algorithm 1 in Table 1. We need the total density to generate one gamma distribution.
Algorithm 1 |
---|
repeat |
generate |
, |
if return |
end repeat |
3 Squeeze technique
We improve the speed of the algorithm by the so-called squeeze method. We avoid the power and exponential operations in the acceptance condition (11), by using the following inequalities for .
(13) |
To show this, we start from several inequalities:
(14) | ||||
(15) | ||||
(16) | ||||
(17) |
We can prove them in the following way.
(18) | ||||
(19) | ||||
(20) |
For the left inequality of (13), we differentiate the rejection function
(21) |
With help from the Taylor series , we find
(22) |
In the right-hand side, all the terms in the numerator and in the denominator are positive, but the numerator terms contain additional factor of . This indicates that the ratio of the numerator to the denominator is less than . Together with the case, we find
(23) |
This suggests, together with (14),
(24) |
For the right inequality of (13), with help from (8), (14) and (15), we obtain
(25) |
In practice, we utilize the inequalities (13) or equivalent logical expressions, before evaluating . Translating again, we show the final algorithm in Algorithm 2 (Table 2).
Algorithm 2 |
---|
repeat |
generate |
, |
if return |
if then |
if return |
end repeat |
4 Piecewise envelope functions
Next, we split the envelope function for the rejection method into two parts. Across the switching point at (), we consider the GE distribution in the left and an exponential tail in the right. With help from the CDF of the GE distribution (Eq. (4)), we rewrite the gamma distribution (Eq. (2)) as follows.
(28) |
Here, and are the densities of the left and right parts,
(29) |
and is the second rejection function
(30) |
which satisfies for . Equation (28) tells us that we can apply the rejection method to the GE distribution with in the left part, and that we can apply the rejection method to the exponential density with in the right part.
Using a uniform variate , when , we proceed to the GE part. We generate the GE-distributed number in the range . Using a uniform variate , the GE variate can be drawn by
(31) |
Then we evaluate the acceptance condition, as discussed in Section 3.
In the right part, we obtain an exponential variate from ,
(32) |
and then we apply the rejection method. We can similarly apply the squeeze technique to (30). For , from (16) and (17), we obtain a squeeze relation
(33) |
Finally, we choose . First, we look for that minimizes the total density
(34) |
The solution can be found by solving by a root finder. In this case, we find an approximation by trial and error,
(35) |
This gives a near-minimum within an error of . Another choice is , in analogy with Ahrens & Dieter [1]. This simplifies several parameters in the algorithm.
These procedures are shown in Algorithm 3 (Table 3). The first three lines initialize coefficients, and then the lines inside the loop generate the variate. We reuse or as a uniform variate, in order to reduce the total number of random variates. When , we can further simplify the code, which is a trivial task.
Algorithm 3 |
, // |
, , |
, |
repeat |
generate |
if then |
, |
if return |
if then |
if return |
else |
, |
if return |
if then |
if return |
endif |
end repeat |
5 Numerical tests
We have carried out several benchmark tests. We wrote C codes for the eight methods: 1) the Ahrens & Dieter [1] method, 2) the Best [3] method, 3) the Devroye [4] method, 4) the Kundu & Gupta [7] method (Algorithm 3 in their paper), 5) Algorithm 1 (Table 1), 6) Algorithm 2 (Table 2), 7) Algorithm 3 (Table 3) with , and 8) Algorithm 3 with . In the last case, we have simplified the algorithm, as mentioned in Sec. 4. We use a uniform random generator (gsl_rng_uniform) in the GNU Scientific Library. We use Intel oneAPI Compiler (icx) v2024.1 with the -lgsl -O0 -lm option and the clang compiler (clang) v14.0 with the -lgsl -lm option on AMD Ryzen 5955 processor on Ubuntu Linux 24.04.

Figure 1 shows the normalized number of random variates to generate a gamma distribution, as a function of . The curves indicate theoretical predictions. Algorithms 1 & 2 require , the same number as the most efficient method to date, the Devroye [4] method (not shown). Algorithm 3’s number is given by (34). The circles indicate our Monte Carlo results for eleven shape parameters, . In each case, random numbers are generated. They are in excellent agreement with the theories. The proposed methods, Algorithm 3 in particular, need fewer random numbers than most of the other algorithms. The case is better than the case by %.

Next we compare the speed of the algorithms. Considering practical applications, two settings are considered [9]. In the first setting, we repeatedly draw one random variate times for all 11 parameters. We measure the elapsed time in seconds three times per method, and then take the average elapsed time. Figure 2(a) shows the results. The Devroye [4] method and Algorithm 2 are the fastest. They outperform the well established methods [1, 3]. Although the two compilers give different results, from Algorithms 1 and 2, we see that the squeeze technique is effective. Note that the Kundu & Gupta [7] method does not use the squeeze technique.
In the second setting, we draw random variates all at once, and then repeat this procedure for the 11 parameters. Some algorithms initialize internal parameters, before generating many random variates. For example, in Algorithm 3, the first three lines in Table 3 are used only once per parameter, before generating many variates at once. These lines are repeated each time we draw a variate in the first setting. Figure 2(b) shows the elapsed times. For reference, results with high-level compiler optimizations (the -O2 option) are also displayed. Now the piecewise algorithms such as the Best [3] method and Algorithm 3 become faster than in Fig. 2(a), suggesting that they are fast, once the parameters are initialized. Together with the Best [3] method and the Devroye [4] method, Algorithms 2–3 are close and promising. In Algorithm 3, the cases are always faster than the cases.
6 Discussion and Summary
We have developed the rejection algorithms to generate a gamma variate with the shape parameter . In line of Kundu & Gupta [7], we have employed the GE distribution [5]. The GE distribution is useful, because it resembles the gamma distribution, and because its CDF has a simple form. Kundu & Gupta [7] employed the GE distribution with the scale parameter , that is, , while we employ the scale parameter , , to obtain better acceptance efficiency. Therefore, the parameters and the rejection function in their study are different from ours. Despite a different strategy, our acceptance efficiency is as good as the best one [4].
To improve the performance, we have constructed the squeeze functions in (13). As demonstrated, this makes the code faster. One may notice that the left-hand side (the lower bound) can be negative for , but logically there is no problem. In addition, since , more than 98% of the random numbers are found for , where the lower bound condition is useful.
We have considered piecewise extensions of our algorithms, with an exponential tail [1, 3, 7]. This makes the acceptance efficiency even better. The squeeze functions are presented in (33). The left inequality (the lower bound) is identical to one in the Best [3] method, while the right one (the upper bound) is a new addition. This addition actually makes the Best [3] method few percents faster (not shown). As for Algorithm 3, despite the near-optimum density , the case is always faster than the case, because simplifies the algorithm. Therefore is our choice. In the second setting (Fig. 2(b)), Algorithm 3 with is always faster than Algorithm 2, owing to its good acceptance rate. It is promising when drawing many variates at once. In the first setting (Fig. 2(a)), in agreement with Tanizaki [9], several methods become slower due to the parameter initialization. This is also the case for Algorithm 3. In contrast, Algorithm 2 remains fast in the first and second settings, because of its simplicity.
Considering several aspects, we conclude that Algorithm 2 is one of the best two generators, together with the Devroye [4] method. These two often outperform the others, including the well established methods [1, 3]. With Intel compiler, Algorithm 2 is the best, and the Devroye [4] method is the best with clang. The proposed methods are simple enough, and easy to implement, as shown in Tables 1–3. In addition to the compiler-dependence, there can be a CPU-dependence (and possibly a GPU-dependence). Considering various runtime environments, we want to have as many good algorithms as possible, and ours are certainly one of them.
In summary, we have developed gamma variate generators for , using the GE distribution, the squeeze technique, and the piecewise envelope functions. The proposed methods are excellent in acceptance efficiency. The numerical tests suggest that Algorithm 2 is one of the best two generators for . Algorithm 3 with can be a good option when drawing many variates at once. We hope that the proposed methods are useful in practical applications.
Data Availability
Program codes in C language are available from the author upon request.
References
- Ahrens & Dieter [1974] Ahrens, J. H. and Dieter, U., “Computer Methods for Sampling from Gamma, Beta, Poisson and Binomial Distributions,” Computing 12, 223–246 (1974).
- Baumgarten & Patel [2022] Baumgarten C, and Patel, T., “Automatic random variate generation in Python,” Proc. of the 21st Python in Science Conf. (SCIPY 2022), 46–51 (2022).
- Best [1983] Best, D. J., “A Note on Gamma Variate Generators with Shape Parameter less than Unity,” Computing 30, 185–188 (1983).
- Devroye [1986] Devroye, L., Non-Uniform Random Variate Generation, Springer-Verlag (1986), Chap. VII, Sec. 2.6, p. 304.
- Gupta & Kundu [1999] Gupta, R. D. and Kundu, D., “Generalized exponential distributions,” Australian and New Zealand Journal of Statistics 41, 173–188 (1999).
- Kroese et al. [2011] Kroese, D. P., Taimre, T., and Botev, Z. I., Handbook of Monte Carlo methods, John Wiley & Sons. (2011).
- Kundu & Gupta [2007] Kundu, D. and Gupta, R. D., “A convenient way of generating gamma random variables using generalized exponential distribution,” Computing Statistics & Data Analysis 51, 2796–2802 (2007).
- Marsaglia & Tsang [2000] Marsaglia, G. and Tsang, W. W., “A Simple Method for Generating Gamma Variables,” ACM Transactions on Mathematical Software 26, 363–372 (2000).
- Tanizaki [2008] Tanizaki, H., “A Simple Gamma Random Number Generator for Arbitrary Shape Parameters,” Economics Bulletin 3, 1–10 (2008).
- Yotsuji [2010] Yotsuji, T., Random number generation of probability distributions for computer simulations, Pleiades Publishing, Nagano (2010).