Supermicro Computer evaluates several server configurations with different selection weights. Implement weighted sampling so that a configuration with weight w is selected with probability w / sum(weights).
To make the function deterministic and testable, the caller provides uniform random values instead of requiring the function to access a random-number library. For each random value, return the selected configuration index.
Implement weighted_random(weights, random_values):
weights is a list of positive integers, where weights[i] is the relative probability of selecting configuration i.random_values is a list of floating-point values, each in the interval [0, 1).u corresponds to the target u * sum(weights) on the cumulative weight scale.Build the cumulative distribution once, then use binary search for every sample.
def weighted_random(weights, random_values):