Create an array with a pre determined mean and standard deviation

EORS picture EORS · May 4, 2018 · Viewed 12.1k times · Source

I am attempting to create an array with a predetermined mean and standard deviation value using Numpy. The array needs random numbers within it.

So far I can produce an array and calculate the mean and std. but can not get the array to be controlled by the values:

import numpy as np
x = np.random.randn(1000)
print("Average:")
mean = x.mean()
print(mean)
print("Standard deviation:")
std = x.std()
print(std)

How to control the array values through the mean and std?

Answer

Jundiaius picture Jundiaius · May 4, 2018

Use numpy.random.normal. If your mean is my_mean and your std my_str:

x = np.random.normal(loc=my_mean, scale=my_std, size=1000)