NumPy: Generate six random integers between 10 and 30
Write a NumPy program to generate six random integers between 10 and 30.
Sample Solution:
Python Code:
# Importing the NumPy library as np
import numpy as np
# Generating a NumPy array 'x' of size 6 with random integers between 10 (inclusive) and 30 (exclusive) using np.random.randint()
x = np.random.randint(low=10, high=30, size=6)
# Printing the array 'x'
print(x)
Sample Output:
[14 25 20 12 27 22]
Explanation:
In the above exercise –
x = np.random.randint(low=10, high=30, size=6): This code generates an array of 6 random integers using the np.random.randint() function. By specifying the low=10 and high=30 parameters, you tell the function to generate random integers within the range of 10 (inclusive) to 30 (exclusive). The size=6 parameter indicates that the array should have 6 elements.
print(x): print() function prints the generated array of random integers.
Pictorial Presentation:
Python-Numpy Code Editor:
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