I want to generate random numbers, lets say 100. Now I am using numpy for the purpose like:
print numpy.random.normal(loc=200,scale=50,size=100)
But I want the numbers to be generated only one standard deviation apart from the mean value i.e loc. What would be the best way ?
Should I simply do something like:
print numpy.random.randint(150, high=250, size=100)
or is there any other way of doing it ?
As suggested in @Robert Kern's comment use scipy's truncnorm
from scipy import stats
arr = stats.truncnorm.rvs(-1, 1, loc=200.0, scale=50.0, size=1)
print(arr[0])
The first two parameters indicate the range [a, b] (measured in standard deviations), loc
is the mean (the center of the distribution), and scale
is the standard deviation (how spread the distribution is).
import matplotlib.pyplot as plt
from scipy import stats
# -1 to +3 standard deviations apart
r = stats.truncnorm.rvs(-1, 3, loc=200.0, scale=50.0, size=10**6)
plt.hist(r, bins=100, color='blue')
plt.xlabel("value")
plt.ylabel("frequency")
plt.show()
Sidenote: randint()
and normal()
do not pick a number the same way.
Return random integers from the “discrete uniform” distribution in the “half-open” interval.
Draw random samples from a normal (Gaussian) distribution.
The odds of getting any number in the chosen interval using randint()
is the same, unlike numbers from a normal distribution (odds of getting a number closer to the peak are greater).