NumPy: Generate an array of 15 random numbers from a standard normal distribution
NumPy: Basic Exercise-18 with Solution
Generate Random Array from Standard Normal
Write a NumPy program to generate an array of 15 random numbers from a standard normal distribution.
This NumPy program generates an array of 15 random numbers drawn from a standard normal distribution, where the mean is 0 and the standard deviation is 1. By leveraging NumPy's random number generation functions, it efficiently produces the desired array. This program highlights the capability of NumPy to generate random samples from various statistical distributions.
Sample Solution :
Python Code :
# Importing the NumPy library with an alias 'np'
import numpy as np
# Generating an array of 15 random numbers from a standard normal distribution using np.random.normal()
rand_num = np.random.normal(0, 1, 15)
# Printing a message indicating 15 random numbers from a standard normal distribution
print("15 random numbers from a standard normal distribution:")
# Printing the array of 15 random numbers
print(rand_num)
Output:
15 random numbers from a standard normal distribution: [ 0.42690788 1.81615544 0.36591912 -0.41417837 -1.13061369 -1.31777265 0.03659045 0.60765805 -0.2148491 0.25934697 -0.89221431 0.33059367 -0.59079163 0.29665161 -0.2753327 ]
Explanation:
The np.random.normal() function generates an array of random numbers from a normal distribution with a mean (loc) of 0 and a standard deviation (scale) of 1. The third argument, 15, specifies the number of random numbers to generate, which is 15 in this case.
Finally print(rand_num) statement prints the generated array of 15 random numbers to the console. The output will vary each time the code is executed due to the random nature of the function.
Python-Numpy Code Editor:
Previous: NumPy program to generate a random number between 0 and 1.
Next: NumPy program to create a vector with values ranging from 15 to 55 and print all values except the first and last.
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://www.w3resource.com/python-exercises/numpy/basic/numpy-basic-exercise-18.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics