How to generate random numbers from Normal distribution in Numpy

In this post, we will learn how to generate random numbers from normal distribution in Numpy. A normal distribution, also known as the Gaussian distribution, is a continuous probability distribution that is symmetric around the mean, with most of the data points concentrated within three standard deviations of the mean. Normal distribution is one of the most useful distributions as many real-world phenomena, such as test scores and human height measurements approximately follow normal distribution.

Let us get started by loading Numppy

import numpy as np
np.__version__

1.24.0

Basic syntax of normal() function is

normal(loc=0.0, scale=1.0, size=None)

Here loc argument is mean value for the normal distribution. By default it is set to 0. scale argument is Standard deviation (spread or “width”) of the normal distribution. The scale argument must be non-negative and it is set to 1 by default.

Generating a random number from normal distribution: Example 1

Let us generate single random number from normal distribution. First, we need instantiate the Random generator object using default_rng() and then we can use normal() function on the object to generate a random number.

rng = np.random.default_rng()
rng.normal()

-1.8938554026963028

Here we have used normal() function without any arguments. Therefore normal() function has generated a random number from normal distribution with the default mean=0, and standard deviation = 1.0.

Let us specify a mean and standard deviation to generate a random number from normal distribution. Here we have. random number from normal distribution with mean 5 and standard deviation 0.1.

# mean and standard deviation
mu, sigma = 5, 0.1 
rng.normal(mu, sigma)

4.9830485714206425

Generating a random array from normal distribution: Example 2

In this example, we show how to generate an array of random numbers from normal distribution by specifying mean, standard deviation and the number of random numbers.
The size argument to normal() function specifies the number of random numbers we need. In this example, we generate 10 random numbers.

# mean and standard deviation
mu, sigma = 5, 0.1 
# generate a array of size 10
rng.normal(mu, sigma, size =10)

We have generated 10 random numbers from a normal distribution with a mean of 5 and a standard deviation of 0.1.

array([5.08252067, 5.01135043, 5.0100295 , 4.85614838, 4.98456451,
       4.9163278 , 4.961215  , 5.0172716 , 5.07399068, 4.94082445])

Generating a 2D random array from normal distribution: Example 3

Using the. size argument we can generate 2D Numpy array with random numbers sampled from Normal distribution with specific mean and standard deviation. To generate 2D array, we specify a tuple with two elements.

In the example below we are generating a matrix of dimension 4×4 with random numbers sampled from normal distribution with mean 5 and standard deviation 0.1.

rng.normal(mu, sigma, size=(4,4))
array([[5.1241646 , 5.0312379 , 5.08338296, 4.99906334],
       [4.97824727, 4.81334972, 4.94622261, 5.0197842 ],
       [5.00230961, 4.83545188, 4.94407655, 5.01664966],
       [5.08796001, 5.0523658 , 4.95579352, 5.15326347]])

Generating random numbers with seed from normal distribution: Example 4

By setting a seed, an integer number, to the Random Generator object we can reproduce the same random numbers using the same seed. In the example below, we use seed argument to deefault_rng() function to reproduce a single random number from normal disstribuution.

rng = np.random.default_rng(seed = 1234)
print(rng.normal())
-1.6038368053963015

Here we use the seed=1234 to reproduce an array of random numbers from normal distribution.

rng = np.random.default_rng(seed = 1234)
print(rng.normal(mu, sigma, size =10))
[5.00640999 5.07408913 5.01526192 5.08637439 5.29130992 4.85211766
 5.0945473  4.83338645 5.03437446 4.94875563]

And in this example, we use the seed=1234 to reproduce a matrix of random numbers from normal distribution.

print(rng.normal(mu, sigma, size=(4,4)))
[[5.1323759  4.91397198 5.05194932 4.87348563]
 [4.7840861  5.04347339 5.17332893 5.05201342]
 [4.89978342 5.02683455 5.07671747 5.1191272 ]
 [4.88425892 5.06962794 5.03513837 4.99675849]]