kooldark commited on
Commit
1a67444
·
verified ·
1 Parent(s): 2dad54b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -47
app.py CHANGED
@@ -1,67 +1,74 @@
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": "",
17
- "Cua": "Tôm",
18
- "Tôm": "Cua",
19
- "Bầu": "Cá",
20
- "Cá": "Bầu"
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()
 
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á": "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 í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()