LaLaTi
LaLaTi

Reputation: 1725

How to draw a random sample from a normal distribution withing a range with numpy

I am not sure how to specify two arguments for the np.random.normal function:

  1. I want the min=10 and max=100

How do I add this to this code?

np.random.normal(size = 100, loc = 68.32, scale = 25.7)

Upvotes: 1

Views: 672

Answers (1)

Jussi Nurminen
Jussi Nurminen

Reputation: 2408

If you limit the minimum and maximum, the data will no longer be normally distributed. That's why it doesn't make sense for normal to support such arguments.

If you want to pick numbers from normal distribution and subsequently limit to an interval, use something like:

data = np.random.normal(size=1000, loc=68.32, scale=25.7)
data_lim = data[np.where(np.logical_and(data > 10, data < 100))]

If you want numbers from an uniform distribution (which is naturally limited to a certain interval), see np.random.rand

Upvotes: 1

Related Questions