i want to create a program which calculates the average of N random numbers taken from a uniform random number distribution.The program must run for
Ν=10,100,1000,10000,100000,1000000 random numbers.Then,i have to plot the mean value as
a function of N.
I did this:
from scitools.std import *
import matplotlib.pyplot as plt
N=10
distribution=[]
for i in range(1,7):
N*=10
random_numbers=[random.uniform(0,1,size=N)]
distribution.append(random_numbers)
plt.semilogx(array(range(N)),array(distribution).mean())
plt.xlabel('N')
plt.grid(True)
plt.show()
It gives me the error in the title in the line where i do the plot.
Also,if there is another ,more pythonic way of doing this i’ll appreciate it.
Thank you.
distributionis a list of arrays,with the inner arrays having different shapes. You can’t form a numpy
array out of such an object by calling
np.array(distribution).distribution. Just compute and store the means. A succinct way to do
that is to use a list comprehension.
from module import *in scripts. It makes it hardto trace where variables come from. The
from module import *wasmade (mainly) for use in interactive sessions, but generally not recommended for scripts.