春节与孩子抽王八 — 优雅的Python
抽王八,一种很简单的纸牌游戏:准备一副牌,随便抽出一张牌当王八,然后把剩下的尽量平分每个人拿一份,把对子挑出去后轮流抽对方的牌,凑成对就可以扔出去,最后谁单独剩一张与王八一样的牌就输了。
Drawing the "tortoise", a very simple poker card game: First prepare a deck of cards, randomly draw one card as the "tortoise", then divide the remaining cards evenly among the players as much as possible. After setting aside all pairs, players take turns drawing cards from next player. When a pair is formed, it can be discarded. The player who is left with a single card matching the "tortoise" at the end loses.
说实话挺无聊的。但是为了让孩子少看点电子产品,我拖着孩子玩了好几个晚上。有时候我快赢了也要让让她,好让她有玩下去的动力。^_^。
To be honest, it's quite boring. But in order to protect my child's eye from electrical products, I spent several evenings playing this game with her. Sometimes, even when I was about to win, I would deliberately let her win to keep her motivated to continue playing.
当然本文重点不是讲这么简单的游戏规则及如何与孩子打成一片的。玩了很多局后我发现一规律,开始把对子挑出去后手里剩牌的张数经常是6、7、8,于是我写了个简单的Python程序模拟多次并统计了结果。
Of course, the focus of this article is not to explain such simple game rules or how to hang with children. After playing many rounds, I noticed a pattern: the number of cards left in hand after setting aside the pairs is often 6, 7, or 8. So, I wrote a simple Python program to simulate this multiple times and analyzed the results.
程序如下(Below is the Python program):
import random
from collections import Counter
# There are 13 ranks (A to K) with 4 cards each
deck = [rank for rank in range(1, 14) for _ in range(4)]
deck.append(14) # joker
# Remove one joker, so we have 53 cards left
def simulate_draw_and_remove():
# Draw 26 cards
drawn_cards = random.sample(deck, 26)
# Count the occurrences of each rank
card_counts = Counter(drawn_cards)
# Count the number of ranks that have an odd count
remaining_cards = sum(1 for count in card_counts.values() if count % 2 != 0)
return remaining_cards
# Simulate multiple times and summarize results
results = Counter()
# Run the simulation 100,000 times to get a good distribution
for _ in range(100000):
remaining = simulate_draw_and_remove()
results[remaining] += 1
# Display the summary of results
for remaining, count in sorted(results.items()):
print(f"{remaining} cards remained: {count} occurrences")
这里我假设王八总是王。模拟10万次,只统计一方(摸26张)手里剩的牌数:
Here I always fix joker as "tortoise. Simulate it up to 100,000 rounds, then summarize the amount of remained cards in one player:
0 cards remained: 7 occurrences
2 cards remained: 1129 occurrences
4 cards remained: 12312 occurrences
6 cards remained: 36768 occurrences
8 cards remained: 36453 occurrences
10 cards remained: 12159 occurrences
12 cards remained: 1152 occurrences
14 cards remained: 20 occurrences
正如猜想,剩6~8 张的可能性最大。
As suspected, the possibility of having 6 to 8 cards left is the highest.
作者:深山老宅