• Post author:
• Post category:NumPy / Python

NumPy` random.normal()` function in Python is used to create an array of specified shape and fills it with random values from a normal (Gaussian) distribution. This distribution is also known as Bell Curve because of its characteristic shape.

This function takes an int or tuple of int, to specify the size of an array, similar to other NumPy functions like the numpy.ones() and numpy.zeros(). In this article, I will explain `numpy.random.normal()` function syntax and using its parameters how to get random values of the single and multi-dimensional arrays with examples.

1. Quick Examples of NumPy random.normal() Function

If you are in a hurry, below are some quick examples of how to use NumPy random.normal() in Python.

``````
# Quick examples of numpy random.normal() function

# Example 1: Get the random samples of normal distribution
arr = np.random.normal(size = 5)

# Example 2: Get the random samples of normal distribution
arr = np.random.normal(size = (3,2))

# Example 3: Get the mean value of random values
arr = np.random.normal(loc = 2)

# Example 4: Get the mean of random values
desired_mean = 2
arr = np.random.normal(loc=desired_mean, size=(3, 4))

# Example 5: Get the standard deviation of random values
arr = np.random.normal(scale = 4.0)

# Example 6: Generate random samples
# Along the specified standard deviation
desired_std_dev = 2
arr = np.random.normal(scale=desired_std_dev, size=(3, 4))

# Example 7: Get the random values
arr = np.random.normal( loc = 2, scale = 4.0, size = 5)
``````

2. Syntax of NumPy random.normal()

Following is the Syntax of np.random.normal().

``````
# Syntax of NumPy random.normal()
numpy.random.normal(loc = 0.0, scale = 1.0, size = None)
``````

2.1 Parameters of random.normal()

Following are the parameters of normal() function.

• `loc` :  It is optional. Like an array of a float value. It specifies the mean of the distribution. By default, it is set to 0.0.
• `scale` :  It is optional. Like an array of a float value. It specifies the standard deviation. By default, it is set to 1.0. It must not be a negative value.
• `size` : It is optional and is an integer or a tuple of integers. It specifies the shape of the returned array. If the size is None. By default, it is set to value 1.

2.2 Return Value of random.normal()

It returns an array of random values from a normal distribution along with the specified shape.

3. Usage of NumPy random.normal()

`random.normal()` function is the built-in function in the NumPy module package of Python. This function generates random samples from a normal distribution/Gaussian distribution. When you pass the int or tuple of int as a parameter into this function, it will return the array of specified size or multi-dimensional array of random samples of normal distribution.

You can use `numpy.random.normal()` to generate an array of random samples from a normal distribution. For instance, first, import the NumPy library and alias it as `np` for convenience. Generate an array `arr` with 5 random samples from a standard normal distribution (mean=0, standard deviation=1). You can customize the size parameter to control the length of the array.

``````
# Import numpy module
import numpy as np

# Get the random samples of normal distribution
arr = np.random.normal(size = 5)
print("Random samples of normal distribution:\n",arr)
``````

Yields below output.

You can also use the `numpy.random.normal()` to generate random samples from a normal distribution and store them in a 3×2 array. For example, generate a 3×2 array (`size=(3,2)`) of random samples from a standard normal distribution (mean=0, standard deviation=1).

``````
# Get the random samples of normal distribution
arr = np.random.normal(size = (3,2))
print("Random samples of normal distribution:\n",arr)

# Output:
# Random samples of normal distribution:
#  [[-0.07927722 -0.26185901]
#  [ 1.17315204 -0.04157541]
#  [-0.19744901  0.16464897]]
``````

Each time you run the code, you will likely get different random samples due to the nature of the random number generation.

4. Get the NumPy Random Values along Loc

When you pass `loc` parameter into this function, it will return the mean of the array’s random values from a normal distribution. When not used, by default it uses a 1.0 value.

You are generating a single random value from a normal distribution with a specified mean (`loc=2`). The `numpy.random.normal()` function is used to create random samples from a normal distribution. For instance, generate a single random sample from a normal distribution with a mean of 2.

``````
# Get the mean of random values
arr = np.random.normal(loc = 2)
print("Random samples along the specified mean(loc):\n",arr)

# Output:
# Random samples along the specified mean(loc):
# 2.5011574792753852
``````

To generate an array of random values from a normal distribution along a specific mean (`loc`), you can use the `numpy.random.normal()` function with the desired mean and specify the size of the output array.

In the below example, `desired_mean` is set to 2, and the `numpy.random.normal()` function is used to generate a 3×4 array of random samples with the specified mean. Adjust the `desired_mean` and `size` parameters according to your specific requirements.

``````
# Specify the desired mean (loc)
desired_mean = 2

# Get the mean of random values
arr = np.random.normal(loc=desired_mean, size=(3, 4))
print("Random samples along the specified mean(loc):\n",arr)

# Output:
# Random samples along the specified mean(loc):
# [[0.55135362 2.50650811 1.19201646 0.90987418]
# [1.04941199 2.38934861 2.57217131 0.97014783]
# [3.40498152 1.8830481  2.86941805 0.70000707]]
``````

5. Get the NumPy Random Values along Scale

When you pass the `scale` parameter into this function, it will return the standard deviation of the array’s random values from the normal distribution. When not used, by default it uses a 0.0 value.

``````
# Get the standard deviation of random values
arr = np.random.normal(scale = 4.0)
print("Random samples along the specified standard deviation(scale):\n",arr)

# Output:
# Random samples along the specified standard deviation(scale):
#  2.204554440088057
``````

Alternatively, to generate random values from a normal distribution along a specific standard deviation (`scale`), you can use the `numpy.random.normal()` function with the desired standard deviation and specify the size of the output array.

In the below example, `desired_std_dev is set` to 2, and the `numpy.random.normal()` function is used to generate a 3×4 array of random samples with the specified standard deviation. Adjust the `desired_std_dev` and `size` parameters according to your specific requirements.

``````
# Specify the desired standard deviation (scale)
desired_std_dev = 2

# Generate random samples along the specified standard deviation
arr = np.random.normal(scale=desired_std_dev, size=(3, 4))
print("Random samples along the specified standard deviation (scale):\n", arr)

# Output:
# Random samples along the specified standard deviation (scale):
# [[-0.32245366  0.3488419   2.74035937  1.11156448]
# [-0.91152624 -0.3869614   1.65888942 -1.17710673]
# [ 0.98915812  1.05648689  1.94941816  2.84545044]]
``````

6. Get the Random Values along Loc, Scale, & Size

If you want to generate random values from a normal distribution with specific mean (`loc`), standard deviation (`scale`), and `size`, you can use the `numpy.random.normal()` function with the desired parameters. It will return the array of the specified size of random samples from a normal distribution. Let’s take the example,

``````
# Get the random values
arr = np.random.normal( loc = 2, scale = 4.0, size = 5)
print(arr)

# Output:
# [ 0.63610699 -1.31665607  0.85681477 -0.08999122  3.95373571]
``````

7. Graphical representation of NumPy random.normal() function

To make a histogram with the help `pyplot` library and print the graph of NumPy `random.normal()` function.

``````
# Import pyplot library
from matplotlib import pyplot as plt

output = np.random.normal( 1, 2.0, size = 5)

print(output)
count, bins, ignored = plt.hist( output, 50)
plt.show()

# Output:
# [ 3.19098457 -1.50999446  2.53421593  4.44277223  4.1947252 ]
``````

What does numpy.random.normal() do?

`numpy.random.normal()` is a function in the NumPy library that generates random samples from a normal (Gaussian) distribution. The normal distribution is characterized by two parameters: the mean (or average) and the standard deviation (a measure of the spread or width of the distribution).

Can I generate a single random number?

You can use `numpy.random.normal()` to generate a single random number. To do this, you need to set the `size` parameter to 1.

Can I use numpy.random.normal() for other distributions?

`numpy.random.normal()` specifically generates random samples from a normal (Gaussian) distribution. If you want to generate random samples from other distributions, NumPy provides different functions for various distributions.

How do I use it to generate random numbers with a specific mean and standard deviation?

To generate random numbers with a specific mean and standard deviation using `numpy.random.normal()`, you need to set the `loc` parameter to the desired mean and the `scale` parameter to the desired standard deviation.

Is the generated data truly random?

The term “random” in the context of computer-generated random numbers can be a bit misleading. The numbers generated by `numpy.random.normal()` and similar functions are not truly random in the sense of being unpredictable. They are generated using algorithms and are considered pseudorandom.

Conclusion

In this article, I have explained NumPy `random.normal()` and using this how to get the random values of 1-D NumPy array and multidimensional array, where the elements are from a normal distribution.