I have recently got back into Python code after ill health and slowly using a PC again.
I have written the following code and run it in Jupyter notebook.
- Can the code be simplipyed - I know that there are so many ways to produce the same result.
- I’d like to animate the chart if possible, showing the charts at 10,50,100,500,1000,5000 and 10000 runs of the dice. This is to show how the randomness of a few throws of the dice can be modified into a more structured probability as the number of throws increases. Thanks, Matt
"""
Create a routine to run 'n' random dice rolls
and present them in a histogram.
"""
import matplotlib.pyplot as plt
import numpy as np
def dice(n):
rolls = []
for i in range(n):
two_dice = ( np.random.randint(1, 7) + np.random.randint(1, 7) )
rolls.append(two_dice)
return rolls
"""
now lets use this to build a histogram of 'n' results
"""
data = dice(99000)
# print(data)
bins = np.arange(14) - 0.5
plt.hist(data, bins=bins, color="blue", rwidth=0.85)
plt.grid(True)
plt.title("2 dice rolling histogram")
plt.xlabel("Sum of 2 dice")
plt.ylabel("frequency of rolling the number")
plt.xticks(range(1,14))
# Set axes limit
plt.xlim(1,13)
"""
this chart is exactly as I want it, but can the code be simplipyed?
"""
plt.show()