fork download
  1. #!/bin/bash
  2.  
  3. # MartingaleSimulator v2
  4.  
  5. startBetAmount=1
  6. startBankAmount=1000
  7.  
  8. #if [[ ${#} -eq 2 ]]; then
  9. # startBetAmount=${1}
  10. # startBankAmount=${2}
  11. #else
  12. # echo "Enter starting bet (As positive non-zero integer)"
  13. # read -r startBetAmount
  14. #
  15. # echo "Enter bank amount (As positive non-zero integer)"
  16. # read -r startBankAmount
  17. #fi
  18.  
  19. numBetsCast=0
  20. currentBetAmount=1
  21. currentBankAmount="${startBankAmount}"
  22.  
  23. sbfw=$((${#startBankAmount} + 2)) #sbfw = starting bank field width. (Used for formatting.)
  24.  
  25.  
  26. greatestWin=0
  27. greatestWinBetNumber=0
  28. biggestLoss=0
  29. greatestLossBetNumber=0
  30.  
  31. #echo numBetsCast=$numBetsCast
  32. echo startBetAmount=${startBetAmount}
  33. echo startBankAmount=${startBankAmount}
  34.  
  35.  
  36. bigLossHappened=false
  37.  
  38. echo "Running simlation"
  39.  
  40. while (( "${currentBankAmount}" >= "${currentBetAmount}" )); do
  41.  
  42. numBetsCast=$((${numBetsCast} + 1))
  43.  
  44. coinToss=$((${RANDOM} % 2))
  45.  
  46. if (( ${coinToss} == 0 )); then
  47. #echo "We won!!!!"
  48. currentBankAmount=$(("${currentBankAmount}" + "${currentBetAmount}"))
  49.  
  50. if (( ${currentBankAmount} > ${greatestWin} )); then
  51. greatestWin=${currentBankAmount}
  52. greatestWinBetNumber=${numBetsCast}
  53. #printf "Bet ${greatestWinBetNumber} put our bank at \$${greatestWin}\n"
  54. fi
  55.  
  56. currentBetAmount=1
  57.  
  58. else
  59. #echo "We lost!!! Double the bet and roll again!"
  60. currentBankAmount=$(("${currentBankAmount}" - "${currentBetAmount}"))
  61. newBetAmount=$(("${currentBetAmount}" * 2))
  62.  
  63. if (( ${currentBetAmount} > ${biggestLoss} )); then
  64. biggestLoss=${currentBetAmount}
  65. greatestLossBetNumber=${numBetsCast}
  66. 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}"
  67. bigLossHappened=true
  68. fi
  69.  
  70. currentBetAmount="${newBetAmount}"
  71.  
  72. fi
  73.  
  74. format="Bet %-${sbfw}b Current balance: %-10b Average profit per bet: $%-10.2f"
  75. if ${bigLossHappened}; then
  76. #Uncomment the following line if you need proof the current bank amount does drop.
  77. #ratio=$(echo "scale=2; (${currentBankAmount} - ${startBankAmount}) / ${numBetsCast}" | bc )
  78. #printf "${format}\n" "${numBetsCast}" "\$${currentBankAmount}" "${ratio}"
  79. bigLossHappened=false
  80. else
  81. # This will not ever appear to drop when watching live with human eye because no matter how big the loss,
  82. # the next win will always double it back from the loss.
  83. ratio=$(echo "scale=2; (${currentBankAmount} - ${startBankAmount}) / ${numBetsCast}" | bc )
  84.  
  85. #Commented out for ideone runtime
  86. #printf "${format}\r" "${numBetsCast}" "\$${currentBankAmount}" "${ratio}"
  87. fi
  88. done
  89.  
  90.  
  91. #Last loss may not have been largest loss recorded, so we need to re-calculate the win/loss ratio.
  92. ratio=$(echo "scale=2; (${currentBankAmount} - ${startBankAmount}) / ${numBetsCast}" | bc )
  93. #echo "echo \"scale=2; (${currentBankAmount} - ${startBankAmount}) / ${numBetsCast}\" | bc"=${ratio}
  94.  
  95. printf "Cannot afford to double bet to \$${currentBetAmount}. You are forced to abandon the martingale strategy since you only have \$${currentBankAmount} left.\n"
  96. printf "You started with \$${startBankAmount}. You now have \$${currentBankAmount}\n"
  97. printf "You have cast a total of ${numBetsCast} bets.\n"
  98.  
  99. winLossAmt=$((${currentBankAmount} - ${startBankAmount}))
  100.  
  101.  
  102. if (( $(echo "${ratio} > 0" | bc -l ) )); then
  103.  
  104. printf "You came out ahead with a profit of \$${winLossAmt}!!!\n"
  105. printf "Average profit per bet is \$%.2f\n" "${ratio}"
  106. else
  107. printf "You lost a total of \$$((${winLossAmt} * -1))\n"
  108. printf "Average loss per bet is \$%.2f\n" $(echo "scale=2; ${ratio} * -1" | bc)
  109. fi
  110.  
  111. echo "Should have quit at bet ${greatestWinBetNumber} when you were ahead by \$$((${greatestWin} - ${startBankAmount})) though."
  112.  
  113.  
  114. if (( $(echo "${ratio} < 0" | bc -l ) )); then
  115. if [[ "${startBankAmount}" > "10000" ]] && [[ "${currentBankAmount}" < "$((${startBankAmount} / 5))" ]]; then
  116. printf "If you need to talk to someone, please call:\n"
  117. printf "National Suicide Prevention Lifeline - 800-273-8255\n"
  118. elif [[ "${currentBankAmount}" == "0" ]]; then
  119. printf "I hope you didn't bet more than you can afford to lose.\n"
  120. else
  121. printf "I hope \$${currentBankAmount} is enough to get you home.\n"
  122. fi
  123. fi
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
Success #stdin #stdout 3.49s 5508KB
stdin
Standard input is empty
stdout
startBetAmount=1
startBankAmount=1000
Running simlation
Bet 1      is our biggest loss yet!! Doubling bet to $2    . Current balance dropped from $1000  to $999  
Bet 6      is our biggest loss yet!! Doubling bet to $4    . Current balance dropped from $1001  to $999  
Bet 16     is our biggest loss yet!! Doubling bet to $8    . Current balance dropped from $1005  to $1001 
Bet 56     is our biggest loss yet!! Doubling bet to $16   . Current balance dropped from $1019  to $1011 
Bet 101    is our biggest loss yet!! Doubling bet to $32   . Current balance dropped from $1029  to $1013 
Bet 102    is our biggest loss yet!! Doubling bet to $64   . Current balance dropped from $1013  to $981  
Bet 285    is our biggest loss yet!! Doubling bet to $128  . Current balance dropped from $1080  to $1016 
Bet 1276   is our biggest loss yet!! Doubling bet to $256  . Current balance dropped from $1520  to $1392 
Bet 2425   is our biggest loss yet!! Doubling bet to $512  . Current balance dropped from $1952  to $1696 
Bet 2426   is our biggest loss yet!! Doubling bet to $1024 . Current balance dropped from $1696  to $1184 
Bet 2427   is our biggest loss yet!! Doubling bet to $2048 . Current balance dropped from $1184  to $160  
Cannot afford to double bet to $2048. You are forced to abandon the martingale strategy since you only have $160 left.
You started with $1000. You now have $160
You have cast a total of 2427 bets.
You lost a total of $840
Average loss per bet is $0.34
Should have quit at bet 2416 when you were ahead by $1207 though.
I hope $160 is enough to get you home.