Skip to content

Instantly share code, notes, and snippets.

@otac0n
Last active August 6, 2021 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otac0n/081ab9a0a2372679322fc42b18ace8a4 to your computer and use it in GitHub Desktop.
Save otac0n/081ab9a0a2372679322fc42b18ace8a4 to your computer and use it in GitHub Desktop.
Shows that the Martingale strategy has a 50% chance of doubling your money.
var rand = new Random();
bool Play(ref int bank, int bet)
{
var win = rand.Next(2) < 1;
checked
{
bank += win ? bet : -bet; // If you win, you gain your bet back AND the winnings. If you lose, you just lose your bet.
}
return win;
}
int Martingale(int bankroll, int target)
{
var bet = 1;
while (bankroll > 0 && bankroll < target)
{
if (Play(ref bankroll, bet))
{
bet = 1; // Reset your bet amount to $1 whenever you win.
}
else
{
checked
{
bet *= 2; // Double your bet if you lose.
}
// It is impossible to make a bet larger than your bankroll.
if (bet > bankroll)
{
bet = 1; // A casino will not loan you money. In this case, reset to a bet of $1.
}
}
}
return bankroll;
}
var start = 100;
Enumerable.Range(0, 100000).Select(_ => (double)Martingale(start, start * 2)).GroupBy(outcome => outcome).ToDictionary(g => g.Key, g => g.Count()).Dump();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment