X=[] #empty list X
Y=[] #empty list Y
Z=[] #empty list Z
size=200
for _ in range(size*n):
X.append(r.random()<p) #list of 200n random values (True or False) with probaility P(True)=p
Y.append(r.random()<q) #list of 200n random values (True or False) with probaility P(True)=q
X=[1 if _==True else 0 for _ in X] #list of 200n random Ber(p) random variables
Y=[1 if _==True else 0 for _ in Y] #list of 200n random Ber(q) random variables
for i in range(size): #create list Z of length 400n, n elements coming from X, n coming from Y alternating
for j in range(i*n, (i+1)*n):
Z.append(X[j])
for j in range(i*n, (i+1)*n):
Z.append(Y[j])
output=Z[0:4000] #keep only first 4000 elements of Z
with open('random_sample_hw1.csv', 'w', newline='') as f: #write Z into a file called random_sample.csv
writer = csv.writer(f)
writer.writerow(output)