next up previous
Next: Getting an Approximate Probability Up: Approximating the Solution of Previous: Combining the Commands into

Adding to the Simulation Module

The Module above is about the simplest function we could create to simulate the entire process of a gambler achieving the goal or being ruined. As the simulator works, additional imformation is being computed and if we keep track of this information, we can learn more about the process we are simulating. It would also be interesting to tell how long it takes a gambler to win or lose. That is, we could include a counter that keeps track of the number of times that the gambler plays before reaching the goal or being ruined. One example of a Module that would do this is

  GamblersRuin[ a_, c_, p_ ] :=
              Module[ {ranval,var1,var2,var3,icnt},
                    icnt = 0;
                    var1 = a;
                    var2 = c;
                    var3 = p;
                    While[ 0 < var1 < var2,
                         icnt = icnt + 1;
                         ranval = Random[];
                         If[ ranval < var3, var1 = var1 + 1, var1 = var1 - 1]
                         ];
                    Return[{icnt,var1==var2}]
                    ]
The variable, icnt, which is included in the local variable list is used to add up the number of times the game is played before termination. There is one more important coding item to note in this example. The Return command will only return one value. In this case we have told the command to return both the counter value and whether or not the gambler failed in a list, which is a single output object. You should learn to return lists of output values that you want unless your module will only produce a single output value.

In designing a Module we should keep in mind the inputs and outputs we want to use. In our case we are inputting the initial amount of money, a, the amount of money we want to have in the end, c, and the probability of winning an individual play of the game, p. The output of the Module is the number of times played and whether or not the gambler is ruined. The input and output of any Module are dependent on the process and the information we want the Module to return.


next up previous
Next: Getting an Approximate Probability Up: Approximating the Solution of Previous: Combining the Commands into
Joe Koebbe 2003-10-01