Ex03 Parrondos Paradox

Module 2: Data Structures

Exercise: Parrondo’s Paradox

Juan Parrondo stated this paradox within game theory where two games, both of which are losing when played individually, can lead to a winning strategy when played alternately. Your goal is to code a simulation to visualize this paradox.

You can learn about the Parrondo’s paradox here: https://en.wikipedia.org/wiki/Parrondo%27s_paradox

Step 1

Create a function named play_game_A that takes a single argument: - current_balance: An integer representing the current balance or score.

  1. Simulate a game outcome using random number generation. For this game:
    • There’s a 5% chance of winning, leading to earning a point.
    • There’s a 95% chance of losing, leading to losing a point.
  2. Return the resulting outcome, which will be current_balance +/- 1.

How do I generate a random number?

For now, use the following code to generate a random number between 0 and 100.

import random

random.randint(0, 100)

What is import random? We will see it at the end of this course ;)

Step 2

Create a function named play_game_B that takes a single argument: - current_balance: An integer representing the current balance or score.

The function should:

  1. Use the current balance to decide which variant of Game B to play:
    • If it is an even number, you earn three points.
    • Otherwise, you lose five points.
  2. Return the resulting outcome, which will be current_balance (+ 3) or (- 5).

Step 3

Create a function simulate_game that takes three parameters: 1. game: A function that represents the game to play. 2. rounds: An integer (default to 1000) that represents how many rounds will be played. 3. initial_capital: An integer (default to 10) representing the starting capital.

The function should: 1. Simulate playing the game for the given number of rounds. 2. Return a list representing the capital after each round.

Observation: the game does not stop even if you reach 0 points. You can get negative scores!

Then run the function for both games, and store the resulting list in a dictionary, like this:

results = {
  "Game A": [10, ...],
  "Game B": [10, ...]
}

Now pass that dictionary to plot_results, defined below. You will be able to see how your balance decreases with each game!

import matplotlib.pyplot as plt

def plot_results(results: dict[str, list[int]]):
  """
  Plot the account balance, respresented by list,
  stored in a dictionary

  Parameters
  ----------
  results : dict
    Contains lists, representing our balance after each round
  """
  # Plot the balance of each game
  for key, value in results.items():
    plt.plot(value, label=key)
  plt.legend()
  plt.ylabel("Balance")
  plt.xlabel("Number of rounds")
  plt.show()
  plt.close()

# Example
results = {
    "Game A": [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    "Game B": [10, 13, 8, 11, 6, 9, 4, 7, 2, 5, 0]
}

plot_results(results)

Step 4

Create a function simulate_alternating_games that takes four parameters: 1. game_A: A function that represents the first game to play. 2. game_B: A function that represents the second game to play. 1. rounds: An integer (default to 1000) that represents how many rounds will be played. 2. initial_capital: An integer (default to 10) representing the starting capital.

The function should: 1. Simulate playing games A and B alternately for the given number of rounds. 2. Return a list representing the capital after each round.

Now store the results in the same dictionary you defined in the previous step, and plot them again.