I've developed a Python script that simulates die rolls and analyses the results. I'm now looking to extend and modify this code for more complex data science tasks and simulations.
- Is this code simple and readable?
- Are there more insightful statistics or visualisations that can be generated from the die roll data?
- How could this code be extended or modified for more complex data science tasks or simulations?
import unittest
from random import randint
import matplotlib.pyplot as plt
import pandas as pd
def roll_die() -> int:
"""Simulate rolling a fair six-sided die and return the result"""
return randint(1, 6)
num_rolls = 1000
die_rolls = [roll_die() for _ in range(num_rolls)]
df = pd.DataFrame({"Rolls": die_rolls})
roll_counts = df["Rolls"].value_counts().sort_index()
print(df)
print(roll_counts)
plt.bar(roll_counts.index, roll_counts.values)
plt.xlabel("Die Face")
plt.ylabel("Frequency")
plt.title("Die Roll Distribution")
plt.show()
class TestRollDie(unittest.TestCase):
def test_roll_die(self):
result = roll_die()
self.assertTrue(1 <= result <= 6)
if __name__ == "__main__":
unittest.main()
