This project simulates and analyzes the game of Blackjack (21) using Monte Carlo methods.
It evaluates different strategies by simulating large numbers of games, computing expected values (EV), win/draw/loss rates, and visualizing performance.
- Overview
- Features
- Project Structure
- Installation
- Methodology
- Compiling
- Outputs & Analysis
- Results & Discussion
- Contact
Blackjack is modeled as a finite-horizon stochastic control problem.
Each state includes:
- Player total
- Soft/hard flag
- Dealer upcard
- Split count
- Double availability
Actions = {Hit, Stand, Double, Split}.
We simulate many rounds to estimate EV with 95% confidence intervals, enabling fair comparisons of strategies and rules.
- Flexible house rules:
- Decks: single or multi-deck
- Dealer S17/H17
- Payout: 3:2 vs 6:5
- Double-after-split, re-splitting, etc.
- Two policies: Basic Strategy vs Naive.
- Supports dataset generation (EV lookups, action heatmaps) and finite-shoe simulation.
- Outputs EV curves, win/loss/push rates, quantile bands.
- All results export to CSV + plots for reproducibility.
simulate-blackjack/
βββ blackjack_pipeline.py # Main pipeline
βββ data/
β βββ blackjack_games.csv
βββ outputs/ # Simulation results (CSVs + plots)
βββ outputs_basic/
βββ outputs_house_rules/
βββ outputs_naive/
βββ outputs_s17/
βββ outputs_variant/
βββ docs/images/ # Figures used in README & poster
βββ requirements.txt
βββ README.md
- Python 3.10+
- pip
git clone https://github.com/yourusername/blackjack_simulation_python.git
cd blackjack_simulation_python
pip install -r requirements.txt
-
Game Modelling
- Deck Model: Supports single or multiple decks with shuffling.
- Ace Handling: Aces initially count as 11, adjusted to 1 if necessary to prevent busting.
- Dealer Rules: Dealer hits until reaching at least 17 (soft 17 behavior configurable).
-
Strategies Tested
- Simple Strategy: Always hit if hand value < 17, otherwise stand.
- Basic Strategy (generated): Decision table derived from simulated outcomes (
strategy.pkl), includes hit/stand/double/split logic.
-
Simulation Process
- Deal two cards to player and dealer.
- Apply chosen strategy to player until they stand, bust, or take a double/split action.
- Dealer plays according to rules.
- Compare outcomes to determine win/draw/loss and calculate payoff.
- Repeat for thousands or millions of games to achieve stable statistics.
-
Performance Metrics
- Win Rate β Percentage of units won.
- Draw Rate β Percentage of units pushed.
- Loss Rate β Percentage of units lost.
- Average EV per Hand β Expected value in units over all hands.
- Cumulative Earnings Graph β Visualizes performance over time with percentile bands.
Run the below commands in order(CMD):
- python blackjack_pipeline.py dataset --n-samples 50000 --outdir data
- python blackjack_pipeline.py analyze --indir outputs --outdir summary
- python blackjack_pipeline.py simulate --policy basic --decks 6 --n-games 200000 --replicates 5 --outdir outputs
- python blackjack_pipeline.py simulate --policy naive --decks 1 --n-games 100000 --replicates 5 --outdir outputs
- python blackjack_pipeline.py simulate --policy basic --decks 6 --s17 --outdir outputs
- python blackjack_pipeline.py simulate --policy basic --decks 6 --payout65 --outdir outputs
- python blackjack_pipeline.py simulate --policy basic --decks 6 --no-das --outdir outputs
- python blackjack_pipeline.py simulate --policy basic --decks 6 --double-911 --outdir outputs
- python blackjack_pipeline.py simulate --policy basic --decks 6 --hit-split-aces --outdir outputs
- python blackjack_pipeline.py simulate --policy basic --decks 6 --max-splits 2 --outdir outputs
The simulation produces CSV results (EV, win/draw/loss %, 95% CI) and visual plots that illustrate how strategy and rule variations impact outcomes.
- Shows expected value (EV) per initial hand with 95% confidence intervals, using Basic Strategy.
- 1β2 decks: EV is close to break-even (CIs overlap zero).
- 4β6 decks: EV becomes negative, confirming that larger deck counts increase the house edge.
- Practical insight: Deck count matters, but less than payout and dealer rules.
- Shows expected value (EV) per initial hand with 95% confidence intervals, using Naive Strategy.
- 1β6 decks: EV stays around β0.059, with overlapping CIs indicating no significant deck effect.
- Practical insight: Deck count has little influence compared to the large inherent disadvantage of the Naive policy.
- Simulated a naive policy: βHit until hand total β€ T, else Stand.β
- EV peaks around T β 15β16 (soft hands β 17β18).
- Increasing T initially reduces premature stands, but after ~16 busts increase and EV declines.
- Even at its peak, EV remains negative, showing that ignoring dealer upcard and softness leads to poor outcomes.
- Table shows the effect of rule variations on expected value (EV) per initial hand under Basic Strategy, with results averaged over 5 replicates.
- The Base game (6-deck, H17, DAS allowed, 3:2 payout) has an EV of β0.0058 (ββ0.6% house edge), with Win% β43.3%, Draw% β8.7%, and Lose% β48.1%.
- Rule changes shift EV as expected: 6:5 payout strongly worsens EV (β1.36 pp), restrictions on doubles or DAS reduce player edge (β0.15 to β0.17 pp), while S17 (+0.25 pp) and hitting split Aces (+0.11 pp) improve EV.
- Overall, payout rules and dealer standing rules have the largest impact, while max splits have little effect.
- A2βA6: Always hit (low risk of bust, potential to improve).
- A7 (soft 18): Flexible:- stand vs weak dealer (2β7), but hit vs strong dealer (8βAce).
- A8βA10: Always stand (already strong hands).
- Totals 4β11: Always hit (no risk of bust).
- Totals 12β16: Critical zone:- hit against strong dealer cards (7βAce), but stand against weak dealer cards (2β6) to let the dealer bust.
- Totals 17+: Always stand (bust risk is too high).
-
Basic Strategy (6-deck):
- Win / Push / Lose β 43.28% / 8.68% / 48.04%
- EV β β0.55%
-
Naive Strategy (6-deck):
- Win / Push / Lose β 41.03% / 9.66% / 49.32%
- EV β β6.03%
Key Insights:
Our simulations quantify the performance gap between Basic Strategy and a Naive hit-threshold strategy, as well as the influence of deck count and rule variations.
- Under Basic Strategy, EV is near break-even for 1β2 decks (confidence intervals overlap 0).
- With 4β6 decks, EV turns clearly negative (β β0.5% per hand at 6 decks).
- Interpretation: More decks slightly worsen the house edge, but the impact is modest compared to rules like payout ratios.
- Naive βHit β€ T, else Standβ rule peaks around T β 15β16 (soft β 17β18).
- EV improves initially but never crosses into positive territory.
- Bust rates rise quickly beyond T β 16, reducing overall EV.
- Conclusion: Ignoring dealer upcard and hand softness leads to unavoidable long-term losses.
- Policy impact: Switching from Naive to Basic improves EV by ~+5.5pp, reducing losses from ~6 units to ~0.5 units per 100 hands.
- Rule sensitivity:
- S17 (dealer stands on soft 17) improves EV by ~+0.2pp.
- 6:5 payout worsens EV by ~β1.3 to β1.5pp.
- Deck count effect is minor compared to these.
- Practical takeaway:
- Always play Basic Strategy.
- Prefer 3:2 payout, S17, DAS tables.
- Avoid 6:5 tables, where the house edge becomes significantly higher.
Overall Conclusion:
- Basic Strategy is nearly break-even under favorable rules, while Naive play incurs severe losses.
- Casino rule variations (payouts, dealer behavior) have far stronger influence on EV than the number of decks.
- Correct strategy choice and table selection are critical for minimizing expected losses.
In case of any clarifications or queries, do reach out to the author :-
Krishna Ramachandra krishna.ramachandra@ucdconnect.ie
zhixuan zhou zhixuan.zhou@ucdconnect.ie
DISCLAIMER : This project is intended purely for educational and academic purpose and does not endorse betting or gambling in any form.







