Now that we have a Module that works, we can use the code to generate a sequence of games played that lead to either the computer simulated gambler losing all the initial money or the gambler increasing the initial amount of money to the desired amount. In order to use the module we could (1) input the Module into a cell, (2) evaluate that cell to load in the definition of the Module, and (3) call the Module repeatedly, keeping track of the results. For example, if we wanted to simulate the process of ten gamblers playing a game starting with $10, trying to reach $15, with a probability of winning an individual play of the game being , we could create a cell with the input line
GamblersRuin[10, 15, 18/38]and evaluate this cell ten times. Each time the cell is evaluated we would have to write down the information returned by the Module. We could then tabulate the information and analyze all the output.
Recall that our goal is to compute the probability of a gambler achieving the
goal. Using the module one time only tells us what would happen to a single
person gambling. To compute an approximation of the probability of winning we
will need to repeat the process many times and use the output of all the
simulations to compute an approximate probability. In particular, suppose that
we consider simulating a total of 150 gamblers. At the end of evaluating the
simulator 150 times we might find that 11 of the simulated gamblers won and 139
were ruined. An approximate probability for achieving the goal is given by
Our first reaction to this prospect should be that even though the Module does most of the work, evaluating the Module many times and writing down the individual gamblers results will be tedious. Since computers were put on this world to perform lots of tedious tasks we should try to get the software to handle this. The first thing we should do is build a piece of code that will run GamblersRuin as many times as we want and then compute the approximate probability for us. Let's consider the following new Module.
SimulateGR[a_, c_, p_, ngamblers_] := Module[ {var1,var2,var3,nwins,nlosses,output}, var2 = c; var3 = p; nwins = 0; nlosses = 0; Do[ Clear[var1,output]; var1 = a; output = GamblersRuin[var1,var2,var3]; If[output[[2]], nwins = nwins + 1, nlosses = nlosses + 1] {i,1,ngamblers} ]; Return[nwins/(nwins+nlosses)] ]The module is a little longer and has added a few variables and lines of code. The important features in the Module are:
The output from this module is an approximate probability for a gambler achieving the goal. We can now compare the results of calculations using the new simulator to the exact value from earlier in the chapter. That is, we can use the simulator to compute an approximation of the exact value computed in the exact solution method. For example, suppose that after loading the two modules we need, GamblersRuin and SimulateGR, we enter the line
SimulateeGR[10,15,0.44,500]An example output would be the number . This means the simulator predicts the chances of achieving the goal of ending the evening with $15 when we start from $10 is 25.4%.
The exact value in this case comes from evaluating the function