一步步解析Python斗牛游戲的概率
過年回家,都會(huì)約上親朋好友聚聚會(huì),會(huì)上經(jīng)常會(huì)打麻將,斗地主,斗牛。在這些游戲中,斗牛是最受歡迎的,因?yàn)榭梢院芏嗳艘黄鹜?,而且沒有技術(shù)含量,都是看運(yùn)氣(專業(yè)術(shù)語(yǔ)是概率)。
斗牛的玩法是:
- 1、把牌中的JQK都拿出來
- 2、每個(gè)人發(fā)5張牌
- 3、如果5張牌中任意三張加在一起是10的 倍數(shù),就是有牛。剩下兩張牌的和的10的余數(shù)就是牛數(shù)。
牌的大?。?/p>
4條 > 3條 > 牛十 > 牛九 > …… > 牛一 >沒有牛
而這些牌出現(xiàn)的概率是有多少呢?
由于只有四十張牌,所以采用了既簡(jiǎn)單,又有效率的方法枚舉來計(jì)算。
計(jì)算的結(jié)果:
- 所有牌的組合數(shù):658008
- 出現(xiàn)四條的組合數(shù):360,概率 :0.05%
- 出現(xiàn)三條的組合數(shù):25200,概率 :3.83%
- 出現(xiàn)牛十的組合數(shù):42432,概率 :6.45%
- 出現(xiàn)牛九或牛八的組合數(shù):87296,概率 :13.27%
- 出現(xiàn)牛一到牛七的組合數(shù):306112,概率 :46.52%
- 出現(xiàn)沒有牛的組合數(shù):196608,概率 :29.88%
所以有七成的概率是有牛或以上的,所以如果你經(jīng)常遇到?jīng)]有牛,說明你的運(yùn)氣非常差或者本來是有牛的,但是你沒有找出來。
Python源代碼:
# encoding=utf-8
__author__ = 'kevinlu1010@qq.com'
import os
import cPickle
from copy import copy
from collections import Counter
import itertools
'''
計(jì)算斗牛游戲的概率
'''
class Poker():
'''
一張牌
'''
def __init__(self, num, type):
self.num = num # 牌數(shù)
self.type = type # 花色
class GamePoker():
'''
一手牌,即5張Poker
'''
COMMON_NIU = 1 # 普通的牛,即牛一-牛七
NO_NIU = 0 # 沒有牛
EIGHT_NINE_NIU = 2 # 牛九或牛八
TEN_NIU = 3 # 牛十
THREE_SAME = 4 # 三條
FOUR_SAME = 5 # 四條
def __init__(self, pokers):
assert len(pokers) == 5
self.pokers = pokers
self.num_pokers = [p.num for p in self.pokers]
# self.weight = None # 牌的權(quán)重,權(quán)重大的牌勝
# self.money_weight = None # 如果該牌贏,贏錢的權(quán)重
self.result = self.sumary()
def is_niu(self):
'''
是否有牛
:return:
'''
# if self.is_three_same():
# return 0
for three in itertools.combinations(self.num_pokers, 3):
if sum(three) % 10 == 0:
left = copy(self.num_pokers)
for item in three:
left.remove(item)
point = sum(left) % 10
return 10 if point == 0 else point
return 0
def is_three_same(self):
'''
是否3條
:return:
'''
# if self.is_four_same():
# return 0
count = Counter([p.num for p in self.pokers])
for num in count:
if count[num] == 3:
return num
return 0
def is_four_same(self):
'''
是否4條
:return:
'''
count = Counter([p.num for p in self.pokers])
for num in count:
if count[num] == 4:
return num
return 0
def sumary(self):
'''
計(jì)算牌
'''
if self.is_four_same():
return GamePoker.FOUR_SAME
if self.is_three_same():
return GamePoker.THREE_SAME
niu_point = self.is_niu()
if niu_point in (8, 9):
return GamePoker.EIGHT_NINE_NIU
elif niu_point == 10:
return GamePoker.TEN_NIU
elif niu_point > 0:
return GamePoker.COMMON_NIU
else:
return GamePoker.NO_NIU
def get_all_pokers():
'''
生成所有的Poker,共四十個(gè)
:return:
'''
pokers = []
for i in range(1, 11):
for j in ('A', 'B', 'C', 'D'):
pokers.append(Poker(i, j))
return pokers
def get_all_game_poker(is_new=0):
'''
生成所有g(shù)ame_poker
:param pokers:
:return:
'''
pokers = get_all_pokers()
game_pokers = []
if not is_new and os.path.exists('game_pokers'):
with open('game_pokers', 'r') as f:
return cPickle.loads(f.read())
for pokers in itertools.combinations(pokers, 5): # 5代表五張牌
game_pokers.append(GamePoker(pokers))
with open('game_pokers', 'w') as f:
f.write(cPickle.dumps(game_pokers))
return game_pokers
def print_rate(game_pokers):
total_num = float(len(game_pokers))
four_num = len([game_poker for game_poker in game_pokers if game_poker.result == GamePoker.FOUR_SAME])
three_num = len([game_poker for game_poker in game_pokers if game_poker.result == GamePoker.THREE_SAME])
ten_num = len([game_poker for game_poker in game_pokers if game_poker.result == GamePoker.TEN_NIU])
eight_nine_num = len([game_poker for game_poker in game_pokers if game_poker.result == GamePoker.EIGHT_NINE_NIU])
common_num = len([game_poker for game_poker in game_pokers if game_poker.result == GamePoker.COMMON_NIU])
no_num = len([game_poker for game_poker in game_pokers if game_poker.result == GamePoker.NO_NIU])
print '所有牌的組合數(shù):%d' % total_num
print '出現(xiàn)四條的組合數(shù):%d,概率 :%.2f%%' % (four_num, four_num * 100 / total_num)
print '出現(xiàn)三條的組合數(shù):%d,概率 :%.2f%%' % (three_num, three_num * 100 / total_num)
print '出現(xiàn)牛十的組合數(shù):%d,概率 :%.2f%%' % (ten_num, ten_num * 100 / total_num)
print '出現(xiàn)牛九或牛八的組合數(shù):%d,概率 :%.2f%%' % (eight_nine_num, eight_nine_num * 100 / total_num)
print '出現(xiàn)牛一到牛七的組合數(shù):%d,概率 :%.2f%%' % (common_num, common_num * 100 / total_num)
print '出現(xiàn)沒有牛的組合數(shù):%d,概率 :%.2f%%' % (no_num, no_num * 100 / total_num)
def main():
game_pokers = get_all_game_poker() # 658008種
print_rate(game_pokers)
main()
以上就是Python計(jì)算斗牛游戲的概率相關(guān)內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Python使用random和tertools模塊解一些經(jīng)典概率問題
- python概率計(jì)算器實(shí)例分析
- Python對(duì)小數(shù)進(jìn)行除法運(yùn)算的正確方法示例
- python的三目運(yùn)算符和not in運(yùn)算符使用示例
- 總結(jié)Python中邏輯運(yùn)算符的使用
- python 運(yùn)算符 供重載參考
- 解析Python中的二進(jìn)制位運(yùn)算符
- 實(shí)例說明Python中比較運(yùn)算符的使用
- 跟老齊學(xué)Python之眼花繚亂的運(yùn)算符
- python三元運(yùn)算符實(shí)現(xiàn)方法
- Python實(shí)現(xiàn)的概率分布運(yùn)算操作示例
相關(guān)文章
python新式類和經(jīng)典類的區(qū)別實(shí)例分析
這篇文章主要介紹了python新式類和經(jīng)典類的區(qū)別,結(jié)合實(shí)例形式分析了python新式類和經(jīng)典類的功能、區(qū)別與使用方法,需要的朋友可以參考下2020-03-03
Python中實(shí)現(xiàn)定時(shí)任務(wù)常見的幾種方式
在Python中,實(shí)現(xiàn)定時(shí)任務(wù)是一個(gè)常見的需求,無論是在自動(dòng)化腳本、數(shù)據(jù)處理、系統(tǒng)監(jiān)控還是其他許多應(yīng)用場(chǎng)景中,Python提供了多種方法來實(shí)現(xiàn)定時(shí)任務(wù),包括使用標(biāo)準(zhǔn)庫(kù)、第三方庫(kù)以及系統(tǒng)級(jí)別的工具,本文將詳細(xì)介紹幾種常見的Python定時(shí)任務(wù)實(shí)現(xiàn)方式2024-08-08
python生成13位或16位時(shí)間戳以及反向解析時(shí)間戳的實(shí)例
這篇文章主要介紹了python生成13位或16位時(shí)間戳以及反向解析時(shí)間戳的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
pandas數(shù)據(jù)類型之Series的具體使用
本文主要介紹了pandas數(shù)據(jù)類型之Series的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
用python實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了用python實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07

