Language: Python
Write a program to simulate an experiment of tossing a fair coin 16 times and counting the number of heads. Repeat this experiment 10**5 times to obtain the number of heads for every 16 tosses; save the number of heads in a vector of size 10**5 (call it headCounts).
You should be able to do this in 1-3 lines of numpy code. (Use np.random.uniform1 to generate a 2d array of 10**5 x 16 random numbers between 0 and 1, then convert values less than 0.5 to True - or “head”, and count the number of heads for each simulated coin.)
Here is my code, but I am having difficulty understanding the question
import numpy as np
import scipy.stats as stats
import numpy.random as rn
m = 10**5; # number of coins
N = 16; # number of tosses each coin
sims = np.empty(N)
for j in range(N):
flips = np.random.randint(0, 2, m)
sims[j] = np.sum(flips)
print(sims[:])
import numpy as np
import scipy.stats as stats
import numpy.random as rn
m = 10**5; # number of coins
N = 16; # number of tosses each coin
simulations=rn.random(size=(m,N))
#each row represents one experiment, each column represents one
coin
headCounts=np.sum(simulations<.5,1)#sum all the places where
values is <.5, row wise
#headCounts now stores number of heads obtained in each of the 10^5
experiments
Get Answers For Free
Most questions answered within 1 hours.