2012-10-10 53 views

回答

1

幾乎建於:

# setup (do this once) 
cs = r.cumsum() 

# get a random value with probability r[i] 
rn = np.random.uniform() 
idx = cs.searchsorted(rn) 
0

scipy.stats可以幫助,即使它不是在與NumPy的要求:

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])