Pieters bash scripts
Documentation for the bash scripts I have published
Loading...
Searching...
No Matches
monty_hall.sh
Go to the documentation of this file.
1#!/bin/bash
2##! Author: Pieter van der Star (info@pietervanderstar.nl)
3##! Modifications by: (unmodified)
4##!
5##! This program is free software: you can redistribute it and/or modify
6##! it under the terms of the GNU Affero General Public License as
7##! published by the Free Software Foundation, either version 3 of the
8##! License, or (at your option) any later version.
9##!
10##! This program is distributed in the hope that it will be useful,
11##! but WITHOUT ANY WARRANTY; without even the implied warranty of
12##! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13##! GNU Affero General Public License for more details.
14##!
15##! You should have received a copy of the GNU Affero General Public License
16##! along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18#Changelog:
19#┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓
20#┃ 3 jun 2022 │ Updated to work in bash │ Pieter van der Star ┃
21#┠───────────────┼──────────────────────────────────────────┼──────────────────────────┨
22#┃ 3 jun 2022 │ First release │ Pieter van der Star ┃
23#┠───────────────┼──────────────────────────────────────────┼──────────────────────────┨
24#┃ 21 feb 2024 │ Added doxygen documenation comments │ Pieter van der Star ┃
25#┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┛
26
27##! @file
28##! @brief This script simulates the Monty Hall problem to help decide on a strategy.
29
30echo " __ __ _ _ _ _ _ "
31echo "| \\/ | ___ _ __ | |_ _ _ | | | | __ _ | | | |"
32echo "| |\\/| | / _ \\ | '_ \\ | __| | | | | | |_| | / _\` | | | | |"
33echo "| | | | | (_) | | | | | | |_ | |_| | | _ | | (_| | | | | |"
34echo "|_| |_| \\___/ |_| |_| \\__| \\__, | |_| |_| \\__,_| |_| |_|"
35echo " |___/"
36echo
37echo " _ _"
38echo " _ __ _ __ ___ | |__ | | ___ _ __ ___ "
39echo "| '_ \\ | '__| / _ \\ | '_ \\ | | / _ \\ | '_ \` _ \\ "
40echo "| |_) | | | | (_) | | |_) | | | | __/ | | | | | |"
41echo "| .__/ |_| \\___/ |_.__/ |_| \\___| |_| |_| |_|"
42echo "|_|"
43
44
45
46
47echo "Behind one of the doors is a car you can win."
48echo "Behind which door is the car?"
49echo " ___ ___ ___"
50echo " | | | | | |"
51echo " | A | | B | | C |"
52echo " | | | | | |"
53echo " |___| |___| |___|"
54echo
55echo "You choose A."
56echo "Let's open another door."
57echo
58echo " ___ ___ ___"
59echo " | | | | || | )_)"
60echo " | A | | B | ||(\\_|__'\\_\\"
61echo " | | | | ||| | \""
62echo " |___| |___| |||\"\"\"\"\"\"|"
63
64echo "Now do you want to switch?"
65echo "Should you?"
66echo "We can run this a couple of times, keep track of when we win if we"
67echo "switch and if we don't and see what happens."
68
69if [ $# -lt 1 ]; then
70 echo "Running 100 times, you can change this by passing a number as first argument."
71 numrounds=100;
72else
73 numrounds=$1;
74fi
75if [ $# -lt 2 ]; then
76 verbosity=0;
77else
78 verbosity=$2;
79fi
80
81##! @brief Swap your choice from the chosen one to the other unrevealed door.
82##! @param $arg1 Position of the prize.
83##! @param $arg2 Position of the choice.
84##! @returns New choice position.
85function swap {
86 prizepos=$1;
87 guess=$2;
88 opening=0;
89 while [ $guess -eq $opening ] || [ $prizepos -eq $opening ]; do
90 opening=$((opening + 1))
91 opening=$((opening % 3))
92 done
93 if [ $verbosity -ge 2 ]; then
94 echo "opening $opening"
95 fi
96 newchoice=$((guess+1));
97 newchoice=$((newchoice % 3));
98 while [ $newchoice -eq $opening ]; do
99 newchoice=$((newchoice + 1));
100 newchoice=$((newchoice % 3));
101 done
102 return $newchoice;
103}
104
105##! Number of wins for player A
106winsa=0;
107##! Number of wins for player B
108winsb=0;
109##! Position of the prize
110prizepos=0;
111##! Original guess
112guess=0;
113##! Counter for the number of rounds already done.
114i=1;
115##! Counts how often the random number was 0. Used for statistics at the end of the script.
116rand[0]=0;
117##! Counts how often the random number was 1. Used for statistics at the end of the script.
118rand[1]=0;
119##! Counts how often the random number was 2. Used for statistics at the end of the script.
120rand[2]=0;
121while [ $i -le $numrounds ]; do
122 prizepos=$(($RANDOM % 3));
123 rand[$prizepos]=$((rand[$prizepos]+1));
124 guess=$(($RANDOM % 3));
125 rand[$guess]=$((rand[$guess]+1));
127 newguess=$?
128 if [ $verbosity -ge 1 ]; then
129 echo "prize: $prizepos a: $guess b: $newguess"
130 echo "---------------------------------------"
131 fi
132 if [ $guess -eq $prizepos ]; then
133 winsa=$((winsa+1));
134 fi
135 if [ $newguess -eq $prizepos ]; then
136 winsb=$((winsb+1));
137 fi
138 if [[ $verbosity -ge 1 && $numrounds -lt 1000 ]]; then
139 apercent=$((winsa*100/i))
140 bpercent=$((winsb*100/i))
141 echo "keep: $winsa $apercent% swap: $winsb $bpercent%";
142 fi
143 if [ $((i%1000)) -eq 0 ]; then
144 apercent=$((winsa*100/i))
145 bpercent=$((winsb*100/i))
146 if [ $verbosity -ge 1 ]; then
147 echo "keep: $winsa $apercent% swap: $winsb $bpercent%";
148 fi
149 print "progress: $(((i*1000/numrounds)/10)).$(((i*1000/numrounds)%10))%";
150 fi
151 ((i+=1))
152done
153
154 ((i-=1))
155##! Percentage of wins for player A
156apercent=$((winsa*100/i))
157##! Percentage of wins for player B
158bpercent=$((winsb*100/i))
159printf "Let's look at the results. How often do you win:\n"
160printf " |keep|swap|\n"
161printf " |----|----|\n"
162printf " |%4d|%4d|\n" $winsa $winsb
163printf " |%3d%%|%3d%%|\n" $apercent $bpercent
164echo "Assuming you ran this for enough tests, you see you should swap."
165
166printf "Random number distribution:\n"
167printf " | 0 | 1 | 2 |\n"
168printf " |-----|-----|-----|\n"
169printf " |%5d|%5d|%5d|\n" ${rand[0]} ${rand[1]} ${rand[2]}
170printf " |%2d.%d%%|%2d.%d%%|%2d.%d%%|\n" $(((rand[0]*1000/(2*i)/10))) $(((rand[0]*1000/(2*i)%10))) $(((rand[1]*1000/(2*i)/10))) $(((rand[1]*1000/(2*i)%10))) $(((rand[2]*1000/(2*i)/10))) $(((rand[2]*1000/(2*i)%10)))
$bpercent
Percentage of wins for player B.
$winsa
Number of wins for player A.
$guess
Original guess.
$i
Counter for the number of rounds already done.
$rand[0]
Counts how often the random number was 0. Used for statistics at the end of the script.
$winsb
Number of wins for player B.
$prizepos
Position of the prize.
$apercent
Percentage of wins for player A.
swap($arg1, $arg2)
Swap your choice from the chosen one to the other unrevealed door.
Definition monty_hall.sh:85