NumPy: Create a vector of length 5 filled with arbitrary integers from 0 to 10
NumPy: Basic Exercise-23 with Solution
Write a NumPy program to create a vector of length 5 filled with arbitrary integers from 0 to 10.
Sample Solution :
Python Code :
# Importing the NumPy library with an alias 'np'
import numpy as np
# Generating a NumPy array 'x' containing 5 random integers between 0 and 10 using np.random.randint()
x = np.random.randint(0, 11, 5)
# Printing a message indicating a vector of length 5 filled with arbitrary integers from 0 to 10
print("Vector of length 5 filled with arbitrary integers from 0 to 10:")
# Printing the generated vector 'x'
print(x)
Sample Output:
Vector of length 5 filled with arbitrary integers from 0 to 10: [ 0 10 2 0 6]
Explanation:
In the above code np.random.randint() function generates an array of random integers. The function takes three arguments: the lower bound (inclusive), the upper bound (exclusive), and the number of random integers to generate. In this case, it generates an array of 5 random integers between 0 (inclusive) and 11 (exclusive).
Pictorial Presentation:
Python-Numpy Code Editor:
Previous: NumPy program to create a vector with values from 0 to 20 and change the sign of the numbers in the range from 9 to 15.
Next: NumPy program to multiply the values of two given vectors.
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