In: Statistics and Probability
Suppose that your enemy has a set of 20 weighted coins that each has a probability of 0.6 of landing heads when flipped. Estimate the mean number of heads if the set was flipped over and over by simulating 250,000 flips of the set and computing the average. (In python)
Python-code
import numpy as np
import random
coinoutcomes = ("H","T")
iterations = 20
simulations = 250000
allflips = []
random.seed(1453)
# function to return the randon value
# on biased biased coin FLIP
# since the probability of heads = 0.6, we have 3 heads and 2 tails
in 5 items
def biasedcoin():
return random.choice(['H','T','H','H','T'])
headsfound = np.zeros((simulations))
coinflips = 0
for j in range(0, simulations):
temp = []
for i in range(0,iterations) :
temp.append(biasedcoin())
if temp[i] ==
"H":
headsfound[j] = headsfound[j] + 1
meanheads = np.mean(headsfound)
print("\nAverage heads found = ", meanheads)
Python-output
Average heads found = 11.99912