In: Computer Science
For this problem, complete the function stub to simulate the following random variable: Suppose we have a (possibly) unfair coin where the probability of Head is ? and we flip this coin ? times. Let X = "the number of heads showing."
Demonstrate your solution by generating 105 random variates for the parameters shown and displaying them using show_distribution(...).
STARTER CODE:
# First, create a function which simulates the coin, where
# you return 1 with probability p and 0 with probability 1-p.
N = 10
p = 0.25
num_trials = 10**5
def coinFlip(p):
return 0 # Just to get it to compile
def coinFlips(N,p):
return 0 # Just to get it to compile
print("Demonstration:")
seed(0)
I have provided the implementation for these 2 functions. Now, you can use show_distribution() method to show the distribution. SOURCE CODE: *Please follow the comments to better understand the code. **Please look at the Screenshot below and use this code to copy-paste. ***The code in the below screenshot is neatly indented for better understanding. from random import seed, random N = 10 p = 0.25 num_trials = 10 ** 5 # First, create a function which simulates the coin, where # you return 1 with probability p and 0 with probability 1-p. def coinFlip(p): # get random number and return 1 if it is less than p if random() < p: return 1 else: # 0 if it's greater return 0 def coinFlips(N, p): # take count as 0 head_count = 0 # flip coins for N times for i in range(N): # flip coin toss = coinFlip(p) # if it's 1, then we have got a head if toss == 1: head_count += 1 # return the count return head_count # TEST print("Demonstration:") seed(0) # num_heads = coinFlips(N, p) # print('Number of heads, X=', num_heads)
=========