Is there build in numpy funciton to return index idx from one dimensional array r with probability r[i]. All r[i] non negative and sums to one.
2 Answers
scipy.stats can help, even if it is not in NumPy as requested:
import scipy.stats
r = [.1,.8,.1]
x = range(len(r))
mydist = scipy.stats.rv_discrete(values=(x,r), name='mydist')
print mydist.rvs(size=20)
# array([1, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1])