|
|
import streamlit as st |
|
|
import secrets |
|
|
from collections import Counter |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Bầu Cua Tôm Cá - Xác Suất Xúc Xắc 3", page_icon="dice_icon.png", layout="centered") |
|
|
|
|
|
|
|
|
st.title("Bầu Cua Tôm Cá") |
|
|
st.subheader("Chọn 3 mặt xúc xắc:") |
|
|
|
|
|
|
|
|
valid_faces = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"] |
|
|
opposite_faces = { |
|
|
"Tôm": "Nai", |
|
|
"Nai": "Tôm", |
|
|
"Cua": "Bầu", |
|
|
"Bầu": "Cua", |
|
|
"Gà": "Cá", |
|
|
"Cá": "Gà" |
|
|
} |
|
|
|
|
|
|
|
|
face_selectors = [st.radio(f"Chọn mặt xúc xắc {i+1}", valid_faces) for i in range(3)] |
|
|
|
|
|
|
|
|
roll_times = st.selectbox("Số lần lắc:", ["1000", "5", "50", "100", "500", "1"]) |
|
|
|
|
|
|
|
|
if st.button("Lắc Xúc Xắc"): |
|
|
faces = face_selectors |
|
|
roll_times = int(roll_times) |
|
|
|
|
|
|
|
|
roll_history = [] |
|
|
for _ in range(roll_times): |
|
|
rolled_faces = [] |
|
|
for face in faces: |
|
|
valid_choices = [f for f in valid_faces if f != opposite_faces[face]] |
|
|
rolled_faces.append(secrets.choice(valid_choices)) |
|
|
roll_history.extend(rolled_faces) |
|
|
|
|
|
|
|
|
face_counts = Counter(roll_history) |
|
|
|
|
|
|
|
|
total_rolls = len(roll_history) |
|
|
probabilities = {face: (count / total_rolls) * 100 for face, count in face_counts.items()} |
|
|
|
|
|
|
|
|
sorted_probabilities = sorted(probabilities.items(), key=lambda x: x[1], reverse=True) |
|
|
|
|
|
|
|
|
sorted_face_counts = sorted(face_counts.items(), key=lambda x: x[1], reverse=True) |
|
|
|
|
|
|
|
|
st.write(f"Kết quả lắc xúc xắc (tổng cộng {total_rolls // 3} lần):") |
|
|
for face, count in sorted_face_counts: |
|
|
st.write(f"{face}: {count} lần") |
|
|
|
|
|
st.write("\nXác suất tổng hợp:") |
|
|
for rank, (face, prob) in enumerate(sorted_probabilities, 1): |
|
|
st.write(f"{rank}. {face}: {prob:.2f}%") |
|
|
|
|
|
|
|
|
if st.button("Reset"): |
|
|
st.experimental_rerun() |
|
|
|