嘿,我是编程新手,但我似乎不会编写概率问题的代码。例如,我该如何编码? 一个盒子里有12个A型晶体管和18个B型晶体管。随机取出一个晶体管返回。这个过程是重复的。第二种选择的概率是A型,谢谢!在
这是我第一次尝试。在
from scipy import stats as st
import numpy as np
import random
total=30
totalA=12
totalB=18
def transistor():
return random.choice("A","B")
random.seed(0)
for _in range(30):
try1=transistor()
try2=transistor()
if try1="A":
prob1=totalA/total
else:
prob1=totalB/total
if try2="A":
prob2=totalA/total
else:
prob2=totalB/total
if try1=="A" and try2=="A"
prob=2*totalA/total
如果你试图运行一个模拟,这个代码将给你一个10000次试验的概率。每次都会产生不同的结果。试验越多,就越准确。正确的理论答案是
0.24
。在相关问题 更多 >
编程相关推荐