next up previous
Next: Combining the Commands into Up: Approximating the Solution of Previous: Approximating the Solution of

Defining Commands for a Simulator in Mathematica

In this section, we will just write down a list of commands that we will need in developing a simulator for the problem. In the next section, we will put the commands together in a Module. The first thing to do is write down the steps that occur in the process that we want to simulate. First, the gambler will enter the casino with $a$ dollars. So we need a command to initialize the amount of money for the gambler. The input line

  a = 15
will give the gambler $15 initially. Next we must find some way of generating a random outcome. Each time the gambler plays the game we want to generate a random outcome. Throughout the simulation we will need to generate a possibly large number of these random outcomes.

The basic Random[] command in Mathematica generates a uniformly distributed random number in the interval $[0,1]$. The use of a uniform distribution implies that any number in the unit interval is equally likely of being returned by the command. Note that the Random command is a general random number generator and the output can be modified by including arguments. For now we will be content with uniform numbers between zero and one. Using the numbers from the example in the previous section the probability of winning an individual play of the game is $p=0.49$ and the probability of losing is $q=0.51$. Let's consider how to use the Random function to generate a win or loss randomly. One way to proceed is to generate a random number and then test the number to see where it lies in the interval $[0,1]$. If the output of the Random function is less than or equal to $0.49$ the gambler wins. If the output is greater than $0.49$ gambler loses.

We need to translate these steps into Mathematica code. To initialize the probabilities and compute a single random number the following commands will set things up

  p = 0.49
  q = 1.0 - p
  ranval = Random[]
After evaluating a cell with these three lines, the variables, $p$ and $q$, will be initialized to the correct probabilities, and the variable ranval will have a random number between 0 and 1 stored in it. Then to test the random number to see if the gambler wins we can use
  If[ test, true action, false action ]
The If command has three arguments. The first argument is the test to perform, the second argument is the action to take if the test is true, and the third argument is the action to perform if the test is false. Note that other arguments can be included in the If command.

The test we are interested in is to see if the random number stored in the variable ranval is less than or equal to $p$. The action we want the simulator to take if the test is true is to add one to $a$ and the action we want the simulator to take when the test is false is to subtract one from $a$. The appropriate command would be

  If[ ranval <= p, a = a + 1, a = a - 1 ]
Executing all these commands is the equivalent of playing the game one time. These commands must be repeated until the variable $a$ in the code is either zero or the value we are trying to achieve, say $20 as above.

To repeat this many times we can use the While command in Mathematica. To make things clear consider the following set of commands which simulate the gambler going through the entire process.

  a = 15
  c = 20
  p = 0.49
  While[ 0 < a < c,
    ranval = Random[];
    If[ ranval < p, a = a + 1, a = a - 1 ];
    ]
The While command has two arguments. The first is a test that is performed and the second is the action to take until the test is no longer true. In the example above the test is to see if the value of $a$ is between zero and $c$. This appears in the code fragment
  0 < a < c
which is the first argument in the While command. Recall that the arguments must be separated by comma. The rest of the input lines,
  ranval = Random[];
  If[ ranval < p, a = a + 1, a = a - 1 ];
make up the second argument of the While command. The action to take in the While block is while the test in the first argument is true perform the two commands in the second argument. The first input line generates the next random number and the second tests the random number to simulate playing the game as described above. Notice that if more than one command is included in an argument, as in the While command above, the commands must be separated by a semicolon.

Evaluating a cell which includes all the lines listed above will produce no output. However, if you type the simple input line

  a
at the end of the cell, output for the form
  Out[5]= 20
will be produced. The output above indicates the gambler achieved the goal in this case. Note that each time the cell is evaluated a different results may occur due to the fact that different random numbers will be generated. To get an idea of how this works, use the chapter notebook, which has this code, and evaluate the cell a few times. The only possible outputs you will see are a 0 or a 20.


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