Assume I have an array of items [1,2, ...n]
, and a probabilities array [p1,p2,....,pn]
, where n
is a very large number and may reach to thousands. The sum of all probabilities equals 1.
I need to select 3 unique items randomly each time, an item with a high probability has a higher chance of being selected.
I need to do the selection for more than 20k times.
I've implemented a working method by creating a new array that contains the items with repetition based on their probability. For example, if probabilities for item1, item2, and item3 are [2/n,4/n,1/n]
respectively, then the new array will contain [1,1,2,2,2,2,3]
.
It works fine but it's not efficient. Also, using this method there is the possibility of selecting the same item multiple times, then I have to reselect another item which consumes time.
Are there any efficient methods or built-in functions in MATLAB for this purpose?