|
import streamlit as st |
|
import random |
|
from collections import Counter |
|
|
|
|
|
st.set_page_config(page_title="Bầu Cua Tôm Cá", layout="centered") |
|
st.title("Bầu Cua Tôm Cá") |
|
|
|
|
|
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)] |
|
shake_count = 500 |
|
|
|
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: |
|
break |
|
|
|
|
|
results.append(random.choice(outcomes)) |
|
shake_count += 1 |
|
|
|
|
|
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}") |
|
|
|
|
|
if st.button("Lắc"): |
|
predict_result() |