All problems
Weighted Random Pick (Seeded)
Problem
Pick a value with probability proportional to its weight using a seeded generator.
Draw r = random.Random(seed).random() times the total weight and return the value whose cumulative weight first exceeds r. If the total weight is not positive return None.
Return the chosen value.
Hint
Walk the cumulative sums until the cumulative weight passes r.
Test cases
case 1
in: [["a","b"],[1,3],1]
expected: "a"
case 2
in: [["a","b"],[1,3],2]
expected: "b"
case 3
in: [["x"],[5],7]
expected: "x"
case 4
in: [["a","b","c"],[0,0,1],3]
expected: "c"
case 5
in: [[],[],0]
expected: null
Starter codePython
def weighted_random_pick(values, weights, seed):
# Your code here
pass5 test cases · run your code against them in the editor