Time for action – simulating a game show

Imagine a game show where every time the contestants answer a question correctly, they get to pull three balls from a jar and then put them back. Now there is a catch, there is one ball in there that is bad. Every time it is pulled out, the contestants lose six points. If however, they manage to get out three of the 25 normal balls, they get one point. So, what is going to happen if we have 100 questions in total? In order to get a solution for this, go through the following steps:

  1. Initialize the outcome of the game with the hypergeometric function. The first parameter of this function is the number of ways to make a good selection, the second parameter is the number of ways to make a bad selection, and the third parameter is the number of items sampled.
    points = np.zeros(100)
    outcomes = np.random.hypergeometric(25, 1, 3, size=len(points))
  2. Set the scores based on the outcomes from the previous step.
    for i in range(len(points)):
        if outcomes[i] == 3:
          points[i] = points[i - 1] + 1
        elif outcomes[i] == 2:
          points[i] = points[i - 1] - 6
        else:
          print outcomes[i]
  3. Plot the points array with Matplotlib.
    plot(np.arange(len(points)), points)
    show()

    The following screenshot shows how the scoring evolved:

    Time for action – simulating a game show

What just happened?

We simulated a game show using the hypergeometric function from the NumPy random module. The game scoring depends on how many good and how many bad balls are pulled out of a jar in each session (see urn.py).

import numpy as np
from matplotlib.pyplot import plot, show

points = np.zeros(100)
outcomes = np.random.hypergeometric(25, 1, 3, size=len(points))

for i in range(len(points)):
   if outcomes[i] == 3:
      points[i] = points[i - 1] + 1
   elif outcomes[i] == 2:
      points[i] = points[i - 1] - 6
   else:
      print outcomes[i]

plot(np.arange(len(points)), points)
show()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset