Difference between randn() and normal() functions

Learn about the Difference between randn() and normal() functions in Python NumPy. By Pranit Sharma Last updated : December 25, 2023

NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.

numpy.random.randn() Vs numpy.random.normal() functions

To understand both methods, we need to understand the concept of normal distribution. A normal distribution is a distribution where the values are more likely to occur near the mean value. There are a bunch of cases of this in nature. E.g., the average high temperature in Dallas in June is, let us say, 95 F. It might reach 100, or even 105 average in one year, but it more typically will be near 95 or 97. Similarly, it might reach as low as 80, but 85 or 90 is more likely.

Python numpy.random.normal() Method

The numpy.random.normal() draw random samples from a normal (Gaussian) distribution. It takes some parameters like loc, scale, and size. Loc is the center of the distribution. Scale is the standard deviation of the distribution and size is the output shape. Hence, if we need to generate a generic normal distribution, we use normal().

Python numpy.random.randn() Method

The numpy.random.randn() returns a sample (or samples) from the 'standard normal' distribution. It takes a parameter like (d0, d1, d2, ……dn), these are the dimensions of the returned array, and should be all positive. Hence, if we need to generate a specific normal distribution, we use randn().

Let us understand with the help of an example,

Python code to demonstrate the difference between randn() and normal() functions

Example: numpy.random.normal() Method

import numpy as np

# Using random.normal
res = np.random.normal(0,0.1, 10)

# Display result
print("Result:\n",res)

Output

Example 1: Difference between randn() and normal() functions

Example: numpy.random.randn() Method

import numpy as np

# Using random.randn
res = np.random.randn(2, 4)

# Display result
print("Result:\n",res)

Output

Example 2: Difference between randn() and normal() functions

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.