fork(1) 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. 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}" >= 0 )); 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 "Bankrupt after ${numBetsCast} bets cast."
  69. echo "Should have quit at bet ${greatestWinBetNumber} when you were ahead by \$$((${greatestWin} - ${startBankAmount}))"
Success #stdin #stdout 0.96s 5548KB
stdin
Standard input is empty
stdout
currentBetAmount=1
currentBankAmount=1000
Running simlation
Bet 1          is our biggest loss yet!! Doubling bet to $2        . Current balance is $999      
Bet 12         is our biggest loss yet!! Doubling bet to $4        . Current balance is $1003     
Bet 13         is our biggest loss yet!! Doubling bet to $8        . Current balance is $999      
Bet 14         is our biggest loss yet!! Doubling bet to $16       . Current balance is $991      
Bet 15         is our biggest loss yet!! Doubling bet to $32       . Current balance is $975      
Bet 152        is our biggest loss yet!! Doubling bet to $64       . Current balance is $1013     
Bet 153        is our biggest loss yet!! Doubling bet to $128      . Current balance is $949      
Bet 187        is our biggest loss yet!! Doubling bet to $256      . Current balance is $831      
Bet 188        is our biggest loss yet!! Doubling bet to $512      . Current balance is $575      
Bet 2101       is our biggest loss yet!! Doubling bet to $1024     . Current balance is $1027     
Bet 2102       is our biggest loss yet!! Doubling bet to $2048     . Current balance is $3        
Bet 25236      is our biggest loss yet!! Doubling bet to $4096     . Current balance is $9452     
Bet 25237      is our biggest loss yet!! Doubling bet to $8192     . Current balance is $5356     
Bet 28644      is our biggest loss yet!! Doubling bet to $16384    . Current balance is $-1058    
Bankrupt after 28644 bets cast.
Should have quit at bet 28630 when you were ahead by $14325