def randintw(w, size=1):
 from numpy.random import random
 p = (np.cumsum(w) / np.sum(w))
 rvs = p.searchsorted(random(np.prod(size))).reshape(size)
 return rvs

def randintw ( w , size = 1 ) : from numpy . random import random p = ( np . cumsum ( w ) / np . sum ( w ) ) rvs = p . searchsorted ( random ( np . prod ( size ) ) ) . reshape ( size ) return rvs
generate integer random variables given probabilties useful because it can be used as index into any array or sequence type parameters w : 1d array_like sequence of weights .