0
0
NumPydata~20 mins

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

Choose your learning style9 modes available
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.