def improved(prob_matrix, items):
    cdf = np.cumsum(prob_matrix.T, axis=1)
ridx = np.random.random(size=n)
idx = np.zeros(n, dtype=int)
for i, r in enumerate(ridx):
    idx[i] = np.searchsorted(cdf[i], r)
return items[idx]
Fast random weighted selection across all rows of a stochastic matrix