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.
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:
... returns a random integer n such that lower ≤ n ≤ upper.
... returns a random floating-point number f such that 0.0 ≤ f < 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:
Let's run some experiments!
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:
value count percent 2 149 2.98 3 289 5.78 .. 7 824 16.48 .. 11 296 5.92 12 134 2.68
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.
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:
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.
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.