You will write a function for the expected value of any game, then simulate a made-up game many times and check that the average gain lands near the expected gain.
The idea. The expected value is the sum of each value times its probability. A game can be simulated by picking a value with the right probabilities: random.choices(values, weights=probs)[0] does exactly that. By the law of large numbers, the average of many plays settles near the expected value.
Your tasks.
expected_value(values, probs). Return the sum of each value times its probability.play(values, probs). Return one value picked at random with the given probabilities.average_gain(values, probs, cost, n). Play n times and return the average of the value minus the cost.Have you tried it?
Do the tasks in the editor first, and run the checker after each one. Its hints help when a check fails. When you are ready, compare your code with this solution.
Log in to join the discussion.
Loading…