This challenge is rather simple:
You are given an array of positive (not including 0) integers, and have to select a random element from this array.
But here's the twist:
The probability of selecting an element is dependent on the value of the integer, meaning as the integer grows larger, the probability of it to get selected does too!
You are given the array
[4, 1, 5].
The probability of selecting 4 is equal to 4 divided by the sum of all elements in the array, in this case
4 / ( 4 + 1 + 5 ) = 4 / 10 =
The probability of selecting 1 is
1 / 10 or
An array of positive integers.
Return the selected integer if using a method, or directly print it to
- This is code-golf so shortest code in bytes in any language wins.
- Standard loopholes are forbidden.