"cesco" <[EMAIL PROTECTED]> ha scritto nel messaggio 
news:[EMAIL PROTECTED]
>I have to generate a list of N random numbers (integer) whose sum is
> equal to M. If, for example, I have to generate 5 random numbers whose
> sum is 50 a possible solution could be [3, 11, 7, 22, 7]. Is there a
> simple pattern or function in Python to accomplish that?
>
> Thanks and regards
> Francesco


You can initialize a list to [1, 1, 1, 1, 1], and generate 45 random 
integers between 1 and 5, and every time a number is generated, increase the 
Nth number in the list by one.

Not all distinct lists will have the same chance of occurring, e.g. [46, 1, 
1, 1, 1] will be much less likely than [10, 10, 10, 10, 10]. Depending on 
what you need these numbers for, it can be a good thing or a bad thing.

--Army1987


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to