def drawIndex(probs, tolerant=False):
 if ((not (sum(probs) < 1.00001)) or (not (sum(probs) > 0.99999))):
  if tolerant:
   probs /= sum(probs)
  else:
   print((probs, (1 - sum(probs))))
   raise ValueError()
 r = random()
 s = 0
 for (i, p) in enumerate(probs):
  s += p
  if (s > r):
   return i
 return choice(list(range(len(probs))))

def drawIndex ( probs , tolerant = False ) : if ( ( not ( sum ( probs ) < 1.00001 ) ) or ( not ( sum ( probs ) > 0.99999 ) ) ) : if tolerant : probs /= sum ( probs ) else : print ( ( probs , ( 1 - sum ( probs ) ) ) ) raise ValueError ( ) r = random ( ) s = 0 for ( i , p ) in enumerate ( probs ) : s += p if ( s > r ) : return i return choice ( list ( range ( len ( probs ) ) ) )
draws an index given an array of probabilities .