Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,66 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import random
|
| 3 |
from collections import Counter
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
st.set_page_config(page_title="Bầu Cua Tôm Cá", layout="centered")
|
| 7 |
-
st.title("Bầu Cua Tôm Cá")
|
| 8 |
-
|
| 9 |
-
# Set font and colors
|
| 10 |
-
st.markdown(
|
| 11 |
-
"""
|
| 12 |
-
<style>
|
| 13 |
-
.stButton>button {
|
| 14 |
-
font-size: 16px;
|
| 15 |
-
background-color: #FF0000;
|
| 16 |
-
color: #FFFFFF;
|
| 17 |
-
}
|
| 18 |
-
.stSelectbox, .stRadio {
|
| 19 |
-
font-size: 16px;
|
| 20 |
-
}
|
| 21 |
-
.stText {
|
| 22 |
-
font-size: 16px;
|
| 23 |
-
color: #000000;
|
| 24 |
-
}
|
| 25 |
-
</style>
|
| 26 |
-
""",
|
| 27 |
-
unsafe_allow_html=True
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
# UI elements
|
| 31 |
-
shake_count = st.selectbox("Số lần lắc", ["1000", "50", "100", "500"])
|
| 32 |
-
shake_info = st.empty()
|
| 33 |
-
result_label = st.empty()
|
| 34 |
-
|
| 35 |
-
dice_faces = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
|
| 36 |
-
default_faces = ["Cá", "Gà", "Nai"]
|
| 37 |
-
selected_faces = []
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
selected_faces.append(st.radio(f"Chọn mặt {i+1}", dice_faces, index=dice_faces.index(default_faces[i])))
|
| 43 |
|
| 44 |
-
#
|
|
|
|
| 45 |
opposite_faces = {
|
| 46 |
-
"
|
| 47 |
-
"
|
| 48 |
-
"
|
| 49 |
-
"Tôm": "
|
| 50 |
-
"
|
| 51 |
-
"Cá": "
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
adjacent_faces = {
|
| 55 |
-
"Bầu": ["Tôm", "Gà", "Nai", "Cá"],
|
| 56 |
-
"Cá": ["Bầu", "Nai", "Cua", "Tôm"],
|
| 57 |
-
"Gà": ["Bầu", "Tôm", "Cua", "Nai"],
|
| 58 |
-
"Tôm": ["Bầu", "Cá", "Cua", "Gà"],
|
| 59 |
-
"Nai": ["Bầu", "Gà", "Cua", "Cá"],
|
| 60 |
-
"Cua": ["Tôm", "Cá", "Nai", "Gà"]
|
| 61 |
}
|
| 62 |
|
| 63 |
-
|
| 64 |
-
outcomes = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
|
| 65 |
-
shake_count_int = int(shake_count)
|
| 66 |
-
|
| 67 |
-
if len(selected_faces) == 3:
|
| 68 |
-
results = [random.choice(outcomes) for _ in range(shake_count_int)]
|
| 69 |
-
result_counts = Counter(results)
|
| 70 |
-
sorted_results = sorted(result_counts.items(), key=lambda x: x[1], reverse=True)
|
| 71 |
-
|
| 72 |
-
result_text = "Kết quả:\n"
|
| 73 |
-
for outcome, count in sorted_results:
|
| 74 |
-
result_text += f"{outcome}: {count} ({count/shake_count_int:.2%})\n"
|
| 75 |
-
|
| 76 |
-
result_label.text(result_text)
|
| 77 |
-
shake_info.text(f"Số lần lắc: {shake_count}")
|
| 78 |
-
else:
|
| 79 |
-
result_label.text("Vui lòng chọn 3 mặt đầu tiên.")
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
shake_info.text("Số lần lắc: 0")
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
if st.button("Lắc"):
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
|
|
|
| 89 |
if st.button("Reset"):
|
| 90 |
-
|
|
|
|
| 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 |
+
"Gà": "Nai",
|
| 16 |
+
"Nai": "Gà",
|
| 17 |
+
"Cua": "Tôm",
|
| 18 |
+
"Tôm": "Cua",
|
| 19 |
+
"Bầu": "Cá",
|
| 20 |
+
"Cá": "Bầu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
+
face_selectors = [st.selectbox(f"Chọn mặt xúc xắc {i+1}", valid_faces) for i in range(3)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Roll times selector
|
| 26 |
+
roll_times = st.selectbox("Số lần lắc:", ["1000", "5", "50", "100", "500", "1"])
|
|
|
|
| 27 |
|
| 28 |
+
# Roll dice button
|
| 29 |
+
if st.button("Lắc Xúc Xắc"):
|
| 30 |
+
faces = face_selectors
|
| 31 |
+
roll_times = int(roll_times)
|
| 32 |
+
|
| 33 |
+
# Simulate rolling the dice
|
| 34 |
+
roll_history = []
|
| 35 |
+
for _ in range(roll_times):
|
| 36 |
+
rolled_faces = []
|
| 37 |
+
for face in faces:
|
| 38 |
+
valid_choices = [f for f in valid_faces if f != opposite_faces[face]]
|
| 39 |
+
rolled_faces.append(secrets.choice(valid_choices)) # Use secrets.choice for better randomness
|
| 40 |
+
roll_history.extend(rolled_faces)
|
| 41 |
+
|
| 42 |
+
# Count the frequency of each face
|
| 43 |
+
face_counts = Counter(roll_history)
|
| 44 |
+
|
| 45 |
+
# Calculate the probability of each face (%)
|
| 46 |
+
total_rolls = len(roll_history)
|
| 47 |
+
probabilities = {face: (count / total_rolls) * 100 for face, count in face_counts.items()}
|
| 48 |
+
|
| 49 |
+
# Sort probabilities
|
| 50 |
+
sorted_probabilities = sorted(probabilities.items(), key=lambda x: x[1], reverse=True)
|
| 51 |
+
|
| 52 |
+
# Sort face counts by the number of rolls in descending order
|
| 53 |
+
sorted_face_counts = sorted(face_counts.items(), key=lambda x: x[1], reverse=True)
|
| 54 |
+
|
| 55 |
+
# Display results
|
| 56 |
+
st.write(f"Kết quả lắc xúc xắc (tổng cộng {total_rolls // 3} lần):")
|
| 57 |
+
for face, count in sorted_face_counts:
|
| 58 |
+
st.write(f"{face}: {count} lần")
|
| 59 |
+
|
| 60 |
+
st.write("\nXác suất tổng hợp:")
|
| 61 |
+
for rank, (face, prob) in enumerate(sorted_probabilities, 1):
|
| 62 |
+
st.write(f"{rank}. {face}: {prob:.2f}%")
|
| 63 |
|
| 64 |
+
# Reset button
|
| 65 |
if st.button("Reset"):
|
| 66 |
+
st.experimental_rerun()
|