#Simulate n tosses of a coin and return #the number of heads from random import random def cointoss_count(n): count=0 for x in range(0,n): if random()<0.5: count=count+1 #note that we could also eliminat the if statement #and simply write count=count+(random()<0.5) return count #now toss n coins and m times and count #how many times the number of heads #deviates by no more than p percent #from the expected mean of n/2 def deviation(n,m,p): count=0 for trial in range(0,m): if abs(cointoss_count(n)-n/2)<=p*n/100: count=count+1 return(count)