Rolling a fair die is in some sense a generalization of a flipping a coin. The regular six-sided die allows for many interesting questions:
- What is the probability of rolling a 1?
- What is the probability of rolling an even number?
- If you roll a die 10 times, how many times do you expect to roll a 2?
- If you roll a die 1000 times, how many times do you expect to roll a 1 or a 6?
There exist die with fewer as well as more than six faces. A 10-sided die shows the numbers from 1 to 10. We can ask similar questions:
- What is the probability of rolling an 8, a 9, or a 10?
- If you roll the die 500 times, how many times do you expect to get a 5?
In many games we don’t roll one die, but two or more. Assume we roll 2 die.
- What is the probability that we roll a sum of 4?
- If we roll two dice 100 times, how many times do we expect to get a pair?
The probabilities of some of the above questions may not be straightforward to determine mathematically. However, once we know how to simulate throwing a die, we answer these questions experimentally.
Programming Guidelines
For this assignment, you will simulate rolling a die. Before starting to program, think about the following:
- How can you simulate rolling a regular six-sided die? A regular 10-sided die? Think about this in terms of flipping a coin; the randomness can be achieved the same way, but now there are more possible results.
- How will you keep track of the results?
- How could you model a weighted die that rolls a given number with a higher probability? (for more advanced students to consider)
Program
Start with your program for the Coin Toss Simulation and adjust it to make it about Rolling a 6-sided Die. Note the following changes:
- Rolling a 6-sided die requires choosing a random number from 1 to 6
- Keep track of the results either with six variables representing the six possible outcomes OR, more efficiently with a list with six elements
Adjusting your program to model a die with, for example, 10 sides, requires minimal changes:
- Rolling now requires a random number from 1 to 10
- Keeping track of the results will require either more variables OR a larger list.
NOTE: keeping track of results using a list will require significantly less code, as the result of the die-roll can be used to directly index the list. EX: Let result be the value of the roll, then updating the results list is simply: list[result -1] ++.
Sample Programs
Program 1 – Simulation of Die Throwing (using a list)
#Simulation of Die Throwing (using a list) import random test_data = [0, 0, 0, 0, 0, 0] #this will keep track of the results n = 1000 # this can be easily changed for different sample sizes for i in range(n): result = random.randint(1, 6) test_data[result - 1] = test_data[result - 1] + 1 for i in range(0, 6): print "Number of ", i+1, "'s: ", test_data[i] #To change the number of sides on the die, change the size of #test_data and the range for the random number.
Program 2 – Simulation of Tossing a Weighted Die
#Simulation of Tossing a Weighted Die import random n = 1000 test_data = [0, 0, 0, 0, 0, 0] results = [1, 1, 1, 1, 1, 2, 3, 4, 5, 6] #die weighted to roll a 1 half the time for i in range(n): index = random.randint(0, 9) result = results[index] test_data[result - 1] = test_data[result - 1] + 1 for i in range(0, 6): print "Number of ", i+1, "'s: ", test_data[i]
Program 3 – Simulation of Tossing a Die to find Average result
#Simulation of Tossing a Die to find Average result #Uses a visual representation for the results import matplotlib.pyplot as plt import numpy as np import random def throw_die(data_list, times): total = 0 for i in range(times): num = random.randint(1,6) total = total + num data_list[num-1] = data_list[num-1] + 1 average = total * 1.0 / times return average test_data = [0, 0, 0, 0, 0, 0] #use a list to keep the times each number appears times = 1000 # throw a die n times average = throw_die(test_data,times) print 'The average is',average #Plot the result ind = np.arange(6) # the x locations for the groups width = 0.5 # the width of the bars fig, ax = plt.subplots() result = ax.bar(ind+width/2, test_data, width,color='r') ax.set_ylabel('Counts') ax.set_title('Throw a Die') ax.set_xticks(ind+width) ax.set_xticklabels( ('1', '2', '3', '4', '5','6') ) plt.show()
Internet Resources
- https://www.khanacademy.org/math/probability/independent-dependent-probability/independent_events/v/events-and-outcomes-2
- Probability problem examples related to rolling a die
- Approximately 5 min
- https://www.random.org/dice/?num=10
- Allows you to roll multiple dice once
- Shows results visually
- Uses atmospheric noise to achieve randomness
- http://www.roll-dice-online.com/
- Can change # of die, # of sides, and # of rolls
- Shows individual results and averages
- http://www.shodor.org/interactivate/activities/ExpProbability/
- Roll two dice 1, 5, or 10 times
- Can change layout of die (e.g. make 6-sided die with two 4’s, etc.)
- Results show the sum of the two dice
- https://online.maryville.edu/online-masters-degrees/business-data-analytics/students-guide-to-probability-and-statistics/
- This page includes useful resources about rolling a die and probability