Here is my code:
N=4
for i in range(N):
random_array=np.array([random.random()])
print(random_array)
print(np.sum(a=random_array))
When I run it, it returns:
[0.09264558]
[0.02441606]
[0.90380555]
[0.51249256]
0.5124925563367195
Why is it only returning the last entry of the array and not summing over them all? Help is much appreciated, thanks.
CodePudding user response:
It's because every iteration the array is overridden, I guess you meant:
N=4
random_array = []
for i in range(N):
random_array.append(random.random())
print(random_array)
print(np.sum(a=random_array))
Example out:
[0.33994599217182764]
[0.33994599217182764, 0.11622954323723345]
[0.33994599217182764, 0.11622954323723345, 0.7288062755665261]
[0.33994599217182764, 0.11622954323723345, 0.7288062755665261, 0.18166667717600626]
1.3666484881515935