Practice/Meta/Create a Lottery System Based on Weighted Probability
CodingMust
You need to build a lottery system where each participant has a different chance of winning based on an assigned weight value. The higher the weight, the more likely that participant is to win.
For example, if participant A has a weight of 1 and participant B has a weight of 3, then B should be three times more likely to win than A.
Your system should:
WeightedLottery class that accepts participants and their weights during initializationdraw_winner() method should return one participant selected randomly based on weighted probabilitiesdraw_winner() should be an independent random selectiondraw_winner() efficientlyExample 1:
` Input: participants = ["Alice", "Bob", "Charlie"] weights = [1, 2, 3]
Probability Distribution: Alice: 1/6 ≈ 16.67% Bob: 2/6 ≈ 33.33% Charlie: 3/6 = 50%
Output: One of the participants (e.g., "Charlie") Explanation: Charlie has the highest weight and should win approximately 50% of the time across many draws `
Example 2:
` Input: participants = ["Winner"] weights = [100]
Output: "Winner" Explanation: With only one participant, they always win regardless of their weight value `
Example 3:
` Input: participants = ["X", "Y", "Z"] weights = [10, 10, 10]
Probability Distribution: X: 33.33% Y: 33.33% Z: 33.33%
Output: One of "X", "Y", or "Z" Explanation: Equal weights mean equal probability for each participant `