fork(1) download
  1. #!/bin/bash
  2.  
  3. # MartingaleSimulator v2
  4.  
  5. startBetAmount=1
  6. startBankAmount=10000
  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. greatestWin=0
  24. greatestWinBetNumber=0
  25. biggestLoss=0
  26. greatestLossBetNumber=0
  27.  
  28. #echo numBetsCast=$numBetsCast
  29. echo currentBetAmount=$currentBetAmount
  30. echo currentBankAmount=$currentBankAmount
  31.  
  32. echo "Running simlation"
  33.  
  34. while (( "${currentBankAmount}" >= "${currentBetAmount}" )); do
  35.  
  36. numBetsCast=$((${numBetsCast} + 1))
  37.  
  38. coinToss=$((${RANDOM} % 2))
  39.  
  40. if (( ${coinToss} == 0 )); then
  41. #echo "We won!!!!"
  42. currentBankAmount=$(("${currentBankAmount}" + "${currentBetAmount}"))
  43.  
  44. if (( ${currentBankAmount} > ${greatestWin} )); then
  45. greatestWin=${currentBankAmount}
  46. greatestWinBetNumber=${numBetsCast}
  47. #printf "Bet ${greatestWinBetNumber} put our bank at \$${greatestWin}\n"
  48. fi
  49.  
  50. currentBetAmount=1
  51.  
  52. else
  53. #echo "We lost!!! Double the bet and roll again!"
  54. currentBankAmount=$(("${currentBankAmount}" - "${currentBetAmount}"))
  55. newBetAmount=$(("${currentBetAmount}" * 2))
  56.  
  57. if (( ${currentBetAmount} > ${biggestLoss} )); then
  58. biggestLoss=${currentBetAmount}
  59. greatestLossBetNumber=${numBetsCast}
  60. printf "Bet %-10b is our biggest loss yet!! Doubling bet to %-10b. Current balance is %-10b\n" "${greatestLossBetNumber}" "\$${newBetAmount}" "\$${currentBankAmount}"
  61. fi
  62.  
  63. currentBetAmount="${newBetAmount}"
  64.  
  65. fi
  66. done
  67.  
  68. echo "Cannot afford to double bet to \$${currentBetAmount}. You are forced to abandon the martingale strategy since you only have \$${currentBankAmount} left."
  69. echo "You have cast a total of ${numBetsCast} bets."
  70. echo "Should have quit at bet ${greatestWinBetNumber} when you were ahead by \$$((${greatestWin} - ${startBankAmount}))"
Success #stdin #stdout 0.12s 5504KB
stdin
Standard input is empty
stdout
currentBetAmount=1
currentBankAmount=10000
Running simlation
Bet 3          is our biggest loss yet!! Doubling bet to $2        . Current balance is $10001    
Bet 20         is our biggest loss yet!! Doubling bet to $4        . Current balance is $10010    
Bet 39         is our biggest loss yet!! Doubling bet to $8        . Current balance is $10016    
Bet 40         is our biggest loss yet!! Doubling bet to $16       . Current balance is $10008    
Bet 53         is our biggest loss yet!! Doubling bet to $32       . Current balance is $9997     
Bet 209        is our biggest loss yet!! Doubling bet to $64       . Current balance is $10038    
Bet 210        is our biggest loss yet!! Doubling bet to $128      . Current balance is $9974     
Bet 1179       is our biggest loss yet!! Doubling bet to $256      . Current balance is $10352    
Bet 1180       is our biggest loss yet!! Doubling bet to $512      . Current balance is $10096    
Bet 2311       is our biggest loss yet!! Doubling bet to $1024     . Current balance is $10140    
Bet 3276       is our biggest loss yet!! Doubling bet to $2048     . Current balance is $9589     
Bet 3277       is our biggest loss yet!! Doubling bet to $4096     . Current balance is $7541     
Bet 3278       is our biggest loss yet!! Doubling bet to $8192     . Current balance is $3445     
Cannot afford to double bet to $8192. You are forced to abandon the martingale strategy since you only have $3445 left.
You have cast a total of 3278 bets.
Should have quit at bet 3265 when you were ahead by $1636