#----------------------------------------------------------------------- # LoadedCoin.py # # Simulates n flips of a loaded coin. Also demonstrates the interpretation # of probability as the 'relative frequency' of an event. In a sequence of # identical experiements, the relative frequency of an event will approximate # probability of that event. # # probability of head ~ (number of heads)/(number of flips) # # The accuracy of the approximation improves as the number of experiments # increases. # #----------------------------------------------------------------------- from random import random n = 1_000_000 # number of flips to perform p = 0.63257 # probability of Head results = [] # list of outcomes # perform n flips for i in range(n): # perform one flip x = random() # get a random number in [0.0, 1.0) outcome = "H" if x