number generator

Paul Rubin http
Wed Mar 14 11:50:55 EDT 2007


"Raymond Hettinger" <python at rcn.com> writes:
> Since people are posting their solutions now (originally only hints
> were provided for the homework problem), here's mine:
> 
> 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.



More information about the Python-list mailing list