w3resource

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:

NumPy Random: Generate six random integers between 10 and 30

Python-Numpy Code Editor: