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:

#!/bin/bash
# Monty Hall game
stat_win=0
stat_lose=0
stat_change=0
onemore=Y
echo "Welcome to the Monty Hall game,
you can win a car or a goat"
while [[ ${onemore^} = "Y" ]]
do
declare -a doors
win_door=$(($RANDOM%3+1))
doors[$win_door]="car"
for i in {1..3}
do
if [[ $i -ne $win_door ]]
then
doors[$i]="goat"
fi
done
echo '.---..---..---.
| 1 || 2 || 3 |
°---°°---°°---°'
guess=0
until [[ $guess =~ [1|2|3] ]]
do
echo -n "Please choose one of the doors: (1, 2, 3)> "
read -n 1 guess
echo
done
declare -a rem_doors
#assigns to array values different from $guess
j=1
for i in {1..3}
do
if [[ $i -ne $guess ]]
then
rem_doors[$j]=$i
let j+=1
fi
done
if [[ ${doors[${rem_doors[1]}]} = ${doors[${rem_doors[2]}]} ]] #two goats
then
other_goat=${rem_doors[$((RANDOM%2+1))]}
else
j=1
until [[ ${doors[${rem_doors[$j]}]} = "goat" ]] #find the goat!
do
let j+=1
done
other_goat=${rem_doors[$j]}
fi
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" ]]
then
let stat_change+=1
echo -e "\\nNice! Now your choice is door number $last_door."
guess=$last_door
fi
if [[ $guess -eq $win_door ]]
then
echo -e "\\nCongratulations! You won the car!"
let stat_win+=1
else
echo -e "\\nHere's your brand new... goat! Take care of it!"
let stat_lose+=1
fi
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" ]]
then
echo "Well, I see, you goat-ta go... Bye!"
fi
done
exit

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!!!

Goat

Informazioni su Man from Mars

https://extendedreality.wordpress.com/

  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

Dimmi che ne pensi o fai "Ciao ciao!" con la manina // Share your thoughts or just say "Hello!"

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

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. 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 cliccano Mi Piace per questo: