How to generate geometric random numbers?

Paul Rubin http
Sun Jul 23 23:49:40 EDT 2006


MyInfoStation at gmail.com writes:
> But I am still surprised because the default Random package in Python
> can generate so few discrete random distritbuions, while it can
> generate quite a few continuous distribution, including some not very
> common one.

It looks pretty simple to transform the uniform distribution to the
geometric distribution.  The formula for its cdf is pretty simple:

  cdf(p,n) = (1-p)**(n-1)*p

For fixed p, if the cdf is c, we get (unless I made an error),

   n = log(c, 1-p) - 1

So choose a uniform point c in the unit interval, run it through that
formula, and round up to the nearest integer.

See http://en.wikipedia.org/wiki/Geometric_distribution
for more about the distribution.



More information about the Python-list mailing list