Skip to content

Instantly share code, notes, and snippets.

@greenspray
Created August 30, 2016 19:34
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 greenspray/a90d9ef1f4e49054de948ca487bab9f2 to your computer and use it in GitHub Desktop.
Save greenspray/a90d9ef1f4e49054de948ca487bab9f2 to your computer and use it in GitHub Desktop.
import random
import sys
try:
population_size = int(sys.argv[1])
except:
e = sys.exc_info()[0]
print "Please enter the population sample size i.e how many times to filp the coin"
sys.exit()
random.seed() #seeding means to provide a random starting point for the programme from the sysclock
num_head = num_tail = 0 #counters for number of heads and tails
sample_holder = []
for __ in xrange(population_size):
message = ""
data = random.randint(0,1)
if data == 1:
message = "H"
num_head = num_head + 1
elif data == 0:
message = "T"
num_tail = num_tail + 1
sample_holder.append(message)
print sample_holder
print "Number of Heads : " + str(num_head)
print "Number of Tails : " + str(num_tail)
print "P(H) : " + str(float(num_head)/population_size)
print "P(T) : " + str(float(num_tail)/population_size)
@greenspray
Copy link
Author

Takes [1] argument - The population size (i.e number of times to flip the coin)

  1. Creates and populates heads and tails in a list
  2. Prints the number of occurrence of Heads and tails
  3. Calculates the probability of occurrence of Heads and tails in the population sample i.e P(H) and P(T)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment