non-uniform distribution

Javier Montoya jmontoyaz at gmail.com
Sat Jun 12 08:35:08 EDT 2010


On Jun 12, 2:09 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> 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

Hi Steven,

The number of items in the vector is usually between [4,15]. I was
having in mind sth. like:
[0.25, 0.23, 0.30, 0.22] for the 4 elems case.

Best wishes



More information about the Python-list mailing list