Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight. For example:
i
0
1
w[i]
1
3
As shown, the index 0 has a weight 1, and the index 1 has a weight 3.
Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a function rand10 which generates a uniform random integer in the range 1 to 10.
Here is the code to solve the problem:
1 2 3 4 5 6 7
publicintrand10(){ int random = 41; while(random > 40) { random = 7*(rand7() - 1) + rand7(); } return random % 10 + 1; }