NumPy: Generate five random numbers from the normal distribution
Write a NumPy program to generate five random numbers from the normal distribution.
Sample Solution:
Python Code:
# Importing the NumPy library as np
import numpy as np
# Generating a NumPy array 'x' of size 5 with random numbers from a normal distribution using np.random.normal()
x = np.random.normal(size=5)
# Printing the array 'x'
print(x)
Sample Output:
[-1.85145616 -0.4639516 0.49787567 1.23607083 -1.33332987]
Explanation:
In the above exercise – x = np.random.normal(size=5): This line generates an array of 5 random numbers sampled from a normal distribution. The np.random.normal() function is used to generate the random numbers. By specifying the size=5 parameter, you tell the function to generate an array of 5 elements.
print(x): This line prints the generated array of random numbers
Pictorial Presentation:
Python-Numpy Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics