In: Computer Science
(+30) Write a python program per the following specifications:
At this point you should have a total of 50000 observations
NOTE: sd should be > 100
‘Yes’ if it is within the range calculated in 5c
‘No’ if it is not
my work:
import random import math myArray=[] a=0 b=1 size= 50 j=0 n=1000 #populate array with size=50 random integers in range 0-1 and repeat for 1000 times while(j<n): for k in range(size): randNum=random.randint(a,b) myArray.insert(k,randNum) j=j+1 #display array size print("size of my array is ",len(myArray)) #print myArray 50 values per line k=0 sub=[] while(k<len(myArray)): sub=myArray[k:k+50] print("...",k,sub) k=k+50 #count no. of ones and zeroes zeros=0 ones=0 i=0 while(i<len(myArray)): if(myArray[i]==1): ones=ones+1 else: zeros=zeros+1 i=i+1 print("number of 1 is ",ones) print("number of 0 is ",zeros) #calculate mean and SD of binomial distribution #probability of occurence of 1 is p = 1/2 and occurence of 0 is q =1/2 #mean of Binomial distribution = np #SD(standard deviation) of Binomial distribution = (npq)^1/2 #here n=50000 or length of array n=len(myArray) p=0.5 q=0.5 mean= n*p #expected mean of no of ones sd = math.sqrt(n*p*q) print("===============================") print("mean of 1s in My Array: ",mean," sd(standard deviation): ",sd) Lrange=int(round(mean-sd)) Rrange=int(round(mean+sd)) print("range: mean+-sd: ",Rrange," ",Lrange) #round off the range to nearest integer #check if no of ones is within range if(ones>= Lrange and ones<= Rrange): output="Yes" else: output="No" print("Actual number of ones is within the range mean+-sd ??? Ans is: ", output)
getting error line 11
for k in range(size):
^
IndentationError: expected an indented block please
help
import random
import math
myArray=[]
a=0
b=1
size= 50
j=0
n=1000
#populate array with size=50 random integers in range 0-1 and
repeat for 1000 times
while(j<n):
for k in range(size):
randNum=random.randint(a,b)
myArray.insert(k,randNum)
j=j+1
#display array size
print("size of my array is ",len(myArray))
#print myArray 50 values per line
k=0
sub=[]
while(k<len(myArray)):
sub=myArray[k:k+50]
print("...",k,sub)
k=k+50
#count no. of ones and zeroes
zeros=0
ones=0
i=0
while(i<len(myArray)):
if(myArray[i]==1):
ones=ones+1
else:
zeros=zeros+1
i=i+1
print("number of 1 is ",ones)
print("number of 0 is ",zeros)
#calculate mean and SD of binomial distribution
#probability of occurence of 1 is p = 1/2 and occurence of 0 is q
=1/2
#mean of Binomial distribution = np
#SD(standard deviation) of Binomial distribution = (npq)^1/2
#here n=50000 or length of array
n=len(myArray)
p=0.5
q=0.5
mean= n*p #expected mean of no of ones
sd = math.sqrt(n*p*q)
print("===============================")
print("mean of 1s in My Array: ",mean," sd(standard deviation):
",sd)
Lrange=int(round(mean-sd))
Rrange=int(round(mean+sd))
print("range: mean+-sd: ",Rrange," ",Lrange) #round off the range
to nearest integer
#check if no of ones is within range
if(ones>= Lrange and ones<= Rrange):
output="Yes"
else:
output="No"
print("Actual number of ones is within the range mean+-sd ??? Ans is: ", output)
code screenshot:
Expected output: