w3resource

NumPy: Generate six random integers between 10 and 30


2. Random Integers in Range

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

For more Practice: Solve these Related Problems:

  • Generate an array of 8 random integers between 50 and 100 and then sort the array in descending order.
  • Create a function that produces random integers within a range and counts the frequency of each unique value.
  • Generate random integers between two bounds and compute the difference between the maximum and minimum values.
  • Simulate a lottery draw by generating 6 unique random integers within a specified range.

Go to:


PREV : Random Normal Numbers.
NEXT : 3x3x3 Random Array.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.