Having a fixed mean of normal distribution in Python

Question:

Is there a substitute of np.random.normal()? I am seeking a function which will provide me a fixed mean and not vary with every run as shown below.

import numpy as np
mu, sigma = 50, 1.0 # mean and standard deviation
Nodes=220
r = np.random.normal(mu, sigma, Nodes)
print(r)

mean=np.mean(r)
print("mean =",mean)

Run 1 gives

mean = 49.957893448684665

Run 2 gives

mean = 50.13868428629214
Asked By: user20032724

||

Answers:

You can use a seed to make the random numbers ‘predictable’. This way you fix your random numbers and the mean will stay the same each time you run it. Even better, for everyone, the mean is now the same:

import numpy as np
mu, sigma = 50, 1.0 # mean and standard deviation
Nodes=220

np.random.seed(0)

r = np.random.normal(mu, sigma, Nodes)
mean=np.mean(r)
print("mean =",mean)

Returns: 50.07519566707803

Changing the value (seed value 0 in this case) will change your results

Answered By: T C Molenaar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.