1 /*
2  *  Mathlib : A C Library of Special Functions
3  *  Copyright (C) 1998 Ross Ihaka and the R Core Team.
4  *  Copyright (C) 2000 The R Core Team
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, a copy is available at
18  *  https://www.R-project.org/Licenses/
19  *
20  *  SYNOPSIS
21  *
22  *    #include <Rmath.h>
23  *    double rgeom(double p);
24  *
25  *  DESCRIPTION
26  *
27  *    Random variates from the geometric distribution.
28  *
29  *  NOTES
30  *
31  *    We generate lambda as exponential with scale parameter
32  *    p / (1 - p).  Return a Poisson deviate with mean lambda.
33  *    See Example 1.5 in Devroye (1986), Chapter 10, pages 488f.
34  *
35  *  REFERENCE
36  *
37  *    Devroye, L. (1986).
38  *    Non-Uniform Random Variate Generation.
39  *    New York: Springer-Verlag.
40  *    Pages 488f.
41  */
42 
43 #include "nmath.h"
44 
rgeom(double p)45 double rgeom(double p)
46 {
47     if (!R_FINITE(p) || p <= 0 || p > 1) ML_WARN_return_NAN;
48 
49     return rpois(exp_rand() * ((1 - p) / p));
50 }
51