number generator

Anton Vredegoor anton.vredegoor at gmail.com
Wed Mar 14 12:24:16 EDT 2007


Paul Rubin wrote:

>> def genpool(n, m):
>>     if n == 1:
>>         yield [m]
>>     else:
>>         for i in xrange(1, m):
>>             for rest in genpool(n-1, m-i):
>>                 yield rest + [i]
>>
>> import random
>> print random.choice(list(genpool(n=4, m=20)))
> 
> This generates a lot of the partitions more than once, with
> possibly unequal probability.

Well, I just noticed that with my memoization function it produces too 
little :-(

But I hope you notice that this function doesn't create only partitions 
but all possible outcomes?

A.



More information about the Python-list mailing list