Skip to content

Instantly share code, notes, and snippets.

@miguelmartin75
Created May 12, 2014 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmartin75/3cd8fbc20709c0fdec16 to your computer and use it in GitHub Desktop.
Save miguelmartin75/3cd8fbc20709c0fdec16 to your computer and use it in GitHub Desktop.
Biased Coin
import random
HEADS = 0
TAILS = 1
n = input("How many samples do you wish to compute?: ")
p = random.random()
print "probability of getting heads is:", p
correctGuesses = 0
wrongGuesses = 0
for i in range(0, n):
choice = random.randint(HEADS, TAILS)
coinFlip = random.random()
# if we get a heads
if coinFlip < p:
# if we guessed heads
if choice == HEADS:
# then we guessed correctly
correctGuesses = correctGuesses + 1
else:
# otherwise, we guessed wrong
wrongGuesses = wrongGuesses + 1
# otherwise if we got a tails
else:
# if we guessed tails
if choice == TAILS:
# then we guessed correctly
correctGuesses = correctGuesses + 1
else:
# otherwise we got the guess wrong
wrongGuesses = wrongGuesses + 1
percentageCorrect = float(correctGuesses) / n
print "correct guesses:", correctGuesses, "(" + str(percentageCorrect) + ")"
print "wrong guesses:", wrongGuesses, "(" + str(1 - percentageCorrect) + ")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment