NumPy: Generate five random numbers from the normal distribution
NumPy: Random Exercise-1 with Solution
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:
Previous: NumPy Random Exercises Home.
Next: Write a NumPy program to generate six random integers between 10 and 30.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/numpy/python-numpy-random-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics