python 学习 羊车门问题 DAY19

(一)

import random
counts = 10000
sum_y1 = 0
sum_y2 = 0
ls1 = ["羊1","羊2","车"]
ls2 = ["羊","车"]
for i in range(counts): #第一种
    pc = random.choice(ls1)
    cc = random.choice(ls1)
    if pc == cc:
        sum_y1 += 1
for j in range(counts): #第二种
    pc = random.choice(ls2)
    cc = random.choice(ls2)
    if pc == cc:
        sum_y2 += 1
print("第一种概率为{0:.2f} , 第二种概率为{1:.2f}".format(sum_y1/counts,sum_y2/counts))
 

(二)

import random
counts = 10000
sum_y1 = 0
sum_y2 = 0
for i in range(counts): #第一种
    pc = random.randint(0,2)
    cc = random.randint(0,2)
    if pc == cc:
        sum_y1 += 1
for j in range(counts): #第二种
    pc = random.randint(0,1)
    cc = random.randint(0,1)
    if pc == cc:
        sum_y2 += 1
print("第一种概率为{0:.2f} , 第二种概率为{1:.2f}".format(sum_y1/counts,sum_y2/counts))

你可能感兴趣的:(python基础)