Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

simulate three coin tosses and tabulate the results. Create this program in Pyth

ID: 3880896 • Letter: S

Question

simulate three coin tosses and tabulate the results. Create this program in Python and run it for 10,000 trials, printing out the fraction of trials in which there are 0, 1, 2, and 3 heads. Are these fractions what they should be? it should be similar to the matlab program bellow:

N = 10000; % Number of simulations

U = rand(3,N); % a 3-by-N matrix of random numbers from [0,1]

Y = (U < 0.5); % Y=1 (heads) if U < 0.5, otherwise Y=0 (tails)

X = sum(Y); % Sums across columns. X = number of heads

hist(X); % Histogram of X

Explanation / Answer

Python code:

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
N=10000 #No of trials
trials = np.random.random((3,N)) # generating 3XN array of random numbers b/w 0,1
Y= trials < 0.5 # Y=1 (heads) if trials < 0.5, otherwise Y=0 (tails)
X = sum(Y) #Sums across columns X = number of heads(0/1/2/3) as there are total 3 rows(coin tosses)
plt.hist(X) #plotting histogram