# Simulation 2

### Pair of Dice Example

Here we'll simulate rolling a pair or 6-sided dice, keeping track of what numbers we get. So one trial = roll a pair of 6 sided dice. How often should a 7 appear (aka the ideal "expected" result)? How often should a 2 appear?

 histogram = new Histogram(2, 12); // min=2, max=12 (inclusive) for (i: series(100)) { // --one trial-- roll = random(1, 6) + random(1, 6); // 2..12 histogram.add(roll); // record one trial on the histogram } print(histogram);

Mathematically, the 7 should appear 1/7 of the time. As then number of trials gets larger (100, 1000, 10000), note how the simulation produces a count which gets closer to the mathematically expected result.

Trial = flip a coin 10 times, count the number of heads. Here we use a second, nested loop to flip a coin 10 times, counting the number of times that heads comes up.

How often do you expect that all 10 flips are heads?

 histogram = new Histogram(0, 10); for (i: series(100)) { // --one trial-- count = 0; for (j: series(10)) { roll = random(1, 2); // 1=heads if (roll == 1) { count = count + 1; } } histogram.add(count); } print(histogram);

### Heads In A Row Example

Flip a coin 10 times, but stop on the first tails. How many heads in a row do you get? ("break;" is a command to immediately exit the current loop.)

 histogram = new Histogram(0, 10); for (i: series(1000)) { count = 0; for (j: series(10)) { roll = random(1, 2); // 1=heads, 2=tails if (roll == 2) { break; // end loop, record this count } count++; } histogram.add(count); } print(histogram);

### Mehran Class Bet Exercise

On certain days in Mehran's probability class, Mehran will give a student \$50 if they can roll 10 6-sided dice and get a total of 25 or less or 45 or more. What are the odds of the student winning?

 var histogram = new Histogram(1, 1); for (i: series(1000)) { // your code here } print(histogram);

### Roulette Exercise

Roulette -- the wheel with the little ball that falls into a slot, appearing in many movies (Roulette wikipedia). The slots are numbered 0..36. The simplest bet is to take 19..36 (or 1..18), which pays double if the ball falls in that range. If the ball falls in the 0 slot, then all players lose -- this provides the small house edge which is magnified greatly the more times the player plays. The effect of playing more and more times can be explored with the simulation.

Add code here so one trial is playing roulette with a starting balance of 10, betting 1 each time, and playing 50 rounds (or until out of money). For each trial, record the ending balance in the histogram. (Solution available.)

 histogram = new Histogram(0, 35); for (i : series(1000)) { bal = 10; for (j: series(50)) { // your code here } histogram.add(bal); } print(histogram);

The code shows the range of outcomes playing roulette 50 times. What happens if the player plays 500 or 5000 times? Side question: if the player plays 10 times, there is an even/odd pattern in their possible balance. Why is this?

### Rickety Bridge Exercise

You are in a party of 6 on one side of a deep ravine. Pirates are making you all walk across a rickety bridge across the ravine, but only one person can go across at a time. For the 1st person to go across, there is 1/6 chance of bridge collapse. For the 2nd person, a 2/6 chance of collapse, and so on. The 6th person has a 6/6 (100%) chance of collapse if they have to go. If the bridge collapses, nobody else has to go. If you want to avoid being on the bridge when it collapses, which person 1-6 do you want to be?

 var histogram = new Histogram(1, 6); for (i: series(10)) { // person j goes: 1, 2, .. for (j: series(1, 6)) { // Generate a random roll to decide if // bridge collapses for person j. // If bridge collapses for person j, break; // your code here } histogram.add(j); // after break, j is the last person } print(histogram);

What is the best person to be? It's harder to see which is the worst person to be, but it can be done with a large number of trials.

If you are interested in probability, it is also possible to work out the exact mathematics for each person. Construct a tree of the events. From the start position, there are two branches: a. person 1 bridge collapse (1/6), b. person 1 makes it across (5/6). Then extending from branch (b) there are two branches for person 2, and so on. For the probability of a particular outcome, work from the start, multiplying together the probabilities of each branch taken. Note that for each a/b branch, the probabilities add to 1 (either a or b happens). Working out just the first couple layers of the tree, you can check your math vs. the results of the simulation.

### Monty Hall Problem

This is a very famous probability problem based on the Lets Make A Deal TV show. See Monty Hall Problem (wikipedia) for background. Provided for this problem is a `neitherOfThese(a, b)` function which given two numbers that are 1, 2, or 3, returns one of 1, 2, or 3 which is not equal to the two passed in numbers. This function is simple, but turns out to be very useful for the Monty Hall problem.

For the standard strategy, the player keeps their original pick, and so wins 1/3 of the time. The code below simulates the standard strategy. The "switch" strategy means the player changes their pick to the door which is not their original pick and is not the door revealed by Monty. Is the switch strategy superior? The reasoning is very unintuitive, however the simulation can quite clearly show the performance of both strategies. Add code below the standard strategy to implement/print the results of the switch strategy to answer this question.

 win1 = 0; // count regular strategy wins win2 = 0; // count "switch" strategy wins for (i: series(1000)) { // have doors 1,2,3. First select one of them as the "good" one // the other two contain goats. good = random(1, 3) // contestant picks one at random pick = random(1, 3) // Monty hall now reveals a door which is not // the good one, and not the one picked. reveal = neitherOfThese(pick, good); // The standard strategy: don't switch so you win // if the pick is the good door. if (pick == good) { win1 = win1 + 1; } // add switch/win2 strategy here // your code here } print("win1", win1, "win2", win2, "total", i);