Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Suppose I have a shuffled pack of cards, out of which 4 red cards have been lost
- or removed. So now I have 22 red cards and 26 black cards in random order. I am
- dealing cards in pairs. If a pair has 2 red cards, they are dealt to Mr. Red, if
- the pair has 2 black cards, they're dealt to Mr. Black, and if the pair contains
- 1 red and 1 black card, they are dealt to Mr. Mismatch.
- After all the cards have been dealt in pairs, whoever has the most cards wins.
- Now, Mr. Mismatch will almost always win because there are more ways to get a
- mismatched pair (card-1 is red and card-2 is black or vice versa) than a pair of
- reds or blacks. Also, since there are 4 fewer red cards, Mr. Red will never win.
- Now what I'm trying to do is run a simulation about 1_000,000 times to see what
- percentage of games are won by the three players.
- """
- from random import shuffle
- cards = 22*["red"] + 26*["black"]
- red_won = 0
- black_won = 0
- mismatch_won = 0
- trials = 1_000_000
- for _ in range (trials):
- r = 0 # red score in each trial
- b = 0 # black score in each trial
- m = 0 # mismatch score in each trial
- shuffle (cards) # shuffle the cards with random.shuffle()
- for i in range (0, 47, 2): # there are 48 cards
- if cards[i] == "red" and cards[i+1] == "red":
- r += 2
- elif cards[i] == "black" and cards[i+1] == "black":
- b += 2
- elif ((cards[i] == "red" and cards[i+1] == "black")
- or (cards[i] == "black" and cards[i+1] == "red")):
- m += 2
- if (r > b) and (r > m): # red will never win
- red_won += 1
- elif (b > r) and (b > m):
- black_won += 1
- elif m > b and m > r:
- mismatch_won += 1
- print(f"Mr. Red won = {red_won} times = {red_won*100/trials : .2f}%")
- print(f"Mr. Black won = {black_won} times = {black_won*100/trials : .2f}%")
- print(f"Mr. Mismatch won = {mismatch_won} times = {mismatch_won*100/trials : .2f}%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement