Create App2.py
Browse files
App2.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import secrets # Use secrets instead of random for better randomness
|
| 3 |
+
from collections import Counter
|
| 4 |
+
|
| 5 |
+
# Streamlit app setup
|
| 6 |
+
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")
|
| 7 |
+
|
| 8 |
+
# Title and instructions
|
| 9 |
+
st.title("Bầu Cua Tôm Cá")
|
| 10 |
+
st.subheader("Chọn 3 mặt xúc xắc:")
|
| 11 |
+
|
| 12 |
+
# Dropdown menus for dice faces
|
| 13 |
+
valid_faces = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
|
| 14 |
+
opposite_faces = {
|
| 15 |
+
"Tôm": "Nai",
|
| 16 |
+
"Nai": "Tôm",
|
| 17 |
+
"Cua": "Bầu",
|
| 18 |
+
"Bầu": "Cua",
|
| 19 |
+
"Gà": "Cá",
|
| 20 |
+
"Cá": "Gà"
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
# Change dropdowns to radio buttons
|
| 24 |
+
face_selectors = [st.radio(f"Chọn mặt xúc xắc {i+1}", valid_faces) for i in range(3)]
|
| 25 |
+
|
| 26 |
+
# Roll times selector
|
| 27 |
+
roll_times = st.selectbox("Số lần lắc:", ["1000", "5", "50", "100", "500", "1"])
|
| 28 |
+
|
| 29 |
+
# Roll dice button
|
| 30 |
+
if st.button("Lắc Xúc Xắc"):
|
| 31 |
+
faces = face_selectors
|
| 32 |
+
roll_times = int(roll_times)
|
| 33 |
+
|
| 34 |
+
# Simulate rolling the dice
|
| 35 |
+
roll_history = []
|
| 36 |
+
for _ in range(roll_times):
|
| 37 |
+
rolled_faces = []
|
| 38 |
+
for face in faces:
|
| 39 |
+
valid_choices = [f for f in valid_faces if f != opposite_faces[face]]
|
| 40 |
+
rolled_faces.append(secrets.choice(valid_choices)) # Use secrets.choice for better randomness
|
| 41 |
+
roll_history.extend(rolled_faces)
|
| 42 |
+
|
| 43 |
+
# Count the frequency of each face
|
| 44 |
+
face_counts = Counter(roll_history)
|
| 45 |
+
|
| 46 |
+
# Calculate the probability of each face (%)
|
| 47 |
+
total_rolls = len(roll_history)
|
| 48 |
+
probabilities = {face: (count / total_rolls) * 100 for face, count in face_counts.items()}
|
| 49 |
+
|
| 50 |
+
# Sort probabilities
|
| 51 |
+
sorted_probabilities = sorted(probabilities.items(), key=lambda x: x[1], reverse=True)
|
| 52 |
+
|
| 53 |
+
# Sort face counts by the number of rolls in descending order
|
| 54 |
+
sorted_face_counts = sorted(face_counts.items(), key=lambda x: x[1], reverse=True)
|
| 55 |
+
|
| 56 |
+
# Display results
|
| 57 |
+
st.write(f"Kết quả lắc xúc xắc (tổng cộng {total_rolls // 3} lần):")
|
| 58 |
+
for face, count in sorted_face_counts:
|
| 59 |
+
st.write(f"{face}: {count} lần")
|
| 60 |
+
|
| 61 |
+
st.write("\nXác suất tổng hợp:")
|
| 62 |
+
for rank, (face, prob) in enumerate(sorted_probabilities, 1):
|
| 63 |
+
st.write(f"{rank}. {face}: {prob:.2f}%")
|
| 64 |
+
|
| 65 |
+
# Reset button
|
| 66 |
+
if st.button("Reset"):
|
| 67 |
+
st.experimental_rerun()
|