import streamlit as st import secrets # Use secrets instead of random for better randomness from collections import Counter # Streamlit app setup 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") # Title and instructions st.title("Bầu Cua Tôm Cá") st.subheader("Chọn 3 mặt xúc xắc:") # Dropdown menus for dice faces 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à" } # Change dropdowns to radio buttons face_selectors = [st.radio(f"Chọn mặt xúc xắc {i+1}", valid_faces) for i in range(3)] # Roll times selector roll_times = st.selectbox("Số lần lắc:", ["1000", "5", "50", "100", "500", "1"]) # Roll dice button if st.button("Lắc Xúc Xắc"): faces = face_selectors roll_times = int(roll_times) # Simulate rolling the dice 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)) # Use secrets.choice for better randomness roll_history.extend(rolled_faces) # Count the frequency of each face face_counts = Counter(roll_history) # Calculate the probability of each face (%) total_rolls = len(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) # Sort face counts by the number of rolls in descending order sorted_face_counts = sorted(face_counts.items(), key=lambda x: x[1], reverse=True) # Display results 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}%") # Reset button if st.button("Reset"): st.experimental_rerun()