Monty Hall problem: Difference between revisions

no edit summary
(J: add a small bit of explanation)
No edit summary
Line 316:
Algorithm random: prize count = 4991, = 49.91%
bash$</lang>
 
=={{header|bash}}==
{{works with|bash 2.x| and compatible unix shells}}
<lang bash>
#!/bin/sh
# Simulates the "monty hall" probability paradox and shows results.
# http://en.wikipedia.org/wiki/Monty_Hall_problem
# Should rewrite this in C for faster calculating of huge number of rounds.
# (Hacked up by Éric Tremblay, 07.dec.2010)
 
num_doors=3 # default number of doors
num_rounds=10 #default number of rounds
[ "$1" = "" ] || num_doors=$[$1+0]
[ "$2" = "" ] || num_rounds=$[$2+0]
 
nbase=1 # or 0 if we want to see door numbers zero-based
num_win=0; num_lose=0
 
echo "$num_doors doors, playing $num_rounds times."
[ "$num_doors" -lt 3 ] && {
echo "Hey, there has to be at least 3 doors!!"
exit 1
}
echo
 
function one_round() {
winning_door=$[$RANDOM % $num_doors ]
player_picks_door=$[$RANDOM % $num_doors ]
host_leaves_door=$winning_door
while [ "$host_leaves_door" = "$player_picks_door" ]; do
#echo -n "(Host looks at door $host_leaves_door...) "
host_leaves_door=$[$RANDOM % $num_doors]
done
#echo
#echo "Round $[$nbase+current_round]: "
echo -n "Player chooses #$[$nbase+$player_picks_door]. "
[ "$num_doors" -ge 10 ] &&
# listing too many door numbers (10 or more) will just clutter the output
echo -n "Host opens all except #$[$nbase+$host_leaves_door] and #$[$nbase+$player_picks_door]. " \
|| {
# less than 10 doors, we list them one by one instead of "all except ?? and ??"
echo -n "Host opens"
host_opens=0
while [ "$host_opens" -lt "$num_doors" ]; do
[ "$host_opens" != "$host_leaves_door" ] && [ "$host_opens" != "$player_picks_door" ] && \
echo -n " #$[$nbase+$host_opens]"
host_opens=$[$host_opens+1]
done
echo -n " "
}
echo -n "(prize is behind #$[$nbase+$winning_door]) "
echo -n "Switch from $[$nbase+$player_picks_door] to $[$nbase+$host_leaves_door]: "
[ "$winning_door" = "$host_leaves_door" ] && {
echo "WIN."
num_win=$[num_win+1]
} || {
echo "LOSE."
num_lose=$[num_lose+1]
}
}
 
# ok, let's go
current_round=0
while [ "$num_rounds" -gt "$current_round" ]; do
one_round
current_round=$[$current_round+1]
done
echo
echo "Wins: $num_win, Losses: $num_lose"
exit 0
</lang>
Output of a few runs:
<pre>
$ ./monty_hall_problem.sh
3 doors, playing 10 times.
 
Player chooses #2. Host opens #1 (prize is behind #3) Switch from 2 to 3: WIN.
Player chooses #2. Host opens #3 (prize is behind #1) Switch from 2 to 1: WIN.
Player chooses #2. Host opens #1 (prize is behind #3) Switch from 2 to 3: WIN.
Player chooses #2. Host opens #1 (prize is behind #2) Switch from 2 to 3: LOSE.
Player chooses #2. Host opens #1 (prize is behind #3) Switch from 2 to 3: WIN.
Player chooses #3. Host opens #2 (prize is behind #1) Switch from 3 to 1: WIN.
Player chooses #3. Host opens #2 (prize is behind #3) Switch from 3 to 1: LOSE.
Player chooses #2. Host opens #3 (prize is behind #2) Switch from 2 to 1: LOSE.
Player chooses #3. Host opens #1 (prize is behind #3) Switch from 3 to 2: LOSE.
Player chooses #2. Host opens #3 (prize is behind #1) Switch from 2 to 1: WIN.
 
Wins: 6, Losses: 4
 
$ ./monty_hall_problem.sh 10 5
10 doors, playing 5 times.
 
Player chooses #8. Host opens all except #1 and #8. (prize is behind #8) Switch from 8 to 1: LOSE.
Player chooses #2. Host opens all except #3 and #2. (prize is behind #3) Switch from 2 to 3: WIN.
Player chooses #9. Host opens all except #2 and #9. (prize is behind #2) Switch from 9 to 2: WIN.
Player chooses #6. Host opens all except #8 and #6. (prize is behind #8) Switch from 6 to 8: WIN.
Player chooses #5. Host opens all except #7 and #5. (prize is behind #7) Switch from 5 to 7: WIN.
 
Wins: 4, Losses: 1
 
$ ./monty_hall_problem.sh 3 1000
3 doors, playing 1000 times.
 
Player chooses #3. Host opens #2 (prize is behind #3) Switch from 3 to 1: LOSE.
Player chooses #3. Host opens #2 (prize is behind #1) Switch from 3 to 1: WIN.
Player chooses #2. Host opens #3 (prize is behind #1) Switch from 2 to 1: WIN.
 
[ ... ]
 
Player chooses #1. Host opens #3 (prize is behind #2) Switch from 1 to 2: WIN.
Player chooses #3. Host opens #1 (prize is behind #3) Switch from 3 to 2: LOSE.
Player chooses #3. Host opens #1 (prize is behind #2) Switch from 3 to 2: WIN.
 
Wins: 655, Losses: 345
</pre>
 
=={{header|BASIC}}==
Anonymous user