Numpy randn – Python NumPy random.randn() Function

Numpy randn: The random module is part of the NumPy library. This module includes the functions for generating random numbers. This module includes some basic random data generating methods, as well as permutation and distribution functions and random generator functions.

NumPy random.randn() Function:

Python randn: NumPy’s random.randn() function generates random numbers from a standard normal distribution. The function generates an array of the specified shape and populates it with random samples selected from the standard normal distribution, N(0,1).

The following relationship can be used to generate random values from N(μ,σ2):

σ * np.random.randn() + μ

where μ = mean

σ = standard deviation

Syntax:

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

Parameters

Numpy random.randn: d0, d1, d2, …, dn: This is Optional. Specify the dimensions of the returned array, which must be entirely positive. If no arguments are provided, a single Python float is returned.

Return Value:

np.randn: Normally distributed random values in a specified shape are returned.

NumPy random.randn() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Get a single normally distributed random number using the numpy.random.randn() function and store it in a variable.
  • Print the above generated normally distributed random number.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Get a single normally distributed random number using the numpy.random.randn() function 
# and store it in a variable.
randm_num = np.random.randn()
# Print the above generated normally distributed random number
print("The normally distributed random number generated = ", randm_num)

Output:

The normally distributed random number generated = -0.4516522308806266

Example2

Approach:

  • Import numpy module using the import keyword.
  • Get normally distributed random numbers using the numpy.random.randn() function in a given shape(row_size*col_size) and store it in a variable.
  • Print the above generated normally distributed random numbers in a given shape.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Get normally distributed random numbers using the numpy.random.randn() function in a given 
# shape(row_size*col_size) and store it in a variable.
randm_num = np.random.randn(4, 5)
# Print the above generated normally distributed random numbers in a given shape.
print("The normally distributed random numbers generated in a given shape =\n ", randm_num)

Output:

The normally distributed random numbers generated in a given shape =
[[-0.05703439 0.90294364 -0.29937602 0.90918933 -0.22330781]
 [ 0.50079642 -1.50654505 0.79491178 0.00959949 0.72612899]
 [ 0.65490011 -0.42303323 -0.30130309 1.08312562 -0.36398949]
 [ 0.19115473 0.12114164 0.78822333 -0.48272115 0.14909362]]

Example3

Randn python: We can choose any normal distribution to draw the sample from by using the

σ * np.random.randn() + μ relationship,

where μ = mean

σ = standard deviation

Approach:

  • Import numpy module using the import keyword.
  • Applying the above formula by giving some random shape,  σ and μ values to generate
    normally distributed random numbers.
  • Store it in a variable
  • Print the above generated normally distributed random numbers using the formula given.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Applying the above formula by giving some random shape, σ and μ values
# to generate normally distributed random numbers
# Store it in a variable
randm_num = 8 * np.random.randn(4, 5) + 10
# Print the above generated normally distributed random numbers using the formula given.
print("The normally distributed random numbers generated using the formula given =\n ", randm_num)

Output:

The normally distributed random numbers generated using the formula given =
[[ 3.34694558 2.07408517 19.41238942 9.20545537 10.90070875]
 [-5.70580461  5.25773497 7.30316076 -1.04195376 3.3744448 ]
 [12.11257151  8.68555075 26.04407143 12.29987094 6.75866292]
 [29.46198517 -1.10219534 14.51084917 5.91377164 13.55330625]]