+4 votes
in Programming Languages by (37.5k points)
How can I select k samples between two numbers from a Gaussian (normal) distribution in Python?

E.g. The samples should be between 10 and 12.

1 Answer

+2 votes
by (233k points)

You can use Numpy's normal() function to draw samples from the parameterized normal distribution. You need to set the values of parameters "loc" [mean ("centre") of the distribution] and "scale" [standard deviation (spread or "width") of the distribution] properly to get the samples between a range.

Here is an example to select ten samples between 10 and 12 from a Gaussian distribution.

>>> import numpy as np
>>> rnd=np.random.default_rng()
>>> rnd.normal(loc=11, scale=0.1, size=10)
array([10.90239842, 10.98048705, 11.05175105, 10.96414277, 10.79745089, 10.95877718, 11.0053723 , 10.82757506, 10.97739127, 11.08950675])


...