Advertisement
Pnerd6

Aunt Mary

May 4th, 2022 (edited)
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. """
  2. Suppose I have a shuffled pack of cards, out of which 4 red cards have been lost
  3. or removed. So now I have 22 red cards and 26 black cards in random order. I am
  4. dealing cards in pairs. If a pair has 2 red cards, they are dealt to Mr. Red, if
  5. the pair has 2 black cards, they're dealt to Mr. Black, and if the pair contains
  6. 1 red and 1 black card, they are dealt to Mr. Mismatch.
  7.  
  8. After all the cards have been dealt in pairs, whoever has the most cards wins.
  9. Now, Mr. Mismatch will almost always win because there are more ways to get a
  10. mismatched pair (card-1 is red and card-2 is black or vice versa) than a pair of
  11. reds or blacks. Also, since there are 4 fewer red cards, Mr. Red will never win.
  12.  
  13. Now what I'm trying to do is run a simulation about 1_000,000 times to see what
  14. percentage of games are won by the three players.
  15.  
  16. """
  17.  
  18. from random import shuffle
  19.  
  20. cards = 22*["red"] + 26*["black"]
  21. red_won = 0
  22. black_won = 0
  23. mismatch_won = 0
  24. trials = 1_000_000
  25.  
  26. for _ in range (trials):
  27.     r = 0  # red score in each trial
  28.     b = 0  # black score in each trial
  29.     m = 0  # mismatch score in each trial
  30.  
  31.     shuffle (cards)  # shuffle the cards with random.shuffle()
  32.     for i in range (0, 47, 2):  # there are 48 cards
  33.         if cards[i] == "red" and cards[i+1] == "red":
  34.             r += 2
  35.         elif cards[i] == "black" and cards[i+1] == "black":
  36.             b += 2
  37.         elif ((cards[i] == "red" and cards[i+1] == "black")
  38.             or (cards[i] == "black" and cards[i+1] == "red")):
  39.             m += 2
  40.    
  41.     if (r > b) and (r > m):     # red will never win
  42.         red_won += 1
  43.     elif (b > r) and (b > m):  
  44.         black_won += 1
  45.     elif m > b and m > r:
  46.         mismatch_won += 1
  47.    
  48. print(f"Mr. Red won = {red_won} times = {red_won*100/trials : .2f}%")
  49. print(f"Mr. Black won = {black_won} times = {black_won*100/trials : .2f}%")
  50. print(f"Mr. Mismatch won = {mismatch_won} times = {mismatch_won*100/trials : .2f}%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement