# Simulation

Sometimes there are probability questions that are difficult to find theoretically or where actually conducting an experiment is impractical. This is where simulation comes in handy. If you want to know what happens when you explode a nuclear bomb, well, that's not something you can afford to do very often, even if you could manage to obtain of a nuclear device in the first place. Let's say you want to study the evolution of a species over a 1000 year period; that's not something you can wait around for.

Computers can be used to simulate those conditions for us much quicker and cheaper than we could do it (if we could do it at all). There are computer software packages that will perform very complicated simulations. You can also use computers to perform simple simulations (what we're going to do) but you don't have to have a computer to perform simulations, they can be sometimes be done with everyday materials such as coins or dice.

## Goals and Objectives

When you are through with this lesson, you'll be able to do the following tasks.

• Identify which type of simulation is appropriate.
• Design simulations problems.
• Perform simulations using both technology and non-technology methods.
• Find probabilities and expected values based on the simulation results.

## Types of Simulation

There are two major types of simulation that we're going to look at.

1. Finding probabilities.
2. Finding expected values

### Finding Probabilities

When asked to find the probability of something, you want to conduct an experiment many times (the more times the better), and record the outcomes of each experiment. If you're only looking for one probability (see Great Birthday Problem), then you can classify outcomes as success or failure, but if you're looking for more than one probability (see Craps) it will be useful to record the individual outcomes.

Once you have finished collecting the data by repeating the experiment several times, you can find the empirical probability or relative frequency by taking the number of successes divided by the number of trials. Sometimes you will see this written as the number in the event divided by the number in the sample space, but the number of successes divided by the number of trials works just as well and is simpler to remember.

### Finding Expected Values

Sometimes you're asked to find the average number of times it will take to accomplish something. For example, how many moves does it take to win Chutes & Ladders or how many numbers must be called out before someone yells "BINGO!"?

The key to working this problems is to simulate the problem and record the number of times it takes to finish the game or experiment. Repeat the whole game many times (the more the better) and then when you're all done, find the mean (average) of the numbers you have recorded. This is the expected value.

Sometimes the expected value can be found theoretically, but other times it is just impossible or impractical to do theoretically.

## Designing Simulations

The important thing to remember when designing simulations is that you have to make it realistic or the simulation is not valid. For instance, if you want to simulate births of children, there are two possibilities, boy or girl, and both are equally likely. You need to find some way of simulating that experience ... and flipping a coin immediately comes to mind because there are also two outcomes that are equally likely. Don't roll a die to simulate births because there are six outcomes possible. However, if you rolled a die and decided a roll of 1, 2, or 3 is a boy and a 4, 5, or 6 is a girl, then that would be okay because there are two outcomes, each equally likely.

If you want to simulate birthdays, you can't roll a die or flip a coin. There are 365 birthdays, not including leap day, in a year and so you would need something that generated values between 1 and 365. In this case, you could write the numbers from 1 to 365 on pieces of paper and stick them in a hat and randomly select them, replacing the paper and reshuffling the papers after each draw.

On the other hand, there is the good old trusty random number generator on the calculator or computer. This is highly recommended for all but the simplest simulations because of speed. Every programming language has a random number generator and even applications like spreadsheets include them, too. They usually generate random numbers with a uniform distribution between 0 and 1 (including 0, but not including 1), so they need transformed into something useful.

The command to generate the random integer varies from language to language, but it usually goes something like this. n = int ( a * rand() + b ) where a is the number of values to generate and b is the starting value.

Here are some examples:

BASIC - Simulate rolling a die with 6 sides
let n = int ( 6 * rand + 1 )
JavaScript - Simulate a spinner with 10 slots numbered 1 - 10
spin = Math.floor( 10 * Math.random() + 1 );
Pascal - Simulate coin tosses, 0 = tail, 1 = heads
toss := Random(2);

Here is a little example of what can be done with JavaScript on your web pages to generate random numbers.

 values between and You can even sort the data into or order. This is helpful for when you need to see if there are any duplicate values or how many numbers lie between two given values.

If you want to simulate things where the outcomes aren't equally likely, then you need to make adjustments. For example, in craps, you roll 2 dice and look at the sum. To simulate this, you would either generate to uniform integers between 1 and 6 or you would generate numbers between 1 and 36 and assign them as 1=1, 2 thru 3=2, 4 thru 6=4, 7 thru 10=5, etc., so that the probabilities match what the probabilities of the sums are.

If the outcomes aren't uniformly distributed, but have some other distribution like normal or poisson, then you could use a computer algebra system to generate the numbers and perform the simulation.

## Simulation Problems

Here are some problems to help you get a feel for simulation techniques. Go ahead and work through each one and when you're done, come back and take a quiz.

The Great Birthday Problem
You walk into a room with a group of people in it and make the following wager, "I'll bet \$50 that there ( are / are not ) two people in this room with the same birthday." How many people should be in the room before you bet that two share the same birthday?
Craps
While there are many variations to the game of craps the simple game is this. A wager is placed and then a pair of dice are rolled. If the sum is a 7 or 11, you win and the game is over. If the sum is a 2, 3, or 12, you lose and the game is over. If any other sum is rolled, that is called your point and you continue to roll until either you either roll that point again or you roll a 7 and the game is over. You win if you roll your point again and lose if you roll a 7.