Python – Plotting histogram of a for loop for dice rolls

import matplotlib.pyplot as plt
import random

def roll(n):
    outcomes = [random.randint(1, 6) for _ in range(n)]
    return sum(outcomes)

rolls = 20
simu = 1000
sums = [roll(rolls) for _ in range(simu)]

plt.hist(sums, bins=40)
plt.title(f"Sum of {rolls} rolls ({simu} times)")

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top