gamblers_ruin_simulation


gamblers_ruin_simulation, a Python code which simulates a game in which a gambler starts with a fixed bankroll, and then repeatedly makes the same bet until going bankrupt.

In the game of gambler's ruin, two gamblers, A and B, start with $A_STAKES and $B_STAKES, respectively, and play until one of them is bankrupt. They repeatedly flip a coin. On heads, A wins one dollar from B, and on tails, B wins one dollar from A.

The game is easy to simulate, and can also be analyzed. It is worth study in part because the analysis or simulation of the game reveals some surprising features about the chances of winning, the typical length of a game, and the number of times each player will be in the lead.

In particular, most people will be surprised to find that if A starts with $1 and B with $100, the "average" game will take 100 coin tosses.

It is known that, if A and B start the game with $A_STAKES and $B_STAKES,

Note that the game of gambler's ruin can be regarded as a case of a 1D random walk in a finite region, in which the "particle" starts A_STAKES steps from one wall and B_STAKES steps from the other, and in which the walk ends when the particle strikes a wall.

gamblers_ruin_plot() displays the "score" for player A at each step of the game.

gamblers_ruin_simulation() plays the game a number of times, and computes statistics of the number of steps and the probability of winning, as well as histograms of the number of steps and the number of times the lead was reversed.

Usage:

gamblers_ruin_plot ( a_stakes, b_stakes )
where plays one game and plots the trajectory.

gamblers_ruin_simulation ( a_stakes, b_stakes, game_num )
where plays the game game_num times, computes statistics, and plots some histograms.

Licensing:

The computer code and data files described and made available on this web page are distributed under the MIT license

Languages:

gamblers_ruin_simulation is available in a MATLAB version and an Octave version and a Python version.

Related Data and codes:

python_simulation, Python codes which use simulation to study card games, contests, and other processes which have a random element. Usually, the purpose is to try to predict the average behavior of the system over many trials.

Reference:

  1. Martin Gardner,
    The Mathematical Circus,
    Mathematics Association of America, 1996,
    ISBN13: 978-0883855065,
    LC: QA95.G287.
  2. Ian Stewart,
    "Repealing the Law of Averages",
    Scientific American,
    Volume 278, Number 4, April 1998, pages 102-104.

Source Code:


Last revised on 12 November 2022.