NumPy: Create a one dimensional array of forty pseudo-randomly using random numbers from a uniform distribution
NumPy: Basic Exercise-47 with Solution
Create Array of Random Uniform [0,1]
Write a NumPy program to create a one-dimensional array of forty pseudo-randomly generated values. Select random numbers from a uniform distribution between 0 and 1.
This problem involves writing a NumPy program to generate a one-dimensional array containing forty pseudo-randomly generated values selected from a uniform distribution between 0 and 1. By generating random values from a uniform distribution, the program constructs a series of pseudo-random numbers suitable for various statistical and simulation applications.
Sample Solution :
Python Code :
# Importing the NumPy library with an alias 'np'
import numpy as np
# Setting the seed for NumPy's random number generator to 10
np.random.seed(10)
# Generating an array of 40 random numbers from a uniform distribution [0, 1)
print(np.random.rand(40))
Output:
[0.77132064 0.02075195 0.63364823 0.74880388 0.49850701 0.22479665 0.19806286 0.76053071 0.16911084 0.08833981 0.68535982 0.95339335 0.00394827 0.51219226 0.81262096 0.61252607 0.72175532 0.29187607 0.91777412 0.71457578 0.54254437 0.14217005 0.37334076 0.67413362 0.44183317 0.43401399 0.61776698 0.51313824 0.65039718 0.60103895 0.8052232 0.52164715 0.90864888 0.31923609 0.09045935 0.30070006 0.11398436 0.82868133 0.04689632 0.62628715]
Explanation:
In the above code -
np.random.seed(10) sets the random seed value to 10. Setting the seed ensures that the same set of random numbers is generated each time the code is executed. It is useful for reproducibility when working with random numbers in your code.
print(np.random.rand(40)): This line generates an array of 40 random numbers between 0 (inclusive) and 1 (exclusive), using a uniform distribution. The np.random.rand() function is used to generate the random numbers, and the resulting array is printed.
Python-Numpy Code Editor:
Previous: NumPy program to create a two-dimensional array of specified format.
Next: NumPy program to create a two-dimensional array with shape (8,5) of random numbers. Select random numbers from a normal distribution (200,7).
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-47.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics