gamblers_ruin_simulation, an Octave code which simulates the game of gambler's ruin.
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.
The code GAMBLERS_RUIN_PLOT displays the "score" for player A at each step of the game.
The code 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.
gamblers_ruin_plot ( a_stakes, b_stakes )where
gamblers_ruin_simulation ( a_stakes, b_stakes, game_num )where
The computer code and data files described and made available on this web page are distributed under the MIT license
gamblers_ruin_simulation is available in a MATLAB version and an Octave version.
octave_simulation, Octave 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.