The problem is as follows:
Simulate flipping three fair coins and counting the number X of heads.
- Use your simulation to estimate P(X = 1) and EX. Compare the estimates with the true values, derived from theoretical computations.
- Modify the above to allow for a biased coin where P(heads)=3/4.
I have been able to simulate an unbiased coin as follows:
import random
SIMULATION_COUNT = 9999999
coin_surface_dictionary = {'H':0.5, 'T': 0.5}
def get_coin_surface():
return random.choice(['H', 'T'])
def get_three_coin_surface():
list_vector = []
list_vector.append(get_coin_surface())
list_vector.append(get_coin_surface())
list_vector.append(get_coin_surface())
return list_vector
if __name__ == "__main__":
one_head_count_int = 0
for ch in range(1, SIMULATION_COUNT):
coin_surface_vector = get_three_coin_surface()
head_count_int = coin_surface_vector.count("H")
if head_count_int == 1:
one_head_count_int = one_head_count_int + 1
# END if
# END for loop
probability = one_head_count_int / SIMULATION_COUNT
print(probability)
How can I simulate a biased coin by minimally modifying this source code?