how to generate random numbers that satisfy certain distribution

Arnaud Delobelle arnodel at
Sat Jan 23 20:57:07 CET 2010

thinke365 <thinke365 at> writes:

> such as uniform distribution, Normal distribution or poisson distribution.
> is there any package that can be used to generate such random numbers.

It's all in the standard library, the module is called -surprisingly-

- use random.uniform for the uniform distributions
- use random normalvariate for normal distributions

There isn't a Poisson distribution function, but there is a expovariate
function.  I think you can get poisson like this, but take it with a
grain of salt because my probability skills are very rusty!

import random
import itertools

def poisson(l):
    e = random.expovariate
    acc = 0.0
    for n in itertools.count():
        acc += e(l)
        if acc >= 1.0:
            return n


More information about the Python-list mailing list