how to generate random numbers that satisfy certain distribution

Steven D'Aprano steve at
Sat Jan 23 23:06:25 CET 2010

On Sat, 23 Jan 2010 12:29:22 -0800, Paul Rubin wrote:

> Peter Chant <peteRE at MpeteOzilla.Vco.ukE> writes:
>> I remeber being told that adding up 12 random numbers in the range 0-1
>> (which is what most computer random number genertors at the time
>> chucked out) and subtracted 6 gives a pretty good normal distribution. 
>> I think I did try it once and it failed, but I must have done something
>> odd.
> That gives you a binomial distribution on 12 trials, which approximates
> a normal distribution when the number of trials is large.  12 isn't too
> bad.  But there's a simpler way, the Box-Muller transform, that gives
> you a pair drawn from a precisely normal distribution from two uniform
> random samples:

The Box-Muller transform is reasonably simple, but you can't be serious 
that it is simpler than adding twelve random numbers and subtracting six!

def almost_normal():
    return sum([random.random() for _ in xrange(12)], -6)

Not that I'm recommending that anyone use this binomial approximation for 
anything but the quickest and dirtiest uses, particularly since the 
random module already has two excellent normal distributions (gauss and 


More information about the Python-list mailing list