Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,74 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import secrets #
|
| 3 |
from collections import Counter
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
st.set_page_config(page_title="Bầu Cua Tôm Cá - Xác Suất Xúc Xắc 3", page_icon="
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
st.title("Bầu Cua Tôm Cá")
|
| 10 |
st.subheader("Chọn 3 mặt xúc xắc:")
|
| 11 |
|
| 12 |
-
#
|
| 13 |
valid_faces = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
|
| 14 |
opposite_faces = {
|
| 15 |
-
"Gà": "Nai",
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
"Tôm": "Cua",
|
| 19 |
-
"Bầu": "Cá",
|
| 20 |
-
"Cá": "Bầu"
|
| 21 |
}
|
| 22 |
|
| 23 |
-
#
|
| 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 |
-
#
|
| 27 |
-
roll_times = st.selectbox("Số lần lắc:", ["1000", "5", "50", "100", "500", "1"])
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 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 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
for face, count in sorted_face_counts:
|
| 59 |
-
st.write(f"{face}: {count} lần")
|
| 60 |
-
|
| 61 |
-
st.write("\
|
| 62 |
-
for rank, (face, prob) in enumerate(
|
| 63 |
st.write(f"{rank}. {face}: {prob:.2f}%")
|
| 64 |
|
| 65 |
-
# Reset
|
| 66 |
if st.button("Reset"):
|
| 67 |
-
st.experimental_rerun()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import secrets # Dùng secrets thay cho random để tăng tính ngẫu nhiên
|
| 3 |
from collections import Counter
|
| 4 |
|
| 5 |
+
# Cấu hình Streamlit
|
| 6 |
+
st.set_page_config(page_title="Bầu Cua Tôm Cá - Xác Suất Xúc Xắc 3", page_icon="🎲", layout="centered")
|
| 7 |
|
| 8 |
+
# Tiêu đề ứng dụng
|
| 9 |
+
st.title("🎲 Bầu Cua Tôm Cá 🎲")
|
| 10 |
st.subheader("Chọn 3 mặt xúc xắc:")
|
| 11 |
|
| 12 |
+
# Danh sách các mặt xúc xắc hợp lệ
|
| 13 |
valid_faces = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
|
| 14 |
opposite_faces = {
|
| 15 |
+
"Gà": "Nai", "Nai": "Gà",
|
| 16 |
+
"Cua": "Tôm", "Tôm": "Cua",
|
| 17 |
+
"Bầu": "Cá", "Cá": "Bầu"
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
+
# Chọn mặt xúc xắc bằng radio button
|
| 21 |
face_selectors = [st.radio(f"Chọn mặt xúc xắc {i+1}", valid_faces) for i in range(3)]
|
| 22 |
|
| 23 |
+
# Chọn số lần lắc ban đầu
|
| 24 |
+
roll_times = st.selectbox("Số lần lắc ban đầu:", ["1000", "5", "50", "100", "500", "1"])
|
| 25 |
+
roll_times = int(roll_times)
|
| 26 |
|
| 27 |
+
# Khi nhấn nút "Lắc Xúc Xắc"
|
| 28 |
if st.button("Lắc Xúc Xắc"):
|
| 29 |
faces = face_selectors
|
|
|
|
|
|
|
|
|
|
| 30 |
roll_history = []
|
| 31 |
+
max_iterations = 1000 # Giới hạn số vòng lặp để tránh chạy vô tận
|
| 32 |
+
iteration = 0
|
| 33 |
+
|
| 34 |
+
while iteration < max_iterations:
|
| 35 |
+
iteration += 1
|
| 36 |
+
|
| 37 |
+
# Lắc xúc xắc và ghi nhận kết quả
|
| 38 |
+
for _ in range(roll_times):
|
| 39 |
+
rolled_faces = [secrets.choice([f for f in valid_faces if f != opposite_faces[face]]) for face in faces]
|
| 40 |
+
roll_history.extend(rolled_faces)
|
| 41 |
+
|
| 42 |
+
# Đếm số lần xuất hiện của từng mặt
|
| 43 |
+
face_counts = Counter(roll_history)
|
| 44 |
+
total_rolls = len(roll_history)
|
| 45 |
+
|
| 46 |
+
# Tính xác suất
|
| 47 |
+
probabilities = {face: (count / total_rolls) * 100 for face, count in face_counts.items()}
|
| 48 |
+
|
| 49 |
+
# Sắp xếp theo số lần xuất hiện
|
| 50 |
+
sorted_face_counts = sorted(face_counts.items(), key=lambda x: x[1], reverse=True)
|
| 51 |
+
|
| 52 |
+
# Nếu có ít nhất 2 mặt xúc xắc, kiểm tra điều kiện dừng
|
| 53 |
+
if len(sorted_face_counts) > 1:
|
| 54 |
+
rank_1_face, rank_1_count = sorted_face_counts[0]
|
| 55 |
+
rank_2_face, rank_2_count = sorted_face_counts[1]
|
| 56 |
+
|
| 57 |
+
if rank_1_count - rank_2_count >= 30:
|
| 58 |
+
break # Dừng vòng lặp nếu điều kiện được thỏa mãn
|
| 59 |
+
|
| 60 |
+
# Tăng số lần lắc để tiếp tục thử nghiệm
|
| 61 |
+
roll_times *= 2 # Nhân đôi số lần lắc để đạt điều kiện nhanh hơn
|
| 62 |
+
|
| 63 |
+
# Hiển thị kết quả cuối cùng
|
| 64 |
+
st.write(f"🔄 Đã lắc tổng cộng **{total_rolls // 3} lần** sau {iteration} vòng lặp:")
|
| 65 |
for face, count in sorted_face_counts:
|
| 66 |
+
st.write(f"- {face}: {count} lần")
|
| 67 |
+
|
| 68 |
+
st.write("\n📊 **Xác suất tổng hợp:**")
|
| 69 |
+
for rank, (face, prob) in enumerate(sorted(probabilities.items(), key=lambda x: x[1], reverse=True), 1):
|
| 70 |
st.write(f"{rank}. {face}: {prob:.2f}%")
|
| 71 |
|
| 72 |
+
# Reset ứng dụng
|
| 73 |
if st.button("Reset"):
|
| 74 |
+
st.experimental_rerun()
|