## Lab Exercise 7

### Introduction

Create a directory on your USB device for this lab, say, lab07, and launch IDLE.

This week, you will not submit your shell window at the end of the session. Don't worry if you close it accidentally along the way.

You will submit a responses.txt file this week. Download this template file and use it to record any answers or predictions asked for in the exercises.

### Random Numbers

Python's built-in random module defines a boatload of functions for working with random numbers. They are not truly random, of course, but they behave sufficiently like random numbers that we can use them effectively in many settings.

Today, you will use two functions from the random module:

• randint(lower, upper)

... returns a random integer n such that lowernupper.

• random()

... returns a random floating-point number f such that 0.0f < 1.0.

You can use a longer form of the import command to make these functions available directly:

```    from random import randint
die_1 = randint(1,6)

from random import random
game_result = random()
```

These functions can be used to model the world in a number of ways. Today, we will use them in specific ways:

• We can use randint() to simulate an n-sided die. Call it with arguments that match the die's smallest and largest values, and it returns an integer in that range.

• We can use random() to simulate the result of a game between two opponents. Suppose that Team A has a 60% chance of winning in a game against Team B. Call random() and compare the result to 0.60. If it is less than 0.6, say that Team A won the game. If not, say that Team B did.

Let's run some experiments!

### Task 1: Run an Experiment with Dice

When we roll two dice, sums in the middle are more likely than sums at the extremes. For example, if we roll two six-sided dice, then a sum of 7 is much more likely than a sum of 2 or 12. This is an important part of the strategy in many games, such as backgammon and craps.

Of course, random numbers are random. With a small number of rolls, we might see an unusual number of 2s or 12s compared to 7s. But as the number of rolls goes up, if the dice are fair, then we expect to see the distribution of 2s to 7s to 12s behave as expected.

What is the probability distribution of the rolls for a pair of dice?

Write a program to help us answer this question. Name it throwing_dice.py.

To keep your programming job manageable, use 4-sided dice. This means that the total for a pair will range between 2 and 8.

Here are some pointers:

• Ask the user for the number of times to roll the dice. Simulate rolling a pair of dice that many times. Keep track of how many times you see each possible value, 2 through 12. At the end, print out the number of rolls for each possible value, as well as the percentage of rolls for each value. For six-sided dice, the output might look like:
```    value count percent

2    149    2.98
3    289    5.78
..
7    824   16.48
..
11    296    5.92
12    134    2.68
```

• Use the string formatting commands you saw in Chapter 4 to print your table in neat columns.

Run two trials of your program: 5,000 rolls and 5,000,000 rolls. Copy the tables printed by the program into your responses file.

### Task 2: Run an Experiment with a 7-Game Series

When two teams play a game, we expect the stronger team to win most of the time. But we also know that the weaker team will occasionally win. This isn't so much a matter of randomness as probability.

When two teams play a best-of-seven-games series, we also expect the stronger team to win most of the time. By playing several games, it becomes harder for the weaker team to win. It may win a game or two or even three, but the better team is more likely to win each game, and thus to prevail in the series. Still, we know that the weaker team will occasionally win even a seven-game series.

What is the likelihood that the stronger team
wins a seven-game series?

Write a program to help us answer this question. Name it seven_game_series.py.

As a user, I am interested in the current baseball playoffs, where I estimate the stronger team typically has a 51% to 54% chance of winning a single game against the weaker team.

Here are some pointers:

• First, write code to simulate a seven-game series. Ask the user for the probability that Team A (the stronger team) wins a single game against Team B. Loop until one of the teams has won four games. When the loop terminates, print the number of games won by each team.

• Next, add code to simulate multiple seven-game series. Ask the user for a second input: the number of series to play. Loop this many times over the code that plays a single series. Keep track of how many series each team wins. When the outer loop terminates, print the number of series won by each team, as well the percentage of series won by each.

• Finally (time permitting), add code to keep track of the number of games won by the losing team in each series. (The winner will, of course, win four!) When reporting results, add code to print the average number of games won by the losing team.

• Use the string formatting commands you saw in Chapter 4 to print your output in neat columns. For example:
```            count   percent
Team A  2828126    56.6
Team B  2171874    43.4
```

Run two trials of your program: 5,000 seven-game series and 5,000,000 seven-game series. Copy the results printed by the program into your responses file.

### Finishing Up

Make sure that your program files are complete and saved. Save your responses.txt file.

Submit your files for grading on the electronic submission system, at lab07 -- Experiments with Randomness.

• throwing_dice.py
• seven_game_series.py
• responses.txt

As always, make sure you see the verification screen that says The files listed above were uploaded.

If you need any help, let me know.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... October 8, 2014