Blackjack Monte Carlo: Strategy & Solvency Simulator
It started with a simple question: if you play perfect basic strategy, or count cards, how long does your money actually last? So I wrote a simulator that plays millions of hands and tracks the bankroll the way an actuary tracks an insurer's surplus. What's the expected return, and what are the odds of going broke before you see it?
- Compares expected value, standard deviation and Sharpe ratio across 5 strategies, including a random baseline
- Models bankroll solvency and risk of ruin when you size bets with the Kelly Criterion
- Measures how much card counting actually helps, and plots random-walk paths to show the variance
- Every assumption is configurable, and tests run in CI on GitHub Actions
# the core loop, more or less
for path in range(paths):
bank = bankroll
for r in range(rounds):
stake = max(unit, k * f_star * bank)
bank += stake * b if rng() < p else -stake
if bank < unit: ruined += 1; break
# f* = (b·p − q) / b
risk_of_ruin = ruined / paths