处理七张扑克牌时计算的单对概率的异常概率结果
Anomalous probability result for computed single pair probability when dealing seven card poker hands
背景:今天我想我会开始一个小项目来制作扑克牌
模拟器。我设定的第一个任务是发牌
甲板,并检查各种数字生成的概率
反对公认的价值观。我检查的第一个这样的概率是
单对概率——即生成(数值)
被处理成一对的概率,给定作为输入的数字
发出的牌数和发出的手数,每手牌
从一个单独的洗牌甲板处理。卡片是从
甲板的顶部。下面我展示了该程序的开头。
我首先测试了数字生成的单对概率
五张牌手。计算值在
接受的单对概率的十分之一
对于五张手牌(但总是高大约百分之一):https://en.wikipedia.org/wiki/Poker_probability
但是,当我测试数值生成的单对概率时
对于七张牌,我发现我的牌差了 4% 到 5%
接受值(例如,典型的计算值 = 0.47828;按照上述接受值 = 0.438)。我已经 运行 进行了多达十次的数值实验
百万手交易。七的计算单对概率
手牌稳定,与可接受值保持 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)
如果这手牌包含一对,但也包含更高价值的单位,例如顺子或同花,您的代码仍将其视为一对,而概率文章则不会。
背景:今天我想我会开始一个小项目来制作扑克牌 模拟器。我设定的第一个任务是发牌 甲板,并检查各种数字生成的概率 反对公认的价值观。我检查的第一个这样的概率是 单对概率——即生成(数值) 被处理成一对的概率,给定作为输入的数字 发出的牌数和发出的手数,每手牌 从一个单独的洗牌甲板处理。卡片是从 甲板的顶部。下面我展示了该程序的开头。 我首先测试了数字生成的单对概率 五张牌手。计算值在 接受的单对概率的十分之一 对于五张手牌(但总是高大约百分之一):https://en.wikipedia.org/wiki/Poker_probability
但是,当我测试数值生成的单对概率时 对于七张牌,我发现我的牌差了 4% 到 5% 接受值(例如,典型的计算值 = 0.47828;按照上述接受值 = 0.438)。我已经 运行 进行了多达十次的数值实验 百万手交易。七的计算单对概率 手牌稳定,与可接受值保持 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)
如果这手牌包含一对,但也包含更高价值的单位,例如顺子或同花,您的代码仍将其视为一对,而概率文章则不会。