Probability with random generation

Hi,

I want to implement discrete integer random number generator with a given distribution. The distribution is provided as real weights of indices. Provide unit tests to check the correctness of the solution.

Examples:

1 input: [1.0, 2.0]

Output: 1 with probability 1/3 and 2 with probability 2/3

2.input: [1.0, 1.0, 1.0, 1.0, 1.0]
Output: a number between 0 and 4 with equal probabilities

Please help me. I have tried difference ways but could get it

Thanks in advance.