#!/bin/bash
# MartingaleSimulator v2
startBetAmount=1
startBankAmount=1000
#if [[ ${#} -eq 2 ]]; then
# startBetAmount=${1}
# startBankAmount=${2}
#else
# echo "Enter starting bet (As positive non-zero integer)"
# read -r startBetAmount
#
# echo "Enter bank amount (As positive non-zero integer)"
# read -r startBankAmount
#fi
numBetsCast=0
currentBetAmount=1
currentBankAmount="${startBankAmount}"
sbfw=$((${#startBankAmount} + 2)) #sbfw = starting bank field width. (Used for formatting.)
greatestWin=0
greatestWinBetNumber=0
biggestLoss=0
greatestLossBetNumber=0
#echo numBetsCast=$numBetsCast
echo startBetAmount=${startBetAmount}
echo startBankAmount=${startBankAmount}
bigLossHappened=false
echo "Running simlation"
while (( "${currentBankAmount}" >= "${currentBetAmount}" )); do
numBetsCast=$((${numBetsCast} + 1))
coinToss=$((${RANDOM} % 2))
if (( ${coinToss} == 0 )); then
#echo "We won!!!!"
currentBankAmount=$(("${currentBankAmount}" + "${currentBetAmount}"))
if (( ${currentBankAmount} > ${greatestWin} )); then
greatestWin=${currentBankAmount}
greatestWinBetNumber=${numBetsCast}
#printf "Bet ${greatestWinBetNumber} put our bank at \$${greatestWin}\n"
fi
currentBetAmount=1
else
#echo "We lost!!! Double the bet and roll again!"
currentBankAmount=$(("${currentBankAmount}" - "${currentBetAmount}"))
newBetAmount=$(("${currentBetAmount}" * 2))
if (( ${currentBetAmount} > ${biggestLoss} )); then
biggestLoss=${currentBetAmount}
greatestLossBetNumber=${numBetsCast}
printf "Bet %-${sbfw}b is our biggest loss yet!! Doubling bet to %-${sbfw}b. Current balance dropped from %-${sbfw}b to %-${sbfw}b\n" "${greatestLossBetNumber}" "\$${newBetAmount}" "\$$(("${currentBankAmount}" + "${currentBetAmount}"))" "\$${currentBankAmount}"
bigLossHappened=true
fi
currentBetAmount="${newBetAmount}"
fi
format="Bet %-${sbfw}b Current balance: %-10b Average profit per bet: $%-10.2f"
if ${bigLossHappened}; then
#Uncomment the following line if you need proof the current bank amount does drop.
#ratio=$(echo "scale=2; (${currentBankAmount} - ${startBankAmount}) / ${numBetsCast}" | bc )
#printf "${format}\n" "${numBetsCast}" "\$${currentBankAmount}" "${ratio}"
bigLossHappened=false
else
# This will not ever appear to drop when watching live with human eye because no matter how big the loss,
# the next win will always double it back from the loss.
ratio=$(echo "scale=2; (${currentBankAmount} - ${startBankAmount}) / ${numBetsCast}" | bc )
#Commented out for ideone runtime
#printf "${format}\r" "${numBetsCast}" "\$${currentBankAmount}" "${ratio}"
fi
done
#Last loss may not have been largest loss recorded, so we need to re-calculate the win/loss ratio.
ratio=$(echo "scale=2; (${currentBankAmount} - ${startBankAmount}) / ${numBetsCast}" | bc )
#echo "echo \"scale=2; (${currentBankAmount} - ${startBankAmount}) / ${numBetsCast}\" | bc"=${ratio}
printf "Cannot afford to double bet to \$${currentBetAmount}. You are forced to abandon the martingale strategy since you only have \$${currentBankAmount} left.\n"
printf "You started with \$${startBankAmount}. You now have \$${currentBankAmount}\n"
printf "You have cast a total of ${numBetsCast} bets.\n"
winLossAmt=$((${currentBankAmount} - ${startBankAmount}))
if (( $(echo "${ratio} > 0" | bc -l ) )); then
printf "You came out ahead with a profit of \$${winLossAmt}!!!\n"
printf "Average profit per bet is \$%.2f\n" "${ratio}"
else
printf "You lost a total of \$$((${winLossAmt} * -1))\n"
printf "Average loss per bet is \$%.2f\n" $(echo "scale=2; ${ratio} * -1" | bc)
fi
echo "Should have quit at bet ${greatestWinBetNumber} when you were ahead by \$$((${greatestWin} - ${startBankAmount})) though."
if (( $(echo "${ratio} < 0" | bc -l ) )); then
if [[ "${startBankAmount}" > "10000" ]] && [[ "${currentBankAmount}" < "$((${startBankAmount} / 5))" ]]; then
printf "If you need to talk to someone, please call:\n"
printf "National Suicide Prevention Lifeline - 800-273-8255\n"
elif [[ "${currentBankAmount}" == "0" ]]; then
printf "I hope you didn't bet more than you can afford to lose.\n"
else
printf "I hope \$${currentBankAmount} is enough to get you home.\n"
fi
fi