• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:17 mins read
You are currently viewing How to Use NumPy random.randn() in Python?

NumPy random.randn() function in Python is used to return random values from the normal distribution in a specified shape. This function creates an array of the given shape and it fills with random samples from the normal standard distribution. This function takes a single integer or sequence of integers to specify the size of an array similar to other NumPy functions like the numpy.ones() and numpy.zeros() functions.

Advertisements

In this article, I will explain how to use the NumPy random.randn() function syntax and use its parameters how it can create an array based on a given shape with examples.

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

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


# Quick examples of numpy random.randn() function

# Example 1: Use random.randn() function
arr = np.random.randn()

# Example 2: Get a 1-dimensional array 
# Of random values
arr = np.random.randn(6)

# Example 3: Get a 2-Dimensional array 
# Of random values
arr = np.random.randn(2,5)

# Example 4: Generate 3-dimensional array 
# Of random values
arr = np.random.randn(5,2,4)

# Example 5: Use random.randn() function
# Use negative integer size
arr = np.random.randn(-2)

2. Syntax of NumPy random.randn()

Following is the syntax of the numpy.random.randn() function.


# Syntax of NumPy random.randn()
random.rand(d0, d1, ..., dn) 

2.1 Parameters of NumPy random.randn()

Following are the parameters of random.randn() function.

  • d0, d1, …, dn – The dimension of the returned array and it must be int type. If no argument is specified a single Python float is returned.

2.2 Return Value

It returns a random array of specified shapes, filled with random values of float type from the standard normal distribution.

3. Usage of NumPy random.randn()

The random.randn() is a numpy library function that returns an array of random samples from the standard normal distribution. It allows specified dimensions as an argument and returns an array of specified dimensions. If you don’t provide any argument, it will return the float value. The np.random.randn() function returns all the samples in float form, which are from the univariate “normal” (Gaussian) distribution of mean 0 and variance 1.

Note : The dimensions of the returned array must be non-negative. If you provide a negative argument, then it will return an error.

The numpy.random.randn() function is used to generate a single random value from a standard normal distribution. Since no size argument is provided, the result is a scalar (a single float). You might get different random numbers when you run the same code multiple times.


# Import numpy module
import numpy as np

# Use random.randn() function
arr = np.random.randn()
print("Random normal distribution:\n",arr) 

Yields below output.

NumPy random randn

4. Get 1-D NumPy Array of Random Values

To get a 1-dimensional NumPy array of random values from a standard normal distribution, you can use the numpy.random.randn() function with the desired size (number of elements) as an argument.

In the below example, np.random.randn(6) creates a 1D array with 6 random numbers sampled from a standard normal distribution. You can change the size argument according to your requirements to get a different number of random values in the array.


# Get 1-dimensional array of random values
arr = np.random.randn(6)
print("Random standard normal distribution:\n",arr)

Yields below output.

NumPy random randn

5. Get 2-D NumPy Array of Random Values

To get a 2-dimensional NumPy array of random values from a standard normal distribution, you can use the numpy.random.randn() function with the desired shape (number of rows and columns) as arguments.

In the below example, np.random.randn(2,5) creates a 2D array with 2 rows and 5 columns, filled with random numbers sampled from a standard normal distribution. Adjust the shape according to your specific requirements to get a different number of rows and columns in the array.


# Get 2-Dimensional array of random values
arr = np.random.randn(2,5)
print("Random 2D array from standard normal distribution:\n",arr)

# Output:
# Random 2D array from standard normal distribution:
#  [[-0.30452334 -0.58965119  1.44620239  0.21178132 -1.23169032]
#  [-0.0982516  -2.03183426  2.61222566 -0.02808967 -0.19355945]]

6. Get 3-D NumPy Array of Random Values

You can also get a 3-dimensional NumPy array of random values from a standard normal distribution, you can use the numpy.random.randn() function with the desired shape (number of matrices, rows, and columns) as arguments.

In the below example, np.random.randn(5,2,4) creates a 3D array with 5 matrices, each containing 2 rows and 4 columns. Adjust the shape according to your specific requirements to get a different number of matrices, rows, and columns in the array.


# Generate 3-dimensional array of random values
arr = np.random.randn(5,2,4)
print("Random 3D array from standard normal distribution:\n",arr)

Yields below output.


# Output:
Random 3D array from standard normal distribution:
 [[[ 0.44586429 -0.75923104  0.24859101 -0.40287479]
  [ 0.28737778 -1.01626203 -0.29871821 -0.27456584]]

 [[-0.21788668 -0.65589284  0.3618381   2.89624239]
  [ 0.44544676 -0.57624386  0.08905999  0.50540734]]

 [[ 0.51415003  1.23862672 -0.4504035  -1.07402353]
  [ 0.89009661 -0.67212432 -0.1423969   0.9323793 ]]

 [[ 1.15087113 -0.81190001  2.89083895  0.44212116]
  [ 1.50648245 -1.54615606  2.1085059  -0.98277968]]

 [[-0.08691203 -0.81200768 -0.05944707 -0.5489188 ]
  [ 0.69155505  0.46401546 -0.65565721 -1.72456006]]]

7. Specify Negative Integer as Parameter

To generate a NumPy array of random values from a standard normal distribution with a negative integer as the size parameter. For instance, np.random.randn(-5) will generate a 1D array with -5 random values. Note that NumPy does not restrict the size to be positive, but keep in mind that the size should be an integer.

It’s worth mentioning that specifying a negative size might be unconventional, and in most cases, you would use positive integers for the size parameter when generating random arrays. Adjust the size according to your specific use case and requirements.


# Use random.randn() function
# Use negative integer size
arr = np.random.randn(-2)
print("Random standard normal distribution with negative integer size:\n",arr)

# Output:
# ValueError: negative dimensions are not allowed

Frequently Asked Questions

What does numpy.random.randn() do?

numpy.random.randn() is a function in the NumPy library for Python that generates an array of random numbers sampled from a standard normal distribution, also known as a Gaussian distribution with a mean of 0 and a standard deviation of 1. In mathematical terms, these random numbers are drawn from a normal distribution with a mean (μ) of 0 and a standard deviation (σ) of 1.

Can I control the shape of the output array?

You can control the shape of the output array generated by numpy.random.randn() by specifying the desired dimensions when calling the function. The dimensions are provided as arguments to the function.

How to change the mean and standard deviation of the distribution?

The numpy.random.randn() function specifically generates random numbers from a standard normal distribution with a mean (μ) of 0 and a standard deviation (σ) of 1. If you need to change the mean and standard deviation, you should use numpy.random.normal() instead. This function allows you to generate random numbers from a normal distribution with a specified mean and standard deviation.

How to generate random numbers from a custom normal distribution?

To generate random numbers from a custom normal distribution with a specified mean and standard deviation, you can use the numpy.random.normal() function.

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

numpy.random.randn() specifically generates random numbers from a standard normal distribution, which has a mean of 0 and a standard deviation of 1. If you want to generate random numbers from other distributions, you should use functions that are designed for those distributions.

What is the difference between numpy.random.randn() and numpy.random.normal()?

numpy.random.randn() generates numbers from a standard normal distribution, while numpy.random.normal() allows you to specify the mean and standard deviation.

Conclusion

In this article, I have explained NumPy random.randn() function and using this how to get the random sample or random samples of single and multi-dimensional arrays based on specified dimensions with examples.

Happy Learning!!

References