# This program calculates the Control Chart constant d2 by brute force.
# Samples are generated with mean 0 and std.dev 1.
# Samplesize is first 2 and finally 20
# For each of these a large number of samples are done and RANGE is calculated.
# The mean of all ranges is d2 for the sample size.
import numpy as np
samples = 100000000
print("Samples = ",samples)
print()
for n in (2,3,4,5,6,7,8,9,10,15,20):
iterations = samples
sum = 0
i=0
while i < iterations:
a = np.random.randn(n)
range = max(a)-min(a)
sum += range
i += 1
print(n, sum/iterations)
#OUTPUT:
# Samples = 100000000
#
#2 1.12839154467
#3 1.69256290199
#4 2.05867005648
#5 2.32606679846
#6 2.53443003885
#7 2.70421293596
#8 2.84725965343
#9 2.97003526765
#10 3.07752134982
#15 3.47187549621
#20 3.73506045183