Computer Simulations: How To Simulate A Simple TV Game Using Python

Computer Simulations: How To Simulate A Simple TV Game Using Python

Computer simulations play an important role in many scientific and engineering problems. Depending on the given tasks, different methods are used: someone needs to solve complicated systems of equations which cannot be solved analytically, other perform numerous computations to get enough statistics without real-life experiments. The first mentioned group of tasks is often related to calculations on electric and electronic devices while the latter is often related to the statistics problems (a lot of problems in economy, thermodynamics, etc.). In this article, we discuss an example of how a simple analysis can be performed by the accumulation of the enough number of random events.

Summarizing the video, let’s say that the power of modern computers, not only huge supercomputers but also the home ones, gives us a possibility to make important predictions and impressive results without complicated experiments by anyone of us. And real experiments are usually prepared only after numerous computer simulations which save a lot of money and time.

Probability theory contains a lot of strange and, probably, shocking puzzles. Nevertheless, all of them have a strong mathematical and life background. Let us imagine that you are a television game player. There are three doors in front of you. There is a new Lamborghini behind one of these doors. And there are two seals behind the others. The rules of this game are the following:

  1. If you select the door with Lamborghini – you are a lucky winner.
  2. In another case, the host can open another door with a seal giving you the second attempt

Should you change your suggestion when the host shows seal behind one of these doors? This question is known as Monty Hall problem, named by the host of the television game Let’s Make a Deal started at the American TV in 1963.

So, to change or not to change? An everyday experience says that the probability to guess the right door should not change. The reality is slightly more complicated. Let’s now discuss it in detail.

The first guess is made with the probability 1/3, because there are three equivalent doors. You may seem to deal with the equivalent doors again in the second attempt with the probability to win with 1/2. Nevertheless, the new knowledge appears: the host opens one of the wrong doors and this door has not been selected by you previously. In other words, two attempts are not independent in this game. The initially selected door remains the winning door with the probability 1/3, while opening another wrong door shifts up changes of the last, third, door to 2/3! Isn’t it strange, huh?

Let’s verify this mathematical conclusions with a direct simulation. We speak about Monty Hall problem, so we offer to use a language named in honor about another Monty, Monty Python. The following code for Python 3.x simulates Monty Hall problem for 1,000,000 attempts.

[enlighter lang=”python”]
from random import randint

NN = 1000000; # number of attempts

# counters
winsChange = 0;
winsNoChange = 0;

for n in range(0, NN):
victoryDoor = randint(1, 3); # door with prize
guess1 = 1; # our first guess
masterSuggestion = randint(2, 3); # master suggestion to open the door
while masterSuggestion == victoryDoor:
masterSuggestion = randint(2, 3);
guess2 = 5 – masterSuggestion; # gives 2 if masterSuggestion = 3 and vice versa
if guess1 == victoryDoor: # check victory
winsNoChange += 1;
if guess2 == victoryDoor:
winsChange += 1;

print(‘Wins with change of the door: {0} ({1})\nWins without change of the door: {2} ({3})’.format(winsChange, winsChange/NN, winsNoChange, winsNoChange/NN))
[/enlighter]

The ‘randint’ function from ‘random’ module is used to generate a random integer number from 1 to 3 for doors and guesses. The player always selects the door #1 (it’s enough due to the randomness of the right door). If the guess is right, then the ‘winsNoChange’ variable is incremented, otherwise ‘winsChange’ is incremented. The output of the program is the following:

>>>
Wins with change of the door: 666172 (0.666172)
Wins without change of the door: 333828 (0.333828)
>>>

Thus, there are two times more right guesses after changing the door than without changing one. Math works, guys. And you can try to play Monty Hall problem by yourself here.

5_door_monty_in_teaching

Filed under Programming.
0 0 votes
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments