def pickByDistribution(distribution, r=None):
 if (r is None):
  r = random
 x = r.uniform(0, sum(distribution))
 for (i, d) in enumerate(distribution):
  if (x <= d):
   return i
  x -= d

def pickByDistribution ( distribution , r = None ) : if ( r is None ) : r = random x = r . uniform ( 0 , sum ( distribution ) ) for ( i , d ) in enumerate ( distribution ) : if ( x <= d ) : return i x -= d
pick a value according to the provided distribution .