# Simulations Lecture 14

Steven S. Skiena

Simulations

Often, a system we are interested in may be too complicated to readily understand, and too expensive or big to experiment with.

• What direction will an oil spill move in the Persian Gulf, given certain weather conditions?
• How much will increases in the price of oil change the American unemployment rate?
• Now much traffic can an airport accept before long delays become common?

We can often get good insights into hard problems by performing mathematical simulations.

Scoring in Jai-alai

Jai-alai is a Basque variation of handball, which is important because you can bet on it in Connecticut. What is the best way to bet?

The scoring system in use in Connecticut is very interesting. Eight players or teams appear in each match, numbered 1 to 8. The players are arranged in a queue, and the top two players in the queue play each other. The winner gets a point and keeps playing, the loser goes to the end of the queue. Winner is the first one to get to 7 points.

This scoring obviously favors the low numbered players. For fairness, after the first trip through the queue, each point counts two.

But now is this scoring system fair?

Simulating Jai-Alai

```1 PLAYS                 2
1 WINS THE POINT, GIVING HIM                 1

1 PLAYS                 3
3 WINS THE POINT, GIVING HIM                 1

4 PLAYS                 3
3 WINS THE POINT, GIVING HIM                 2

5 PLAYS                 3
3 WINS THE POINT, GIVING HIM                 3

6 PLAYS                 3
3 WINS THE POINT, GIVING HIM                 4

7 PLAYS                 3
3 WINS THE POINT, GIVING HIM                 5

8 PLAYS                 3
8 WINS THE POINT, GIVING HIM                 1

8 PLAYS                 2
2 WINS THE POINT, GIVING HIM                 2

1 PLAYS                 2
2 WINS THE POINT, GIVING HIM                 4

4 PLAYS                 2
2 WINS THE POINT, GIVING HIM                 6

5 PLAYS                 2
5 WINS THE POINT, GIVING HIM                 2

5 PLAYS                 6
5 WINS THE POINT, GIVING HIM                 4

5 PLAYS                 7
7 WINS THE POINT, GIVING HIM                 2

3 PLAYS                 7
7 WINS THE POINT, GIVING HIM                 4

8 PLAYS                 7
7 WINS THE POINT, GIVING HIM                 6

1 PLAYS                 7
7 WINS THE POINT, GIVING HIM                 8

WIN-PLACE-SHOW IS                 7                2                3

BETTER THAN AVERAGE TRIFECTAS:                 1 TRIALS

WIN  PLACE SHOW   OCCURRENCES
7          2                 3```

Is the Scoring Fair?

How can we test if the scoring system is fair?

We can simulate a lot of games and see how often each player wins the game!

But when player A plays a point against player B, how do we decide who wins? If the players are all equally matched, we can flip a coin to decide. We can use a random number generator to flip the coin for us!

What data structures do we need?

• A queue to maintain the order of who is next to play.
• An array to keep track of each player's score during the game.
• A array to keep track of how often a player has won so far.

Simulation Results

``` Jai-alai Simulation Results

Pos      win     %wins      place     %places      show      %shows
1     16549     16.55      17989      17.99      15123       15.12
2     16207     16.21      17804      17.80      15002       15.00
3     13584     13.58      16735      16.73      14551       14.55
4     12349     12.35      13314      13.31      13786       13.79
5     10103     10.10      10997      11.00      13059       13.06
6     10352     10.35       7755       7.75      11286       11.29
7      9027      9.03       8143       8.14       9007        9.01
8     11829     11.83       7263       7.26       8186        8.19

total games = 100000```

Compare these to the actual win results from Berenson's Jai-alai 1983-1986:

1 14.1%, 2 14.6%, 3 12.8%, 4 11.5%, 5 12.0%, 6 12.4%, 7 11.1%, 8 11.3%

Were these results good?

Yes, but not good enough to bet with! The matchmakers but the best players in the middle, so as to even the results. A more complicated model will be necessary for better results.

Limitations of Simulations

Although simulations are good things, there are several reasons to be skeptical of any results we get.

Is the underlying model for the simulation accurate?

Are the implicit assumptions reasonable, or are there biases?

How do we know the program is an accurate implementation of the given model?

After all, we wrote the simulation because we do not know the answers! How do you debug a simulation of two galaxies colliding or the effect of oil price increases on the economy?

So much rides on the accuracy of simulations it is critical to build in self-verification tests, and prove the correctness of implementation.

Random Number Generator

We have shown that random numbers are useful for simulations, but how do we get them?

First we must realize that there is a philosophical problem with generating random numbers on a deterministic machine.

``Anyone who considers arithmetical methods of producing random digits is , of course, in a state of sin.'' - John Von Neumann

What we really want is a good way to generate pseudo-random numbers, a sequence which has the same properties as a truly random source.

This is quite difficult - people are lousy at picking random numbers. Note that the following sequence produces 0's + 1's with equal frequency but does not look like a fair coin:

Even recognizing random sequences is hard. Are the digits of pseudo-random?

Should all those palindromes (535, 979, 46264, 383) be there?

The Middle Square Method

Von Neumann suggested generating random numbers by taking a big integer, squaring it, and using the middle digits as the seed/random number.

It looks random to me... But what happens when the middle digits just happen to be 0000000000? From then on, all digits will be zeros!

Linear Congruential Generators

The most popular random number generators, because of simplicity, quality, and small state requirements are linear congruential generators.

If is the last random number we generated, then

The quality of the numbers generated depends upon careful selection of the seed and the constants a, c, and m.

Why does it work? Clearly, the numbers are between 0 and m-1. Taking the remainder mod m is like seeing where a roulette ball drops in a wheel with m slots.