In <[EMAIL PROTECTED]>, Raymond Hettinger wrote: > On Mar 9, 7:32 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: >> In <[EMAIL PROTECTED]>, cesco wrote: >> > Given two positive integers, N and M with N < M, I have to generate N >> > positive integers such that sum(N)=M. No more constraints. >> >> Break it into subproblems. Generate a random number X from a suitable >> range and you are left with one number, and the problem to generate (N-1) >> random numbers that add up to (M-X). > > This approach skews the probabilities. The OP said for example with > N=5 and M=50 that a possible solution is [3, 11, 7, 22, 7]. You're > approach biases the probabilities toward solutions that have a large > entry in the first position.
I know but he said also "No more constraints". And… > To make the solutions equi-probable, a simple approach is to > recursively enumerate all possibilities and then choose one of them > with random.choice(). …it would be faster than creating all possibilities. :-) Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list