Python

NumPy np.random.randn() Function

The random.randn() function in NumPy allows you to generate an array of specified shapes. It works by populating the array with random values per “standard normal” distribution.

In this article, we will explore how to use the random.randn() function in NumPy to generate sample arrays.

np.random.randn() Function

The randn() function takes the dimensions of an array as the arguments and returns a float value or a multidimensional array of the specified shape.

As mentioned, the function returns samples from the standard normal distribution.

The standard normal distribution is a special type of normal distribution where the mean is 0 and has a standard deviation value of 1.

A normal distribution is a symmetrical distribution where the data plotted on a graph forms a bell-like shape. Most of the data clusters around a central point in a normal distribution and taper off as they go farther from the main point.

The randn() function in NumPy has a syntax as shown below:

random.randn(d0, d1, ..., dn)

Where the d0, d1, …, dn refers to an optional int type parameter that dictates the returned array’s dimensions. Ensure the values of the d* parameters are non-negative integers.

NOTE: If no argument is provided, the function returns a single floating-point value.

Generate Random Float Using np.random.randn()

To generate a random float using the randn() function, start by importing NumPy, as shown below:

# import numpy
import numpy as np

To generate a random float, call the randn() function with no arguments, as shown below:

print(np.random.randn())
print(np.random.randn())
print(np.random.randn())
print(np.random.randn())

The previous code should generate random integers and return the values, as shown below:

Create 1D Array Using the randn() Function

We can create a 1-dimensional array using the randn function by specifying one value for the dimension parameter.

An example is shown below:

# 1d array
arr = np.random.randn(5)
display(arr)

The previous code should generate a 1D array with five elements as shown below:

array([ 0.4127406 , -0.24008493, -0.4563451 , -0.65624503,  0.43985204])

Create 2D Array Using the randn() Function

To create a 2D array using the randn() function, we can specify two values to represent the array dimensions.

Consider the code, as shown below:

# 2d array
arr = np.random.randn(2,3)
display(arr)

This should return a 2-dimensional array of 2 rows and 3 columns. An example output is shown below:

array([[-0.08095138,  1.65439459,  0.55345608],
       [ 1.06720002,  0.90974257,  0.48808603]])

NOTE: The parameters in randn(2,3) represent rows and columns, respectively.

Create 3D Array Using the randn() Function

To create a 3D array using the randn() function, we can do the following:

arr = np.random.randn(2,2,2)
display(arr)

This should return a 3D array of random values as shown:

array([[[-2.01110783,  3.0148612 ],
        [-1.3227269 ,  0.96494486]],

       [[ 0.14853023,  1.72551442],
        [ 0.23563147, -1.55067172]]])

Reshaping an Array

After generating a random array, we can use the array.reshape() function to reshape the array into our desired format.

Consider the example below:

# 2d array
arr = np.random.randn(4,6)

In the previous example, we generate a 2D array using the randn() function.

To reshape the array into an 8,3 shape, we can do the following:

display(arr.reshape(8,3))

This should return:

Conclusion

In this tutorial, we learned how to use the np.random.randn function to generate 1, 2, and 3-dimensional arrays populated with sample values per Gaussian distribution. Thanks for reading this article, and happy coding.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list