Appending Gauss to array

50 views Asked by At

I am trying to write code that appends to array values of Gauss probability density function. I managed to write this (based on appending to lists):

N=100
U = numpy.array([])
for i in range(0, N):
    n = random.random()
    numpy.append(U,n)
for i in range(0, int(N/2)):
    u1 = U[i]
    u2 = U[i+1]
    numpy.append(U,math.sqrt(-2*math.log(u1))*math.cos(2*math.pi*u2))
    numpy.append(U,math.sqrt(-2*math.log(u1))*math.sin(2*math.pi*u2))

but it shows me an "index 0 is out of bounds for axis 0 with size 0" error. Could someone perhaps explain to me why, and what can I do to fix it?

1

There are 1 answers

0
Anis R. On

numpy.append does not happen in-place, meaning that it does not modify the initial array, it just returns a new one with the values appended. So one possible fix to your code is to replace:

numpy.append(U,n)

by

U = numpy.append(U,n)