Mathematics/Statistics
확률. 큰 수의 법칙 주사위 던지기 실험 - 코드 포함
PSLeon
2024. 1. 22. 16:22
반응형
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
bins = 100000
fname = ['1', '2', '3', '4', '5', '6']
freq = [0] * 6
prob = [0] * 6
for i in range(bins):
x= np.random.randint(1, 7)
if x == 1: freq[0] += 1
elif x == 2: freq[1] += 1
elif x == 3: freq[2] += 1
elif x == 4: freq[3] += 1
elif x == 5: freq[4] += 1
else:
freq[5] += 1
print(f'주사위를 총 {bins}번 던진 경우')
for i in range(1, 7):
print(f'{i}의 빈도는 {freq[i-1]}회')
prob[i-1] = freq[i-1] / sum(freq)
print(f'{i}가 발생할 확률은 {prob[i-1]:.2f}%')
sns.barplot(x = fname, y = prob)