number generator
Army1987
please.ask at for.it
Sun Mar 11 14:25:42 EDT 2007
"cesco" <fd.calabrese at gmail.com> ha scritto nel messaggio
news:1173451441.077648.321270 at c51g2000cwc.googlegroups.com...
>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
More information about the Python-list
mailing list