Question
Dice rolling odds The code shown will simulate rolling a single die (values from 1 to 2). Expand this code to simulate rolling a pair
Dice rolling odds The code shown will simulate rolling a single die (values from 1 to 2). Expand this code to simulate rolling a pair of 6 sided dice 1000 times, store the results in a list and display the percentage of time each roll occurs. If you would like to earn 10 points of extra credit, you may include a matplotlib plot showing a graph of your findings, the title of the graph must include your name (see example output below). Examples of creating graphs can be seen at matplotlib.org under examples.
import random
# List with two zeros to store results of a 2 sided dice roll.
rolls = [0]*2
# Roll random value in the range 1-2
dieRoll = random.randint(1,2)
rolls[dieRoll-1] += 1
dieRoll = random.randint(1,2)
rolls[dieRoll-1] += 1
dieRoll = random.randint(1,2)
rolls[dieRoll-1] += 1
print("Results: %s" % str(rolls))
The percentage can be calculated as the number of times a number was rolled divided by the total number of rolls. When rolling 1 die, the odds of all 6 possibilities should be about equal, but when rolling two dice it should not. For example rolling 7 is much more likely than 2 or 12 since there are more ways to get 7 (2+5, 3+4, etc...).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started