Line numbers

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
#!/bin/bash
##! Author: Pieter van der Star (info@pietervanderstar.nl)
##! Modifications by: (unmodified)
##!
##! This program is free software: you can redistribute it and/or modify
##! it under the terms of the GNU Affero General Public License as
##! published by the Free Software Foundation, either version 3 of the
##! License, or (at your option) any later version.
##!
##! This program is distributed in the hope that it will be useful,
##! but WITHOUT ANY WARRANTY; without even the implied warranty of
##! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##! GNU Affero General Public License for more details.
##!
##! You should have received a copy of the GNU Affero General Public License
##! along with this program. If not, see <https://www.gnu.org/licenses/>.

#Changelog:
#┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓
#┃ 3 jun 2022 │ Updated to work in bash │ Pieter van der Star ┃
#┠───────────────┼──────────────────────────────────────────┼──────────────────────────┨
#┃ 3 jun 2022 │ First release │ Pieter van der Star ┃
#┠───────────────┼──────────────────────────────────────────┼──────────────────────────┨
#┃ 21 feb 2024 │ Added doxygen documenation comments │ Pieter van der Star ┃
#┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┛

##! @file
##! @brief This script simulates the Monty Hall problem to help decide on a strategy.

echo " __  __                   _               _   _           _   _ "
echo "|  \\/  |   ___    _ __   | |_   _   _    | | | |   __ _  | | | |"
echo "| |\\/| |  / _ \\  | '_ \\  | __| | | | |   | |_| |  / _\` | | | | |"
echo "| |  | | | (_) | | | | | | |_  | |_| |   |  _  | | (_| | | | | |"
echo "|_|  |_|  \\___/  |_| |_|  \\__|  \\__, |   |_| |_|  \\__,_| |_| |_|"
echo "                                |___/"
echo 
echo "                        _       _"
echo " _ __    _ __    ___   | |__   | |   ___   _ __ ___  "
echo "| '_ \\  | '__|  / _ \\  | '_ \\  | |  / _ \\ | '_ \` _ \\ "
echo "| |_) | | |    | (_) | | |_) | | | |  __/ | | | | | |"
echo "| .__/  |_|     \\___/  |_.__/  |_|  \\___| |_| |_| |_|"
echo "|_|"




echo "Behind one of the doors is a car you can win."
echo "Behind which door is the car?"
echo "  ___    ___    ___"
echo " |   |  |   |  |   |"
echo " | A |  | B |  | C |"
echo " |   |  |   |  |   |"
echo " |___|  |___|  |___|"
echo 
echo "You choose A."
echo "Let's open another door."
echo 
echo "  ___    ___    ___"
echo " |   |  |   | ||   |   )_)"
echo " | A |  | B | ||(\\_|__'\\_\\"
echo " |   |  |   | |||      | \""echo " |___|  |___| |||\"\"\"\"\"\"|"echo "Now do you want to switch?"echo "Should you?"echo "We can run this a couple of times, keep track of when we win if we"echo "switch and if we don't and see what happens."if [ $# -lt 1 ]; then    echo "Running 100 times, you can change this by passing a number as first argument."    numrounds=100;else     numrounds=$1;fiif [ $# -lt 2 ]; then    verbosity=0;else     verbosity=$2;fi##! @brief Swap your choice from the chosen one to the other unrevealed door.##! @param $arg1 Position of the prize.##! @param $arg2 Position of the choice.##! @returns New choice position.function swap {    prizepos=$1;    guess=$2;    opening=0;    while [ $guess -eq $opening ] || [ $prizepos -eq $opening ]; do        opening=$((opening + 1))        opening=$((opening % 3))    done    if [ $verbosity -ge 2 ]; then        echo "opening $opening"    fi    newchoice=$((guess+1));    newchoice=$((newchoice % 3));    while [ $newchoice -eq $opening ]; do        newchoice=$((newchoice + 1));        newchoice=$((newchoice % 3));    done    return $newchoice;}##! Number of wins for player Awinsa=0;##! Number of wins for player Bwinsb=0;##! Position of the prizeprizepos=0;##! Original guessguess=0;##! Counter for the number of rounds already done.i=1;##! Counts how often the random number was 0. Used for statistics at the end of the script.rand[0]=0;##! Counts how often the random number was 1. Used for statistics at the end of the script.rand[1]=0;##! Counts how often the random number was 2. Used for statistics at the end of the script.rand[2]=0;while [ $i -le $numrounds ]; do    prizepos=$(($RANDOM % 3));    rand[$prizepos]=$((rand[$prizepos]+1));    guess=$(($RANDOM % 3));    rand[$guess]=$((rand[$guess]+1));    swap $prizepos $guess    newguess=$?    if [ $verbosity -ge 1 ]; then        echo "prize: $prizepos a: $guess b: $newguess"        echo "---------------------------------------"    fi    if [ $guess -eq $prizepos ]; then         winsa=$((winsa+1));    fi    if [ $newguess -eq $prizepos ]; then         winsb=$((winsb+1));    fi    if [[ $verbosity -ge 1 && $numrounds -lt 1000 ]]; then        apercent=$((winsa*100/i))        bpercent=$((winsb*100/i))        echo "keep: $winsa $apercent% swap: $winsb $bpercent%";    fi    if [ $((i%1000)) -eq 0 ]; then        apercent=$((winsa*100/i))        bpercent=$((winsb*100/i))        if [ $verbosity -ge 1 ]; then            echo "keep: $winsa $apercent% swap: $winsb $bpercent%";        fi            print "progress: $(((i*1000/numrounds)/10)).$(((i*1000/numrounds)%10))%";    fi    ((i+=1))done    ((i-=1))##! Percentage of wins for player Aapercent=$((winsa*100/i))##! Percentage of wins for player Bbpercent=$((winsb*100/i))printf "Let's look at the results. How often do you win:\n"
printf "                            |keep|swap|\n"
printf "                            |----|----|\n"
printf "                            |%4d|%4d|\n" $winsa $winsb
printf "                            |%3d%%|%3d%%|\n" $apercent $bpercent
echo "Assuming you ran this for enough tests, you see you should swap."

printf "Random number distribution:\n"
printf "                            |  0  |  1  |  2  |\n"
printf "                            |-----|-----|-----|\n"
printf "                            |%5d|%5d|%5d|\n" ${rand[0]} ${rand[1]} ${rand[2]}
printf "                            |%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)))