#!/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"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."