number generator

Marc 'BlackJack' Rintsch bj_666 at
Sat Mar 10 10:16:15 CET 2007

In <1173515984.515050.27400 at>, Raymond
Hettinger wrote:

> On Mar 9, 7:32 am, Marc 'BlackJack' Rintsch <bj_... at> wrote:
>> In <1173453432.893222.308... at>, 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.  :-)

	Marc 'BlackJack' Rintsch

More information about the Python-list mailing list