Weighted random selection from array

Compute the discrete cumulative density function (CDF) of your list — or in simple terms the array of cumulative sums of the weights. Then generate a random number in the range between 0 and the sum of all weights (might be 1 in your case), do a binary search to find this random number in your discrete CDF array and get the value corresponding to this entry — this is your weighted random number.

Leave a Comment