Skip to content

About

A Monte Carlo simulator that compares betting strategies on identical random outcomes.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

StrategyLab

A browser app that runs betting strategies side by side on the same simulated outcomes and measures, with 95% intervals, how much each one loses per dollar wagered and how often it goes bust.

Live: iangopen.github.io/strategylab · Sample output: the one-page report for the default run (PDF)

CI

P(bust) heatmap for Flat: starting bankroll $100 to $1,000 across, base bet $5 to $25 up, with the bankroll readout below

A parameter sweep: P(bust) for Flat across 50 combinations of starting bankroll and base bet, 10,000 sessions each. A dot marks, per base bet, the smallest bankroll from which every larger one keeps P(bust) below 5% at the upper end of its 95% interval.

Results table for the default run, key rows: Flat and Martingale

Key rows of the results table for the default run (the app shows every row): EV per $ wagered is −2.76% for Flat and −2.91% for Martingale, against the −2.70% the house edge predicts; Martingale ends ahead more often, busts about ten times as often, and its busts come after a median of 14 rounds.

Engineering

  • Deterministic, seeded engine in a Web Worker. The engine in src/engine/ is plain TypeScript with integer-cent money; every random number comes from one seeded generator, and a test fails if an engine file uses Math.random or imports React or the DOM. Runs execute in a Web Worker, with progress and Cancel.
  • Statistical tests that were shown to fail when broken. Tolerances are 4 standard errors computed from the simulated sessions, never hand-picked. Key tests were run against deliberately broken code and seen to fail before the code was restored, and the failures are logged in docs/history.md (for example STATUS 148, 165, 166 and 176). One caught a real bias: a $5 bettor at −110 odds measured 5.29 standard errors from theory until payouts carried their sub-cent remainders.
  • Reproducible share links and reports. Copy link puts the whole scenario, including custom rules and sweeps, in the URL. End-to-end tests open a copied link in a new page and check that the rerun gives the same numbers bit for bit; the report prints the seed, the build's commit and that link.
  • CI with Playwright. Every push runs lint, 843 Vitest tests, a strict type check and build, and 55 Playwright tests against the production build. Only a main commit that passes all of them deploys to GitHub Pages.

StrategyLab is an educational simulation for adults (18+). No real money is involved, and nothing here is betting advice. If gambling is causing problems for you or someone you know, call or text 1-800-MY-RESET, the US National Problem Gambling Helpline (ncpgambling.org).

What it is

In a game with a house edge, every bet loses edge × stake on average, whatever came before it. So a strategy's expected loss is the edge times the total it wagers: Martingale or Fibonacci loses the same expected share of each dollar as a flat bettor. Strategies differ in how their results spread out. StrategyLab lets you check this on your own scenarios.

  • 8 built-in strategies: Flat, Martingale, Paroli, D'Alembert, Fibonacci, Labouchère, Oscar's Grind and Kelly. Up to 8 strategies run side by side, and you can write your own as a rule.
  • Scale: one run simulates up to 1,000,000 sessions of up to 1,000,000 rounds each. Every strategy plays the same random outcomes in each session.
  • Games: European and American roulette, a fair coin, any win/lose game, sports odds (American or decimal, two-way market, vig removed proportionally), or a game with up to 12 outcomes and their own payouts.
  • Results: EV per $ wagered next to its theoretical value and standard error; P(profit) and P(bust), each with a 95% Wilson interval; how many rounds busted sessions lasted (median and 10th percentile); final-bankroll percentiles; drawdowns, including how often a session fell to half its starting bankroll; and losing streaks.
  • Charts and replay: percentile bands over time, final-bankroll histograms, and a replay of any one session for every strategy. Every panel of a chart shares the same axes.
  • Links: Copy link puts the whole scenario, custom rules included, in the URL.
  • Report: a one-page summary of a run, ready to print or save as PDF: the takeaways in plain sentences, the key statistics with 95% intervals, a comparison of the strategies through one interval on their difference (taken session by session, because every strategy plays the same sessions; src/engine/stats/paired.ts, measured in src/report/comparison.stats.test.ts), the bankroll chart, every setting, the seed, the build's commit and the link that reruns it to the same numbers. Sample report (PDF) of the default run.
  • Parameter sweeps: vary two of starting bankroll, base bet and max rounds across a grid (up to 20 × 20 cells and 1,000,000 sessions) for one strategy, and see P(bust) in every cell as a heatmap with its 95% interval. A readout names, per base bet, the smallest bankroll on the grid from which every larger one keeps P(bust) below a target (5% by default) even at the upper end of its interval, and says the result is unstable when the passing bankrolls are not one unbroken run. Each cell is an ordinary run with its own derived seed, so any cell reproduces on its own, and the sweep travels in the link.

The default run is European roulette (house edge 1/37 = 2.703%), a $1,000 bankroll, a $10 base bet, a stop at $1,100 and 10,000 sessions with seed 12345. Martingale ends in profit in 82.9% of sessions (95% interval 82.2–83.7%) and Flat in 53.8% (52.8–54.7%). Their EV per $ wagered is −2.91% and −2.76%, against a theoretical −2.70%. Martingale's many small wins are paid for by rare large losses: it busts in 17.1% of sessions (16.3–17.8%), Flat in 1.6% (1.4–1.9%).

"Bankroll over time" for the default run, zoomed to rounds 0 to 25 on Martingale: both panels share the same axes

Bankroll over time for the default run, after "Fit to this strategy" on Martingale (rounds 0 to 25). Shaded bands are the middle 90% and 50% of sessions, thin lines are the first 50 sessions.

Replay of session 6: Martingale runs out of money after 9 rounds while Flat keeps playing

How it works

Common random numbers. Session i gets the same seed for every strategy, splitmix32(masterSeed ^ splitmix32(i)), which seeds a mulberry32 generator. Each round uses exactly one uniform draw, and strategies never touch the generator. So in session i every strategy sees the same wins and losses, and a difference in results comes from the strategy, not from luck. See src/engine/rng.ts and src/engine/runner.ts.

Whole cents. Money is integer cents. A win on a non-integer payout, such as −110 odds, pays the rounded amount and carries the remainder to the next win, so a session's total stays within half a cent of the exact amount.

A Web Worker for all simulation. Runs execute in a worker through Comlink, with progress about every 2% and a Cancel button. The page stays responsive during a run. Per-session data stays in the worker. Only summaries come back: the stats, a shared histogram, 50 sample paths and percentile bands from the first 2,000 sessions.

A rule language instead of code. A custom strategy is JSON data, checked against a closed grammar and compiled into the same strategy interface as the built-ins. A progression rule has up to 10 entries for wins and 10 for losses. Each entry has an optional condition (win or loss streak, profit this cycle, bankroll against the start, size of the last bet) and one action (set, multiply, add, reset, reset the cycle, stop). A sequence rule is a custom Labouchère line of up to 20 numbers. This rule is Martingale:

{ "kind": "progression", "name": "Double after a loss", "startUnits": 1,
  "onWin":  [ { "then": { "type": "reset" } } ],
  "onLoss": [ { "then": { "type": "multiply", "by": 2 } } ] }

The rule and link code contain no eval, new Function or dynamic import, and a test scans for them. A shared link is untrusted input: over 8,000 characters or nested deeper than 16 levels, it is refused before anything is decoded.

Testing

  • Unit and statistical tests: npm run test runs 843 Vitest tests, fast unit tests first, then the Monte Carlo tests (*.stats.test.ts). Seven more (benchmarks and fixture writers) are opt-in.
  • Tolerances come from the data. A statistical test passes when the measured value is within 4 standard errors of the expected one, with the standard error computed from the simulated sessions. Seeds are fixed, so each run gives the same numbers.
  • The EV invariant: for every strategy, EV per $ wagered must equal −edge. invariant.stats.test.ts checks 9 strategies (the 8 built-ins and a custom rule) on 8 games with 20,000 sessions each: house-edge, fair and player-edge games, sports odds and multi-outcome games. The largest deviation is 2.78 standard errors. Kelly refuses to bet on the two house-edge multi-outcome games, and the test asserts that instead.
  • A bug the tolerance caught: before the sub-cent carry, a flat $5 bettor at −110 odds measured 5.29 standard errors from theory over 100,000 sessions. With the carry it measures 0.55 (runner.regression.stats.test.ts).
  • Golden files: stored session and Monte Carlo results must reproduce bit for bit after engine changes.
  • End-to-end: 55 Playwright tests in 12 spec files run against the production build, served under the same /strategylab/ path as the live site. Every number the page shows is compared with the engine run in Node on the same scenario. One test runs the app by keyboard only and checks what a screen reader announces.
  • CI: GitHub Actions runs lint, the Vitest suites, a strict build and the Playwright tests. Only a main commit that passes all four deploys to GitHub Pages.

Get started

Requires Node.js 24. The commands work the same in PowerShell and bash.

npm ci
npm run dev                       # dev server at http://localhost:5173/
npm run test                      # unit tests, then statistical tests
npm run lint
npm run build                     # strict type check and production build
npx playwright install chromium   # once per machine
npm run e2e                       # Playwright against the production build under /strategylab/
npm run bench                     # one fixed 100,000-session run and one max-size sweep, timed (numbers depend on the machine)

npm run screenshots regenerates the README images, public/og-image.png and docs/report-sample.pdf from the live site, or from SCREENSHOT_URL if set. It first checks that the results table and the sweep table equal the engine's numbers.

Repository map

src/engine/    simulation engine, no React or DOM: RNG, games, runner, Monte Carlo,
               strategies/, rules/ (the rule language), stats/
src/worker/    the Web Worker and its client
src/share/     scenario links: encode, decode, validate
src/sweep/     parameter sweeps: grid, caps, cell seeds, the cell-by-cell runner
src/report/    the one-page report and the takeaway and sweep-readout sentences
src/ui/        React components; ui/charts/ draws with Canvas 2D
e2e/           Playwright specs
scripts/       the licence-notice build plugin and the screenshot script
docs/          spec.md (design and specs), history.md (verification log), README images

Roadmap

Not scheduled. From docs/spec.md:

  • Three-way sports lines, where the market prices a draw or a push.
  • Parlays, whose legs must all win.
  • Other ways to remove the vig: power, Shin and additive.

Privacy

The app runs entirely in your browser. It sends no data anywhere, sets no cookies, and stores one setting, your theme choice, in localStorage.

Credits

The built site includes these third-party packages. Their full licence texts ship with it as third-party-licenses.txt, generated at build time by scripts/licenseNotices.ts.

Used Source Author Licence
React, react-dom, scheduler react.dev Meta Platforms, Inc. and affiliates MIT
Comlink GoogleChromeLabs/comlink Google LLC Apache-2.0
mulberry32 generator, reimplemented in src/engine/rng.ts the author's gist Tommy Ettinger CC0 1.0
Strategy colours: the Okabe-Ito colourblind-safe palette Color Universal Design Masataka Okabe and Kei Ito colour values, no licence needed

Build and test tools (Vite, Vitest, Playwright, TypeScript, oxlint) are not shipped. The favicon and screenshots were made for this project.

License

MIT © 2026 Ian Gopen

About

A Monte Carlo simulator that compares betting strategies on identical random outcomes.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages