File size: 2,334 Bytes
de9cbdf c43938d de9cbdf 5baa252 de9cbdf 2dd8bc7 de9cbdf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import random
from collections import Counter
import gradio as gr
class DiceProbabilityApp:
def __init__(self):
self.valid_faces = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
self.roll_history = []
def roll_dice(self, face1, face2, face3, roll_times):
faces = [face1, face2, face3]
roll_times = int(roll_times)
# Simulate rolling the dice
for _ in range(roll_times):
rolled_faces = [random.choice(self.valid_faces) for _ in range(3)]
self.roll_history.extend(rolled_faces)
# Count the frequency of each face
face_counts = Counter(self.roll_history)
# Calculate the probability of each face (%)
total_rolls = len(self.roll_history)
probabilities = {face: (count / total_rolls) * 100 for face, count in face_counts.items()}
# Sort probabilities
sorted_probabilities = sorted(probabilities.items(), key=lambda x: x[1], reverse=True)
# Display results
result_text = f"Kết quả lắc xúc xắc (tổng cộng {total_rolls // 3} lần):\n"
for face, count in face_counts.items():
result_text += f"{face}: {count} lần\n"
result_text += "\nXác suất tổng hợp:\n"
for rank, (face, prob) in enumerate(sorted_probabilities, 1):
result_text += f"{rank}. {face}: {prob:.2f}%\n"
return result_text
def reset_selections(self):
self.roll_history = []
return "Đã reset kết quả."
app = DiceProbabilityApp()
iface = gr.Interface(
fn=app.roll_dice,
inputs=[
gr.Dropdown(choices=app.valid_faces, label="Chọn mặt xúc xắc 1"),
gr.Dropdown(choices=app.valid_faces, label="Chọn mặt xúc xắc 2"),
gr.Dropdown(choices=app.valid_faces, label="Chọn mặt xúc xắc 3"),
gr.Dropdown(choices=["1", "3", "5", "10"], label="Số lần lắc")
],
outputs="text",
live=True,
title="Bầu Cua Tôm Cá - Xác Suất Xúc Xắc",
description="Chọn 3 mặt xúc xắc và số lần lắc để xem kết quả và xác suất."
)
iface.add_button("Quay Xúc Xắc", app.roll_dice)
iface.add_button("Reset", app.reset_selections)
if __name__ == "__main__":
iface.launch()
|