How to generate geometric random numbers?

MyInfoStation at gmail.com MyInfoStation at gmail.com
Sun Jul 23 23:33:41 EDT 2006


Robert Kern wrote:
> Gerhard Fiedler wrote:
> > On 2006-07-23 17:12:20, MyInfoStation at gmail.com wrote:
> >
> >> I am a newbie to Python and would like to genereate some numbers
> >> according to geometric distribution. However, the Python Random package
> >> seems do not have implemented functionality. I am wondering is there
> >> exist any other libraries that can do this job?
> >
> > The usual way is to generate standard random numbers (linear distribution)
> > and then apply whatever transformation you need to generate the desired
> > distribution.
>
> That only works if there is such a transformation.
>
> The geometric distribution and many others have been implemented in numpy:
>
>    http://www.scipy.org/NumPy
>
> In [1]: from numpy import random
>
> In [2]: random.geometric(0.5, size=100)
> Out[2]:
> array([1, 5, 2, 3, 1, 1, 2, 3, 1, 1, 2, 1, 1, 1, 2, 2, 3, 3, 1, 1, 1, 1, 1,
>         2, 2, 1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 3, 1,
>         4, 1, 1, 1, 2, 1, 2, 3, 2, 1, 1, 1, 1, 1, 3, 1, 1, 2, 6, 1, 1, 3, 2,
>         1, 1, 2, 1, 1, 7, 2, 1, 1, 2, 1, 1, 2, 4, 1, 2, 1, 4, 2, 1, 1, 2, 1,
>         4, 2, 1, 1, 3, 1, 3, 1])
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless enigma
>   that is made terrible by our own mad attempt to interpret it as though it had
>   an underlying truth."
>    -- Umberto Eco

Thanks a lot. I will try it out.

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.

Da




More information about the Python-list mailing list