|
|
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="🎲", 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 = { |
|
|
"Gà": "Nai", "Nai": "Gà", |
|
|
"Cua": "Tôm", "Tôm": "Cua", |
|
|
"Bầu": "Cá", "Cá": "Bầu" |
|
|
} |
|
|
|
|
|
|
|
|
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 ban đầu:", ["1000", "5", "50", "100", "500", "1"]) |
|
|
roll_times = int(roll_times) |
|
|
|
|
|
|
|
|
if st.button("Lắc Xúc Xắc"): |
|
|
faces = face_selectors |
|
|
roll_history = [] |
|
|
max_iterations = 1000 |
|
|
iteration = 0 |
|
|
|
|
|
while iteration < max_iterations: |
|
|
iteration += 1 |
|
|
|
|
|
|
|
|
for _ in range(roll_times): |
|
|
rolled_faces = [secrets.choice([f for f in valid_faces if f != opposite_faces[face]]) for face in faces] |
|
|
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_face_counts = sorted(face_counts.items(), key=lambda x: x[1], reverse=True) |
|
|
|
|
|
|
|
|
if len(sorted_face_counts) > 1: |
|
|
rank_1_face, rank_1_count = sorted_face_counts[0] |
|
|
rank_2_face, rank_2_count = sorted_face_counts[1] |
|
|
|
|
|
if rank_1_count - rank_2_count >= 30: |
|
|
break |
|
|
|
|
|
|
|
|
roll_times *= 2 |
|
|
|
|
|
|
|
|
st.write(f"🔄 Đã lắc tổng cộng **{total_rolls // 3} lần** sau {iteration} vòng lặp:") |
|
|
for face, count in sorted_face_counts: |
|
|
st.write(f"- {face}: {count} lần") |
|
|
|
|
|
st.write("\n📊 **Xác suất tổng hợp:**") |
|
|
for rank, (face, prob) in enumerate(sorted(probabilities.items(), key=lambda x: x[1], reverse=True), 1): |
|
|
st.write(f"{rank}. {face}: {prob:.2f}%") |
|
|
|
|
|
|
|
|
if st.button("Reset"): |
|
|
st.experimental_rerun() |