'Select element from array with probability proportional to number of occurrence

Return a random number from the array with probability of value being returned should be proportional to the number of times that an element with that value is stored.

If we have 1, 1, 3, 1, 3 stored, we would expect 1 to be returned with a 3/5 probability and 3 to be returned with 2/5 probability.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source