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:
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))
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]
points
array with Matplotlib.plot(np.arange(len(points)), points) show()
The following screenshot shows how the scoring evolved:
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()