Solution: Random Pick with Weight
Solution: Random Pick with Weight gives an array of positive weights; implement pickIndex to randomly return an index, with each index's chance proportional to its weight.
Statement
You’re given an array of positive integers, w, where w[i] is the weight of the index.
Implement the function pickIndex(), which performs weighted random selection to return an index from the w array. The larger the value of w[i], the heavier the weight is, and the higher the chances of its index being picked.
Suppose that the array consists of the weights . In this case, the probabilities of picking the indexes will be as follows:
-
Index 0:
-
Index 1: ...