Simulation

In this chapter we look at modeling situations that involve an element of chance. We begin by using computer or calculator based simulation. Simulation is a powerful technique for two reasons.

We start with a few basic techniques. Begin by opening your CAS window. Follow along with your computer algebra system as you work through this module. The computer algebra systems used in the Connected Curriculum Project and even many inexpensive calculators have a procedure that generates random numbers. For example, in the TI-92 the line rand() generates a random number as shown in the TI-92 screen below. Each time this procedure is executed it generates a random real number between zero and one. The number zero is sometimes generated but the number 1 is never generated.

Missing TI-92 screen

We often want to generate a random real number between a and b -- possibly a but never b. This can be accomplished by the function myrandom shown in the TI-92 screen below.

Missing TI-92 screen

Now suppose that we want to simulate one throw of an ordinary die -- one with six sides numbered 1, 2, 3, 4, 5, and 6, with each of the sides being equally likely. The function die shown in the TI-92 screen below illustrates how this can be done.

Missing TI-92 screen

We illustrate simulation techniques by looking at a common game of chance. Games like this one are wonderful classroom examples -- the rules are clear and many students are interested in games of chance. However, you need to be sensitive to the fact that some students and their families find games of chance offensive.

Games of chance illustrate the ideas involved in situations involving chance. In many of these situations -- for example, investing in the stock market -- the "rules" are not as well known as in games of chance. For example, when you flip a fair coin you know that the probability of heads is one-half and the probability of tails is one-half. When you invest in a compnay you do not know the probability that its price will rise. Because the rules in games of chance are well-known we can concentrate on the mathematical aspects of simulating these games.

In this section we look at a game involving two dice. When a player throws the two dice there are eleven possible results -- the face up values of the two dice will total 2, 3, 4, ... 11, or 12. Notice these results are not equally likely. Click here to open a new window with a Java applet. Arrange these two windows so that they overlap and you can move easily between the two windows by clicking on the exposed portion of the inactive window to make it active. The Java applet shows the 36 possible results when two dice are thrown. If you click on any one of the results then all the results with the same total turn pink. Notice there is only one result -- one on both dice -- that totals 2 but there six possible combinations that total 7. Thus, the result 7 is six times more likely than the result 2. Close the window withthe Java applet when you are done using it.

The game is played as follows.

You can try the game by clicking here to open a new window with a Java applet. Arrange the windows as usual -- overlapping, so that it is easy to move back-and-forth between the two windows by clicking on the exposed portion of the inactive window to make it active. Because the new window can be very thin, if you have a large monitor you may be able to arrange the two windows so that they both fit on your screen without overlapping.

When the applet begins there are twelve lines indicating twelve possible situations. Notice there is a red dot next to the words "about to start." This indicates that the game has not yet begun.

At the bottom of the applet are two blank dice. Click on these dice to simulate the first throw of the dice. You will see the result of the first throw of the dice. Notice the red dot moves from its former position to the new state of play. Click on the dice to simulate the next throw of the dice. Notice that once again the red dot moves to the new current state of play. Of course, after the first roll of the dice, each roll may result in a win or a loss or the player may remain in the same state. If the player remains in the same state the red dot doesn't actually move; it just stays put. Continue "rolling the dice" until the player either wins or loses.

You can begin a new game at any time by clicking on the words "about to start."

The TI-92 function OneGame below simulates this game. If you have a TI-92 you can transfer this program to your TI-92 using the TI-Graph Link program and cable in the usual way, by linking with another TI-92 calculator that has the program, or, as a last resort, by typing it in.

OneGame

Missing TI-92 program listing Click Here for TI-Graph Link Click Here for TI-Graph Link

There have been numerous problems downloading programs for TI graphing calculators using the method above. If that method works, it is very clean. Sometimes, however, it doesn't work. In that case we can fall back on an older method, uuencoding, that is less user-friendly but more reliable. Click here for more information about this method. Then click here for a uuencoded (text file).

We look at this program line-by-line. The programs in the other CAS windows are similar. The first few lines and the last line


Onegame()
Func
Local j,point,toss


EndFunc

are "housekeeping" lines required by the TI-92. The first interesting line is the line


die() + die() -> point

This line simulates the first throw of the two dice. Notice that we cannot write

2 * die() -> point

because this would throw one die and double the result. The next block of code


If point=2 Then
   0
   Return
 ElseIf point=7 Then
   1
   Return
EndIf

checks to see if the player has either won or lost on the first throw. It returns one if the player has won and zero if the player has lost. Otherwise the program continues with the block of code below.


While 0=0
   die() + die() -> toss
   If toss=7 Then
      0
      Return
   ElseIf toss=point Then
      1
      Return
   EndIf
EndWhile

This block of code throws the dice as often as necessary. On each throw it checks to see if the player has won or lost and, if so, it returns one or zero and quits. Otherwise it continues tossing the dice.

The TI-92 screen below shows the results of playing the game seven times. Of course, your results are likely to be be different.

Missing TI-92 screen

Notice that in this particular experiment there were four wins and three losses. We can simulate 100 games with one clever line as shown in the TI-92 screen below.

Missing TI-92 screen

This line adds 100 terms. Each term is computed by playing the game once. If the game is won then the term is one and if the game is lost then the term is zero. Thus, the sum of all 100 terms is the number of times the game was won. Notice that in the screen above we simulated 100 games three times. The first two times 43 games were won. The last time 47 games were won. Your results are likely to be somewhat different.

The work above illustrates some very important ideas. The results that we obtained varied somewhat. This is to be expected with this kind of simulation. More importantly, this is to be expected when you actually play the game. Sometimes you will do better than other times. You can use simulation effectively in the classroom to discuss the natural variation in results involving an element of chance. For example, you might have everyone in your class simulate 100 games as shown above and then put all the results on the board to show the natural variation.

The screen below shows the results of one simulation of 1,000 games. Notice that this game appears to be somewhat biased against the player. With a class of 20 students simulating 100 games, it is likely that one or more students might reach the wrong conclusion based on just their one simulation. This is an important point -- we need fairly large simulations to reduce the chances of a wrong conclusion.

Missing TI-92 screen


  • Suppose the game was played with two four-sided dice. With four-sided dice the most likely result is 5, so 5 is used wherever 7 was used in the original game. How likely is the player to win with two four-sided dice?

  • Suppose the game was played with two twelve-sided dice. With twelve-sided dice the most likely result is 13, so 13 is used wherever 7 was used in the original game. How likely is the player to win with two twelve-sided dice?

[Next section -- Markov Chains]


Copyright c 1997 by Frank Wattenberg, Department of Mathematics, Montana State University, Bozeman, MT 59717