In [1]:
import numpy as np
import random as r
import csv
In [2]:
p=r.random()/2        #random float in [0, 0.5)
q=(r.random()+1)/2    #random float in [0.5, 1)
n=r.randint(10,20)    #random integer in [10, 20]
In [3]:
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)