All gists matching topic monte-carlo-simulation

timmy / 模擬硬幣擲投並計算機率

0 polubień
0 forków
1 plików
Ostatnio aktywne 10 months ago
這段程式碼用於模擬擲硬幣 n 次,並統計正面與反面的機率。程式會計算並顯示正面與反面的出現次數與機率,以驗證隨機性。
1 import random
2
3
4 def toss_coin():
5 # 0代表正面,1代表反面
6 return "正面" if random.randint(0, 1) == 0 else "反面"
7
8
9 def simulate_tosses(n):
10 # 初始化計數器
Nowsze Starsze