Created
August 30, 2016 19:34
-
-
Save greenspray/a90d9ef1f4e49054de948ca487bab9f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Takes [1] argument - The population size (i.e number of times to flip the coin)