def sample_distribution(distribution):
 r = random.uniform(0, 1)
 s = 0
 for i in range(len(distribution)):
  s += distribution[i]
  if (s >= r):
   return i
 return (len(distribution) - 1)

def sample_distribution ( distribution ) : r = random . uniform ( 0 , 1 ) s = 0 for i in range ( len ( distribution ) ) : s += distribution [ i ] if ( s >= r ) : return i return ( len ( distribution ) - 1 )
sample one element from a distribution assumed to be an array of normalized probabilities .