number generator

Paul Rubin http
Wed Mar 14 02:35:51 CET 2007


Steven D'Aprano <steve at REMOVEME.cybersource.com.au> writes:
> Me too. Although Alex Martelli's algorithm:
> 
> map([random.randrange(5) for i in xrange(45)].count, xrange(5))
> 
> (each value needs adjusting up by one) really boggles my brain. I'm going
> to have to think about that.

Heh, that is woefully inefficient, but it is cute!  I must have missed
Alex's post where it appeared.

Basically you are taking five buckets and 45 marbles.  For each
marble, choose one of the buckets at random and throw the marble into
the bucket you chose.  When you're done, count how many marbles have
ended up in each bucket.  Alex's algorithm does it the hard way: for
each bucket selection, write down the result in a list.  Then scan
through the list to count how many 0's there are, then scan again to
see how many 1's there are, etc.



More information about the Python-list mailing list