Use python to write a program: “Roll” 2 dice 10,000 times keeping track of all t
ID: 3703004 • Letter: U
Question
Use python to write a program:
“Roll” 2 dice 10,000 times keeping track of all the sums of each set of rolls in a list. Then use your program to generate a histogram summarizing the rolls of two dice 10,000 times. The result should look like the histogram plotted below. Use the MatPlotLib function hist (see http://matplotlib.org/api/pyplot_summary.html) and set the number of bins in the histogram equal to the number of different possible outcomes of a roll of your dice. For example, the sum of two dice can be anything between 2 and 12, which corresponds to 11 possible outcomes.
Explanation / Answer
from random import randint import matplotlib.pyplot as plt def main(): counts = [0 for x in range(2, 13)] for i in range(10000): d1 = randint(1, 6) d2 = randint(1, 6) counts[d1+d2-2] += 1 plt.hist(counts, 11) main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.