Using numpy.random.normal with arrays Using numpy.random.normal with arrays python-3.x python-3.x

Using numpy.random.normal with arrays


I don't believe you can control the size parameter when you pass a list/vector of values for the mean and std. Instead, you can iterate over each pair and then concatenate:

np.concatenate(   [np.random.normal(m, s, 100) for m, s in zip(mu, sigma)]) 

This gives you a (400, ) array. If you want a (4, 100) array instead, call np.array instead of np.concatenate.


If you want to make only one call, the normal distribution is easy enough to shift and rescale after the fact. (I'm making up a 10000-long vector of mu and sigma from your example here):

mu = np.random.choice([2000., 3000., 5000., 1000.], 10000)               sigma = np.random.choice([250., 152., 397., 180.], 10000)a = np.random.normal(size=(10000, 100)) * sigma[:,None] + mu[:,None]

This works fine. You can decide if speed is an issue. On my system the following is just 50% slower:

a = np.array([np.random.normal(m, s, 100) for m,s in zip(mu, sigma)])