首页 > 解决方案 > 为什么我的两个样本没有共同的 0 个数字?

问题描述

我编写了一个程序,可以对 0-9 之间的两个列表进行采样。然后我查看它们有多少个相同的数字,并将其发送到相应的计数器。我已经循环了这 1000000 个,但样本从来没有共同的 0 个数字。我的代码有问题,还是我非常不走运

for _ in range(1000000):

    House_deal = random.sample(range(9), k=5)
    Player_deal = random.sample(range(9), k=5)

    hc = Counter(House_deal)
    pc = Counter(Player_deal)
    common = hc.keys() & pc.keys() #get the intersection of both keys
    counts = 0

    for cel in common:
        counts += min(hc[cel], pc[cel])
    if counts == 0:
        common_0 += 1
    elif counts == 1:
        common_1 += 1
    elif counts == 2:
        common_2 += 1
    elif counts == 3:
        common_3 += 1
    elif counts == 4:
        common_4 += 1
    elif counts == 5:
        common_5 += 1

标签: pythonrandom

解决方案


我相信你想要做的是在 1000000 次的试验中确定最常见hcpc。这样的事情应该做。您的主要问题是您没有在循环之外跟踪这些数字。我不确定count是为了什么?

from random import sample
from collections import Counter

common = Counter()

for _ in range(1000000):
  House_deal = sample(range(9), k=5)
  Player_deal = sample(range(9), k=5)

  common.update(set(House_deal).intersection(Player_deal))

推荐阅读