简体   繁体   中英

How to randomly choose a certain number of elements in a list with non-uniform probability distribution without repetition?

For example, I have a list with 10 elements, I would like to choose 4 elements from the list randomly according to a certain probability distribution that I assign to, without repetition.

I know that there is a function numpy.random.choice, however this function allows repetition of choosing elements. How can the function of numpy.random.choice without repetition be achieved in a simple way?

IIUC, you just need to set the replace argument to False in np.random.choice() . Like this:

np.random.seed(123) # for reproducibility

np.random.choice(range(10), replace=False, size=4)
# array([4, 0, 7, 5])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM