Bash game: the Monty Hall problem

This article has been originally published in Italian (here). Feedbacks on content and translation are appreciated. Contributions are welcome. The original article must be considered the reference in case of updates.

Some time ago I frowned a little over the Randall Munroe’s XKCD comic shown below: which weird story could involve a goat in a TV show? The picture’s alt text wasn’t helpful, either: “A few minutes later, the goat from behind door C drives away in the car“. So, two goats, a car, a TV show and a goat-loving guy. Even weirder than before.

Soon I had to summon the power of Wikipedia to get some hints. Long story, short:

The Monty Hall problem is a brain teaser, in the form of a probability puzzle (Gruber, Krauss and others), loosely based on the American television game show Let’s Make a Deal and named after its original host, Monty Hall. The problem was originally posed in a letter by Steve Selvin to the American Statistician in 1975 (Selvin 1975a), (Selvin 1975b). It became famous as a question from a reader’s letter quoted in Marilyn vos Savant’s “Ask Marilyn” column in Parade magazine in 1990 (vos Savant 1990a):

Suppose you’re on a game show, and you’re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, “Do you want to pick door No. 2?” Is it to your advantage to switch your choice?

Vos Savant’s response was that the contestant should switch to the other door (vos Savant 1990a). Under the standard assumptions, contestants who switch have a 2/3 chance of winning the car, while contestants who stick to their choice have only a 1/3 chance.

All clear now, let’s go for a game! Would it be easier to look for a ready-to-play game? Maybe. Would it be funnier to make my own Monty Hall game? Yes, of course. Time to punch in some random bash code, as I already did (and enjoyed!) with “Rock, Paper, Scissors, Lizard, Spock“.

So, here you are, ladies and gentlemen, the Great Monty Hall bash game, in all (uh?) its glory:

# Monty Hall game
echo "Welcome to the Monty Hall game,
you can win a car or a goat"
while [[ ${onemore^} = "Y" ]]
declare -a doors
for i in {1..3}
if [[ $i -ne $win_door ]]
echo '.---..---..---.
| 1 || 2 || 3 |
until [[ $guess =~ [1|2|3] ]]
echo -n "Please choose one of the doors: (1, 2, 3)> "
read -n 1 guess
declare -a rem_doors
#assigns to array values different from $guess
for i in {1..3}
if [[ $i -ne $guess ]]
let j+=1
if [[ ${doors[${rem_doors[1]}]} = ${doors[${rem_doors[2]}]} ]] #two goats
until [[ ${doors[${rem_doors[$j]}]} = "goat" ]] #find the goat!
let j+=1
last_door=${rem_doors[$((j%2+1))]} # switch btw 1 and 2 maths hack!
echo -n "Behind door $other_goat there's a goat.
Do you want to change your choice to door $last_door? (y/n)> "
read -n 1 switch
if [[ ${switch^} = "Y" ]]
let stat_change+=1
echo -e "\\nNice! Now your choice is door number $last_door."
if [[ $guess -eq $win_door ]]
echo -e "\\nCongratulations! You won the car!"
let stat_win+=1
echo -e "\\nHere's your brand new... goat! Take care of it!"
let stat_lose+=1
echo "Stats: $((stat_lose+stat_win)) turn(s) played [Win $stat_win / Lose $stat_lose] - Changed door $stat_change time(s)"
echo -n "One more turn (y/n)?> "
read -n 1 onemore && echo
if [[ ${onemore^} != "Y" ]]
echo "Well, I see, you goat-ta go... Bye!"

Still a little rough, perhaps a full HD goat or a 3D car would be nice but still… Creating the “logic” that impersonates mr. Monty Hall was probably the hardest part of the task (that is, “knowing” where the car is and proposing the right door for the change). Though, I still think I could have come up with a more clever trick to drive the choices and the change proposals.

A little history of the script: reading from the Wiki, I learned that even

Paul Erdős, one of the most prolific mathematicians in history, remained unconvinced until he was shown a computer simulation confirming the predicted result

so I… basically… made a computer simulation to confirm the predicted result. Actually the very first draft of the script was just meant to play with itself… ehm… no, this sounds worse than it is…

Ok, it automatically played a huge number of games, both changing or not changing door, and collected wins (cars) and losses (goats): one of those stupid boring tasks computers are so good at. Y’ know? It worked: to win a goat you really have to keep the door you chose first! That’s a little wonderful result I got with BAAAAsh!

And now, live from my desktop, ladies and gentlemen please welcome mr. Monty “The Script” Hall!!!



Informazioni su Man from Mars

  1. juhan

    MfM rockz!

    Mi piace

  2. Bello !!
    Ora lo linko al post ove accenno di questo “problema” !

    Grazie! Da parte mia e della mia prigrizia :-p

    Mi piace

  3. Ma non avevi linkato già l’originale in italiano? Boh… Comunque grazie! 🙂

    Mi piace

  4. no, mi era sfuggito! nonostante tutto non riesco a leggere tutto cio’ che vorrei.

    Mi piace

Comments are disabled. Please comment and share on Twitter, G+, Facebook.

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:


Stai commentando usando il tuo account Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: