Analyzing the probability of a Python bullfighting game step by step, and the probability of a python bullfighting game

Source: Internet
Author: User

Analyzing the probability of a Python bullfighting game step by step, and the probability of a python bullfighting game

When I go home from the New Year, I will gather together with friends and family. At the meeting, I will often play mahjong, fight landlords, and fight for bullfights. In these games, bullfighting is the most popular because it can be played by many people without technical skills. It is luck (the term is probability ).
The fighting method is:

  • 1. Take out all the JQK cards.
  • 2. Send 5 cards to each user
  • 3. If any three of the five cards are combined with a factor of 10, there will be a bull. The 10 Remainder of the remaining two cards is ox.

Card size:

4> 3> 10> 9> ...... > Niu Yi> NO Niu

What is the probability of these cards?

Because there are only 40 cards, we use simple and efficient enumeration methods for computation.
Calculation Result:

  • Total number of cards: 658008
  • Number of the four combinations: 360, probability: 0.05%
  • Three Combinations: 25200, probability: 3.83%
  • Number of combinations with the ox ten: 42432, probability: 6.45%
  • Number of combinations of ox 9 or ox 8: 87296, probability: 13.27%
  • The number of combinations from OX 1 to Ox 7: 306112, probability: 46.52%
  • Number of groups without cattle: 196608, probability: 29.88%

So there is a probability that there will be more than a cow. If you often encounter a lack of a cow, it means that your luck is very poor or you have a cow, but you didn't find it.

Python source code:

# Encoding = UTF-8 _ author _ = 'kevinlu1010 @ qq.com 'import osimport cPicklefrom copy import copyfrom collections import Counterimport itertools ''' calculate the probability of a bullfighting game ''' class Poker (): '''card ''' def _ init _ (self, num, type): self. num = num # number of cards self. type = type # color class GamePoker (): ''' one-hand card, that is, five Poker ''' COMMON_NIU = 1 # normal ox, that is, Niu Yi-niu Qi NO_NIU = 0 # No Niu EIGHT_NINE_NIU = 2 # Niu JIU or Niu Ba TEN_NIU = 3 # Niu Shi THREE_SAME = 4 #3 FOUR_SAME = 5 #4 def _ init _ _ (self, pokers): assert len (pokers) = 5 self. pokers = pokers self. num_pokers = [p. num for p in self. pokers] # self. weight = None # card weight, winning card with a higher weight # self. money_weight = None # self. result = self. sumary () def is_niu (self): ''' is there a cow: 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): ''' whether 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): ''' whether 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): ''' calculate the card ''' 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_NIUdef get_all_pokers (): ''' to generate all Poker, 40 in total: return: ''' pokers = [] for I in range (1, 11 ): for j in ('A', 'B', 'C', 'D'): pokers. append (Poker (I, j) return pokersdef get_all_game_poker (is_new = 0): ''' generate all game_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 represents the five cards game_pokers.append (GamePoker (pokers) with open ('game _ pokers', 'w') as f: f. write (cPickle. dumps (rows) return response print_rate (game_pokers): total_num = float (len (rows) four_num = len ([game_poker for game_poker in game_pokers if rows = 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 'Total number of cards Composite: % d' % total_num print 'number of four composite: % d, probability: %. 2f % '% (four_num, four_num * 100/total_num) print' three combinations: % d, probability: %. 2f % '% (three_num, three_num * 100/total_num) print': % d, probability: %. 2f % '% (ten_num, ten_num * 100/total_num) print' indicates the combination number of ox 9 or ox 8: % d, probability: %. 2f % '% (eight_nine_num, eight_nine_num * 100/total_num) print' indicates the number of combinations from OX 1 to Ox 7: % d, probability: %. 2f % '% (common_num, common_num * 100/total_num) print': % d, probability: %. 2f % '% (no_num, no_num * 100/total_num) def main (): game_pokers = get_all_game_poker () #658008 print_rate (game_pokers) main ()

The above content is related to the probability of Python computing bullfighting games, and we hope to help you learn it.

Articles you may be interested in:
  • The small ball developed by python is fully elastic and collision game code
  • Python basic tutorial-stone scissors cloth game example
  • Python3.3 use tkinter to develop a guess digital game example
  • Example of a snake game written in Python
  • Python-based mine clearance game instance code
  • Learn Python with old Qi.
  • Python probability calculator instance analysis
  • Python beginners implement 2048 games
  • Compile a simple Russian square game tutorial in Python
  • Python-implemented simple text game instances

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.