Skip to content

Instantly share code, notes, and snippets.

@RFullum
Created June 26, 2020 10:53
Show Gist options
  • Save RFullum/b7e876aa7149d4dfe5d0fa9509abce70 to your computer and use it in GitHub Desktop.
Save RFullum/b7e876aa7149d4dfe5d0fa9509abce70 to your computer and use it in GitHub Desktop.
Beating 50/50 odds in coin flip prediction vs opponent for a 3 flip series
'''
The Coin Flip Prediction Game:
The player picks a sequence of three coin flip outcomes.
The computer picks the sequence most likely to beat the player.
A coin is flipped until the sequence matches either the player
or the computer's sequence and the winner is declared.
The basic steps:
1- User picks 3 outcomes
2- Computer picks better outcomes
3- Flips coins until winner
4- Tally user vs computer wins
In a seemingly random sequence of 50/50 outcomes, the predictability of a
string of those outcomes would also seem to be random. Neither player should
have an advantage. However, using the algorithm employed here, the second player
bases their sequence on the first player's sequence, and greatly increases
the likelyhood of winning.
'''
import random
#get user guesses for three coin flips
def userSequence():
userSequence = []
userSequence.append(input("Enter h (Heads) or t (tails) for your first guess: "))
userSequence.append(input("Enter h (Heads) or t (tails) for your second guess: "))
userSequence.append(input("Enter h (Heads) or t (tails) for your third guess: "))
return userSequence
#computer choses sequence that will most likely beat user sequence
#algorithm:
# userSequence[a, b, c]
# computerSequence[-b, a, b]
def computerSequence(userSequence):
computerSequence = [userSequence[1], userSequence[0], userSequence[1]]
if computerSequence[0] == 'h':
computerSequence[0] = 't'
elif computerSequence[0] == 't':
computerSequence[0] = 'h'
return computerSequence
#randomly returns heads or tails
def coinFlip():
outcomes = ['h', 't']
return random.choice(outcomes)
#sets user and computer sequences
userSequence = userSequence()
computerSequence = computerSequence(userSequence)
print("User Sequence is ", userSequence)
print("Computer Sequence is ", computerSequence)
#initialize list of flips
flipSequence = []
#first three flips (no way to win before three flips)
for i in range(3):
flipSequence.append(coinFlip())
print("Flip ", i+1 , " is ", flipSequence[i])
print("The sequence is: ", flipSequence)
#last three flips in the sequence
flipTriplet = flipSequence[-3::]
#flips coins and adds outcome to end of list until win condition met
while flipTriplet != userSequence and flipTriplet != computerSequence:
flipSequence.append(coinFlip())
flipTriplet = flipSequence[-3::]
print("Flip ", len(flipSequence) , " is ", flipSequence[i])
print("The full sequence is: ", flipSequence)
print("The current triplet sequence is ", flipTriplet)
#winner:
if flipTriplet == userSequence:
print("The user's sequence ", userSequence, " matches the flipped sequence ", flipTriplet)
print("You Win!")
elif flipTriplet == computerSequence:
print("The computer's sequence ", computerSequence, " matches the flipped sequence ", flipTriplet)
print("You lose! You've doomed humanity!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment