w3resource

NumPy: Generate five random numbers from the normal distribution


1. Random Normal Numbers

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:

NumPy Random: Generate five random numbers from the normal distribution

For more Practice: Solve these Related Problems:

  • Generate a 1D array of 10 random numbers from a normal distribution and compute their z-scores.
  • Create a function that generates random normal numbers with a specified mean and standard deviation and returns the array.
  • Simulate a scenario where you generate random normal numbers and then clip the values between two thresholds.
  • Compute the cumulative sum of 5 random numbers drawn from a normal distribution and analyze its trend.

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.



Follow us on Facebook and Twitter for latest update.