Question
Q4. a. Write program that simulates an unbiased coin flip. Your program should print true if the coin flip results in a head and false
Q4. a. Write program that simulates an unbiased coin flip. Your program should print true if the coin flip results in a head and false if the coin flip results in a tail. The library we will be importing is the random library allowing the programmer to access tools to generate pseudo-random number and perform operations that use these tools. See https://docs.python.org/3.5/library/random.html. [5 marks] --------------------------------------------------------------------------------------------------------------------------------------------------- b. Now consider a biased coin. Write program that takes a value p, with range between 0 and 1, as input from the user and tests a number of coin flips where p is the probability of the flip resulting in a result of heads. [5 marks] Example What kind of bias does the coins have? 0.5 Coin flip 1 has a value of heads: True Coin flip 2 has a value of heads: False Coin flip 3 has a value of heads: True --------------------------------------------------------------------------------------------------------------------------------------------------- c. Write program that takes a number n as input from the user and simulates n coin flips printing the results each time. [10 marks] Example: How times would you like to flip the coin? 5 The coin came up heads. The coin came up tails. The coin came up tails. The coin came up heads. The coin came up heads. --------------------------------------------------------------------------------------------------------------------------------------------------- d. Modify the program from task (c) to store the number of heads and tails in variables. Once you have generated the variables, print the total number of heads and the total number of tails. Calculate the ratio of heads to the total coin flips.1 Is this ratio what you would expect, what happens to this ratio as you change the probability of the coin coming up heads? What happens as the number of coins being flipped becomes large? [15 marks] --------------------------------------------------------------------------------------------------------------------------------------------------- e. Using the documentation from the random library, use `random.randrange(a)' to extend your program from part 2 so that it uses 3 sided coins. [15 marks] ================================================================
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started