How to generate random samples from a normal distribution?

Generating random samples from a normal distribution is a common task in various applications, including statistics and machine learning. Let’s learn how to generate random samples from a normal (Gaussian) distribution in Numpy Python library.
numpy random samples of normal distribution

Random normal function

Numpy random normal function does the job.

Parameters:

  • first parameter is the main value around which the others will be randomly generated
  • second parameter is standard deviation value around the main value from the first parameter
  • the third parameter is a size which is the easiest to set via a tuple as in the example below
  • See also  Python code to draw cos(x) using matplotlib

    How to generate random normal distribution in Python

    Sample code:

    import numpy as np
    
    my_array = np.random.normal(5, 3, size=(5, 4))
    
    print(f"Random samples of normal distribution: \n {my_array}")
    
    

    Random samples of normal distribution has been generated.