Lab Exercise 7

Experiments with Randomness


CS 1510
Introduction to Computing


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:

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:

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.

Your task is to answer this question:

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:

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.

Your task is to answer this question:

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:

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.

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