bc6 / app.py
kooldark's picture
Update app.py
fb3da06 verified
raw
history blame
1.74 kB
import streamlit as st
import random
from collections import Counter
# Set up Streamlit app
st.set_page_config(page_title="Bầu Cua Tôm Cá", layout="centered")
st.title("Bầu Cua Tôm Cá")
# UI elements
shake_info = st.empty()
result_label = st.empty()
dice_faces = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
default_faces = ["Cá", "Gà", "Nai"]
selected_faces = []
cols = st.columns(3)
for i, col in enumerate(cols):
with col:
selected_faces.append(st.radio(f"Chọn mặt {i+1}", dice_faces, index=dice_faces.index(default_faces[i])))
def predict_result():
outcomes = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
results = [random.choice(outcomes) for _ in range(500)] # Lắc ban đầu 500 lần
shake_count = 500 # Đếm số lần lắc
while True:
result_counts = Counter(results)
sorted_results = sorted(result_counts.items(), key=lambda x: x[1], reverse=True)
if len(sorted_results) > 1:
rank_1, rank_2 = sorted_results[:2]
rank_1_pct = rank_1[1] / shake_count
rank_2_pct = rank_2[1] / shake_count
if (rank_1_pct - rank_2_pct) > 0.03: # Chênh lệch > 3%
break
# Nếu chưa đạt yêu cầu, tiếp tục lắc từng lần một
results.append(random.choice(outcomes))
shake_count += 1
# Hiển thị kết quả
result_text = f"Số lần lắc: {shake_count}\n\nKết quả:\n"
for outcome, count in sorted_results:
result_text += f"{outcome}: {count} ({count/shake_count:.2%})\n"
result_label.text(result_text)
shake_info.text(f"Tổng số lần lắc: {shake_count}")
# Buttons
if st.button("Lắc"):
predict_result()