Bird
Raised Fist0
NumPydata~20 mins

Integer random with integers() in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Integer Random Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of numpy integers() with size parameter
What is the output of the following code snippet?
NumPy
import numpy as np
np.random.default_rng(seed=42).integers(low=5, high=10, size=3)
A[7 8 5]
B[6 5 5]
C[7 5 6]
D[7 5 5]
Attempts:
2 left
💡 Hint
Remember that integers() generates random integers in [low, high) range.
❓ data_output
intermediate
2:00remaining
Shape and values from integers() with 2D size
What is the shape and one possible output of this code?
NumPy
import numpy as np
rng = np.random.default_rng(1)
rng.integers(0, 3, size=(2, 2))
A
Shape: (2, 2), Output: [[0 2]
 [1 0]]
B
Shape: (2, 2), Output: [[1 0]
 [0 2]]
C
Shape: (2, 2), Output: [[1 2]
 [0 1]]
D
Shape: (2, 2), Output: [[2 2]
 [1 0]]
Attempts:
2 left
💡 Hint
Check the shape parameter and the random seed effect.
🔧 Debug
advanced
2:00remaining
Identify the error in this integers() call
What error does this code raise?
NumPy
import numpy as np
rng = np.random.default_rng()
rng.integers(low=10, high=5)
ANo error, returns an integer
BTypeError: integers() missing required argument 'size'
CSyntaxError: invalid syntax
DValueError: high must be greater than low
Attempts:
2 left
💡 Hint
Check the relationship between low and high parameters.
🧠 Conceptual
advanced
2:00remaining
Range of values generated by integers()
Which statement about numpy's integers() output range is correct?
Aintegers(low, high) generates values in [low, high] inclusive
Bintegers(low, high) generates values in [low, high) exclusive of high
Cintegers(low, high) generates values in (low, high) exclusive of both ends
Dintegers(low, high) generates values in (low, high] inclusive of high only
Attempts:
2 left
💡 Hint
Think about typical Python range behavior.
🚀 Application
expert
3:00remaining
Generate a 1D array of 5 unique random integers between 0 and 9
Which code snippet correctly generates 5 unique random integers between 0 and 9 using numpy's integers()?
A
rng = np.random.default_rng()
rng.choice(10, size=5, replace=False)
B
rng = np.random.default_rng()
rng.integers(0, 10, size=5)
C
rng = np.random.default_rng()
rng.integers(0, 10, size=5, replace=False)
D
rng = np.random.default_rng()
rng.integers(0, 10, size=5, endpoint=True)
Attempts:
2 left
💡 Hint
integers() does not support replace=False, but choice() does.

Practice

(1/5)
1.

What does the numpy.random.integers(low, high) function do?

easy
A. Generates random numbers only equal to low or high.
B. Generates random whole numbers from low (inclusive) to high (inclusive).
C. Generates a sequence of numbers from low to high.
D. Generates random decimal numbers between low and high.

Solution

  1. Step 1: Understand the function purpose

    numpy.random.integers(low, high) generates random integers in a range.
  2. Step 2: Check the range behavior

    The function generates from low inclusive to high inclusive (default endpoint=True).
  3. Final Answer:

    Generates random whole numbers from low (inclusive) to high (inclusive). -> Option B
  4. Quick Check:

    integers() returns [low, high] random integers [OK]
Hint: integers() generates [low, high] by default [OK]
Common Mistakes:
  • Thinking it generates decimal numbers
  • Thinking high is exclusive
  • Confusing with sequence generation
2.

Which of the following is the correct syntax to generate 5 random integers between 1 and 10 using numpy.random.integers()?

import numpy as np
# Your code here
easy
A. np.random.integers(1, 10, size=5)
B. np.random.integers(1, 10, count=5)
C. np.random.integers(low=1, high=10, count=5)
D. np.random.integers(1, 10, length=5)

Solution

  1. Step 1: Check the function parameters

    integers(low, high, size=number) generates an array of random integers.
  2. Step 2: Identify correct keyword for number of values

    The correct keyword is size, so size=5 generates 5 numbers.
  3. Final Answer:

    np.random.integers(1, 10, size=5) -> Option A
  4. Quick Check:

    Use size= for array length [OK]
Hint: Use size= to specify number of random integers [OK]
Common Mistakes:
  • Using positional third argument without size keyword
  • Using wrong keyword like count or length
  • Missing import statement
3.

What is the output of the following code?

import numpy as np
np.random.seed(0)
arr = np.random.integers(1, 5, size=4)
print(arr)
medium
A. [5 4 3 5]
B. [1 2 3 4]
C. [3 3 3 3]
D. [4 4 1 3]

Solution

  1. Step 1: Set random seed for reproducibility

    Using np.random.seed(0) fixes the random numbers generated.
  2. Step 2: Generate 4 random integers between 1 and 5 inclusive

    np.random.integers(1, 5, size=4) produces the array [4 4 1 3] with this seed.
  3. Final Answer:

    [4 4 1 3] -> Option D
  4. Quick Check:

    Seed 0 + integers(1,5,4) = [4 4 1 3] [OK]
Hint: Use seed to get repeatable random integers [OK]
Common Mistakes:
  • Ignoring seed and expecting different output
  • Confusing inclusive range with exclusive
  • Miscounting size parameter
4.

Find the error in this code snippet and choose the correct fix:

import numpy as np
arr = np.random.integers(0, 10, 5)
print(arr)
medium
A. No error; code runs correctly as is.
B. Change third argument to size=5 to specify array size.
C. Add parentheses around arguments: integers((0,10),5).
D. Replace integers with randint to fix syntax.

Solution

  1. Step 1: Check function signature

    integers(low, high, size=...) accepts size as positional or keyword argument.
  2. Step 2: Identify argument usage

    Passing 5 as positional third argument works correctly.
  3. Final Answer:

    No error; code runs correctly as is. -> Option A
  4. Quick Check:

    Positional size argument works [OK]
Hint: size can be positional or keyword in integers() [OK]
Common Mistakes:
  • Confusing integers() with randint()
  • Using wrong keyword like count or length
  • Assuming positional size causes error
5.

You want to generate a 2x3 array of random integers from 10 to 20 using numpy.random.integers() and then filter to keep only even numbers. Which code correctly does this?

hard
A. arr = np.random.integers(5, 10, size=(2,3)) * 2
B. arr = np.random.integers(10, 20, size=(2,3)); arr = arr * 2
C. arr = np.random.integers(10, 20, size=(2,3)); arr = arr[arr % 2 == 0]
D. arr = np.random.integers(10, 20, size=(2,3)); arr = arr[arr % 2 != 0]

Solution

  1. Step 1: Generate 2x3 array of integers 10 to 20

    Use np.random.integers(10, 20, size=(2,3)) to get random integers in the range.
  2. Step 2: Filter only even numbers

    Use boolean indexing arr[arr % 2 == 0] to keep even numbers only.
  3. Final Answer:

    arr = np.random.integers(10, 20, size=(2,3)); arr = arr[arr % 2 == 0] -> Option C
  4. Quick Check:

    Filter with modulo 2 equals zero for evens [OK]
Hint: Filter with arr % 2 == 0 to keep even numbers [OK]
Common Mistakes:
  • Multiplying random numbers instead of filtering
  • Filtering odd numbers by mistake
  • Using wrong range for even numbers