Project 21: Dice Fortune Arena 🎲¶

Difficulty: 🟢 Beginner

Run cells top to bottom. Each step builds on the previous one and saves real files under outputs/dice_fortune_arena/.

← Back to index

Description¶

Simulate thousands of dice rolls, chart the odds, and print a playful fortune message.

Libraries & Modules¶

  • random, collections.Counter
  • matplotlib.pyplot

Python Concepts You'll Practice¶

Loops, randomness, counting distributions, bar charts

🎛️ Parameter Variations¶

  • SIDES — 6 vs 20 sided dice
  • ROLLS — 100 vs 10_000 simulations
  • SEED — reproducible luck
In [1]:
# Install dependencies for this project (safe to re-run)
import sys
!{sys.executable} -m pip install pillow numpy matplotlib wordcloud qrcode[pil] python-barcode fpdf2 jinja2 folium plotly pandas scipy ipywidgets -q
Skipping pip install (already installed)
In [2]:
# Interactive Jupyter setup — run this cell first
%matplotlib inline

from pathlib import Path
from IPython.display import display, Image as IPImage, HTML, Markdown, Audio, IFrame

OUTPUT_DIR = Path("outputs") / "dice_fortune_arena"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"✅ Outputs folder: {OUTPUT_DIR.resolve()}")
✅ Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\dice_fortune_arena

Step 1 — Roll the dice many times¶

In [3]:
import random
from collections import Counter

# 🎛️ TWEAK THESE
SIDES = 6
ROLLS = 5000
SEED = 42

random.seed(SEED)
rolls = [random.randint(1, SIDES) for _ in range(ROLLS)]
counts = Counter(rolls)
print("Face -> count")
for face in range(1, SIDES + 1):
    print(f"  {face}: {counts[face]} ({counts[face] / ROLLS:.1%})")
Face -> count
  1: 808 (16.2%)
  2: 814 (16.3%)
  3: 842 (16.8%)
  4: 852 (17.0%)
  5: 819 (16.4%)
  6: 865 (17.3%)

Step 2 — Chart the luck + write a fortune¶

In [4]:
import matplotlib.pyplot as plt

faces = list(range(1, SIDES + 1))
values = [counts[f] for f in faces]
lucky = max(faces, key=lambda f: counts[f])

fig, ax = plt.subplots(figsize=(8, 4))
ax.bar(faces, values, color="#111111", edgecolor="white")
ax.set_title(f"Dice Fortune Arena — {ROLLS} rolls of a d{SIDES}")
ax.set_xlabel("Face")
ax.set_ylabel("Count")
fig.tight_layout()
chart = OUTPUT_DIR / "dice_fortune_chart.png"
fig.savefig(chart, dpi=140)
plt.show()

fortunes = {
    1: "A quiet start becomes a loud win.",
    2: "Double down on curiosity today.",
    3: "Three ideas > one perfect plan.",
    4: "Stability is your superpower.",
    5: "Try the bold option first.",
    6: "Your streak is loading… keep going.",
}
msg = fortunes.get(lucky, "The universe rolled in your favor.")
out = OUTPUT_DIR / "fortune.txt"
out.write_text(f"Lucky face: {lucky}\nFortune: {msg}\n", encoding="utf-8")
print(f"Lucky face {lucky} → {msg}")
print(f"Saved {chart.name} and {out.name}")
No description has been provided for this image
Lucky face 6 → Your streak is loading… keep going.
Saved dice_fortune_chart.png and fortune.txt

✅ Project complete¶

Check your output folder: outputs/dice_fortune_arena/

Next: Project 22: Caesar Cipher Club · Index