non-uniform distribution

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Jun 12 08:09:07 EDT 2010


On Sat, 12 Jun 2010 03:05:43 -0700, Javier Montoya wrote:

> Dear all,
> 
> I need to generate a vector of random float numbers between [0,1] such
> that their sum equals 1 and that are distributed non-uniformly. Is there
> any python function that generates such a vector?

You haven't explained your requirements in anywhere near enough detail. 
Any of these match your description:

[1.0]
[0.0, 0.0, 0.0, 0.0, 1.0]
[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.3]
[0.01, 0.01, 0.01, 0.01, 0.02, 0.02, 0.02, 0.03, 0.03, 0.84]

and many, many more. What do you mean by "non-uniformly"? Do you have any 
specific distribution in mind? Do you have to have a particular number of 
items?

For instance, you could do this:

L = []
total = 0.0
while total < 1:
    x = random.uniform(0, 1-total)
    L.append(x)
    total += x

assert sum(L) == total == 1.0


-- 
Steven



More information about the Python-list mailing list