w3resource

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.

This problem involves writing a NumPy program to generate a vector of length 5 containing random integers between 0 and 10. The task requires utilizing NumPy's random number generation capabilities to create the vector efficiently while ensuring that the integers fall within the specified range.

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)

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).

Visual Presentation:

NumPy: Create a vector of length 5 filled with arbitrary integers from 0 to 10.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://198.211.115.131/python-exercises/numpy/basic/numpy-basic-exercise-23.php