how to generate random numbers that satisfy certain distribution

Arnaud Delobelle arnodel at googlemail.com
Sat Jan 23 14:57:07 EST 2010


thinke365 <thinke365 at gmail.com> 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-
'random'.

- 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

-- 
Arnaud



More information about the Python-list mailing list