Question: Write a program (called q2.py) to compute the average number of times you need to flip a fair coin before you have seen an

Write a program (called q2.py) to compute the average number of times you need to flip a fair coin before you

Write a program (called q2.py) to compute the average number of times you need to flip a fair coin before you have seen an odd number of heads in a run, followed by a tail. Let H and T represent heads and tails, respectively. Examples of valid runs include HT (ending with 1 head and 1 tail), HHTTTTHHHT (ending with 3 heads and 1 tail), and TTHHTTHHHHTHHHHHHHT (ending with 7 heads and 1 tail). Examples of invalid runs include T (no heads and 0 is an even number), H (must end with T), and HHT (a run of an even number of heads followed by a tail). Before you get started, make sure you understand what the question is asking and then sketch out some examples. Directions for writing your program. a) Your program must have the main() function, which is the main component of your program and the flip_coin() function, which returns either heads or tails to simulate a coin flip. b) Write a Python program to simulate a single trial of the experiment. The result of a single trial is the number of times you need to flip a coin before you have seen an odd number of heads in a run followed by a tail. c) Extend your Python program to run an experiment consisting of n trials. Let n = 10, what is the average number of times the coin was flipped for these 10 trials? d) Run the experiment for 10, 10, 10, 104, 105, and 106 trials. This computation may take a while. Check your output to make sure it makes sense. Do you find the results surprising?

Step by Step Solution

3.49 Rating (152 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Heres a Python program named q2py that follows the specified directions import random def flipcoin r... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!