I am trying to recreate np.random.randn()
for entertainment without using numpy
library.
The function np.random.randn()
could accept arbitrary number of arguments specifying length of each dimension. For example, np.random.randn(2, 3, 4)
creates a 2 * 3 * 4 matrix where each entry is of standard normal distribution.
I have completed the following but get stuck in assigning each entry value (the line enclosed by #####...
)
import random
from itertools import product
def getStandardNormalTensor(*dimList):
# create empty list
lst = 0
for dim in dimList: lst = [lst] * dim
# populate list with N(0, 1) number
for idx in product(*[list(range(dim)) for dim in dimList]):
#######################################
lst[idx] = random.gauss(0, 1)
#######################################
return lst
where obviously lst
does not accept indexing like lst[(1, 2, 3)]
but only lst[1][2][3]
.
The difficulty I am having now is that I could not get indexing to work as I do not know how many dimensions are there in dimList
(i.e. the length of dimList
).
Could someone help me? Thank you in advance!