# This imports the pylab library, which has many useful functions for things # like generating random events. from pylab import * # Coin objects take a parameter p, which represents the bias of the coin. The # flip method returns True with probability p and False with probability (1 - p) # The total_flips field logs how many times the coin was flipped. class Coin: def __init__(self, p): self.p = p self.total_flips = 0 def flip(self): self.total_flips += 1 return (rand() < self.p) ########################################################### # Everything below this just an example illustrating the use of the coin object, # you should remove it from the code you submit ########################################################### # Here are some examples illustrating its use: # Generates a coin where p=.33 coin1 = Coin(.33) print(coin1.flip()) # We can flip many times at once using Python comprehensions: many_flips = [coin1.flip() for i in range(0, 100)] print(many_flips) # Print we can check how many times coin1 has been flipped by accessing the # total_flips field. print(coin1.total_flips) # You can always reset this count to 0 when you begin a new experiment, if you like coin1.total_flips = 0 print(coin1.flip()) print(coin1.total_flips) # Here is a function that takes a coin object as an argument and returns two flips: def test(c): return (c.flip(), c.flip()) # Let's try it with a new coin coin2 = Coin(.5) print(test(coin2)) print(coin2.total_flips)