背景:今天我想我会开始一个建立扑克的小项目 模拟器。我设定的第一项任务是从洗牌中处理卡片 甲板,并检查各种数字生成的概率 反对公认价值观。我检查的第一个这样的概率是 单对概率 - 也就是说,生成(数字) 作为输入数量给出的一对的概率 发出的牌数和每手牌的牌数 是从一个单独的洗牌甲板处理的。卡片是从 甲板的顶部。下面我展示该程序的开头。 我首先测试了数值生成的单对概率 五张牌手。计算出的值就在里面 接受单对概率的百分之十 对于五张牌(但总是高约十分之一):https://en.wikipedia.org/wiki/Poker_probability
但是,当我测试数值生成的单对概率时 对于七张牌,我发现我离开了4%到5% 可接受的值(例如,典型的计算值= 0.47828;上述接受值= 0.438)。我进行了数十次的数值实验 百万手牌。计算出的单对概率为7 牌手稳定,并且与可接受的值保持4%至5%的关闭。目前尚不清楚 为什么会这样。
问题:为什么会这样?我怀疑我的代码没有采取 考虑到了什么,但我无法察觉到什么。 Python代码如下。 。
注意:问题31381901与此类似。但是在下面的代码中,重复计数的问题是通过将处理的手转换为集合来解决的,这将消除重复值,从而将集合的大小(在7张牌的情况下)从7减少到6。减少表示一对。如果存在三种类型,则该集合的大小将为5,因为通过集合转换将消除三种类型中的三种卡中的两种。
from random import shuffle
def make_deck():
'''Make a 52 card deck of cards. First symbol
is the value, second symbol is the suit. Concatenate
both symbols together.
Input: None
Output: List
'''
value = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
suit = ['C','H','S','D']
deck = [j+i for j in value for i in suit]
return deck
def shuffle_deck(deck, times_to_shuffle=7):
'''Shuffle a deck of cards produced by make_deck().
Default: 7 times.
Input: list, int
Output: list (modified in-place)
'''
for n in range(times_to_shuffle):
shuffle(deck)
def test_for_single_pair(hand, cards_per_hand):
'''Tests for presence of a single pair in
a dealt hand by converting the hand to a set.
The set representation of a hand with a single
pair will have one less member than the original
hand.
Input: list, int
Output: int
'''
hand_values_lst = [card[0] for card in hand]
hand_values_set = set(hand_values_lst)
set_size = len(hand_values_set)
if set_size == (cards_per_hand - 1):
return 1
else:
return 0
def deal_series_of_hands(num_hands,cards_per_hand):
'''Deals a series of hands of cards and tests
for single pairs in each hand. Creates a deck
of 52 cards, then begins dealing loop. Shuffles
deck thoroughly after each hand is dealt.
Captures a list of the dealt hands that conform
to the spec (i.e., that contain one pair each),
for later debugging purposes
Input: int, int
Output: int, int, list
'''
deck = make_deck()
single_pair_count = 0
hand_capture = []
for m in range(num_hands):
shuffle_deck(deck)
hand = deck[0:cards_per_hand] #first cards dealt from the deck
pair_count = test_for_single_pair(hand, cards_per_hand)
if pair_count == 1:
single_pair_count += pair_count
hand_capture.append(hand)
return (single_pair_count, num_hands, hand_capture)
cards_per_hand = 7 #User input parameter
num_hands = 50000 #user input parameter
single_pair_count, num_hands_dealt, hand_capture = deal_series_of_hands(num_hands, cards_per_hand)
single_pair_probability = single_pair_count/ num_hands_dealt
single_pair_str = 'Single pair probability (%d card deal; poker hands): '%(cards_per_hand)
print(single_pair_str, single_pair_probability)
答案 0 :(得分:3)
如果手牌包含一对,但也包含更高价值的单位,例如直线或同花,那么您的代码仍会将其视为一对,而概率文章则没有。